Put an 'X' in the corner of a JPanel, what's the trick?

I've seen applications (I assume written in java) where there is an 'X' to dispose() of a panel or toolbar.
Here I've done a quick and dirty mock up of my application. If someone could oblige with a line or 2 showing how to do this, or even a link to the API /forum thread (that I've already searched) - much will be appreciated. Also please bear in mind that I need to keep clear of the 1.4 API 'cos of backward compatability issues, taa.import java.awt.*;
import javax.swing.*;
public class justjunk{
   public static void main(String []args){
      new justjunk();
   justjunk(){
      JFrame frame = new JFrame("demo only");
      JPanel north = new JPanel();
      north.add(new JLabel("some text label"));
      north.add(new JButton("A button"));
      north.add(new JButton("nuther button"));
      *    I want to set an 'X' at the top right hand corner of JPanel
      *    west, so that it can be closed at the discretion of the user,
      JPanel west = new JPanel();
      west.setPreferredSize( new Dimension( 100, (frame.getHeight()-north.getHeight()) ) );
      west.setLayout(new FlowLayout(FlowLayout.CENTER));
      west.setBorder(BorderFactory.createTitledBorder(" My Toolset : "));
      JButton []button = new JButton[8];
         for(int i=0; i<button.length; i++){
            button[i] = new JButton("button "+(i+1));
            west.add(button);
frame.getContentPane().add(north, BorderLayout.NORTH);
frame.getContentPane().add(west, BorderLayout.WEST);
frame.getContentPane().add(new JTextArea(), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);

Here's a suggestion
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class XMarksTheSpot {
    public static void main(String []args){
        new XMarksTheSpot();
    XMarksTheSpot() {
        final JFrame frame = new JFrame("demo only");
        frame.addWindowListener(new WindowAdapter()
            public void windowClosing(WindowEvent e)
                System.exit(0);
        final JPanel north = new JPanel();
        north.add(new JLabel("some text label"));
        north.add(new JButton("A button"));
        north.add(new JButton("nuther button"));
        JLabel x = getXLabel();
        x.addMouseListener(new MouseAdapter()
            public void mousePressed(MouseEvent e)
                north.setVisible(false);
        north.add(x);
         * I want to set an 'X' at the top right hand corner of JPanel
         * west, so that it can be closed at the discretion of the user,
        final JPanel west = new JPanel();
        west.setPreferredSize( new Dimension( 100, (frame.getHeight() -
                                                    north.getHeight()) ) );
        west.setLayout(new FlowLayout(FlowLayout.CENTER));
        west.setBorder(BorderFactory.createTitledBorder(" My Toolset : "));
        x = getXLabel();
        x.addMouseListener(new MouseAdapter()
            public void mousePressed(MouseEvent e)
                frame.remove(west);
                frame.validate();
                frame.repaint();
        west.add(x);
        JButton []button = new JButton[8];
        for(int i=0; i<button.length; i++) {
            button[i] = new JButton("button "+(i+1));
            west.add(button);
frame.getContentPane().add(north, BorderLayout.NORTH);
frame.getContentPane().add(west, BorderLayout.WEST);
frame.getContentPane().add(new JTextArea(), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
private JLabel getXLabel()
JLabel label = new JLabel("X", JLabel.RIGHT);
label.setPreferredSize(new Dimension(45, 25));
//label.setBorder(BorderFactory.createEtchedBorder());
label.setFont(new Font("ludida bright demibold", Font.PLAIN, 24));
label.setForeground(Color.red);
return label;

Similar Messages

Maybe you are looking for

  • Merge Replication Error When Initialising subscription

    I have a SQL server 2005 server with a merge publication of 6 articles all of which are tables which is also the distributor. I have two existing subscriptions one from a SQL 2005 Server and one from a SQL Server 2008 R2 Server both of which are work

  • Delete only one file from Trash

    Hello There, I do not want to empty my entire Trash folder but only some files which i know I am not going to use it. Is there a way to delete only selected files and not just empty trash... Thanks in advance for the help.. NA

  • How to remove my credit card information from my apple I'd?

    Guys, I received the receipt of a wrong transaction that I didn't initiate on I tunes. I need to either remove my credit card information completely, so that every time I buy anything I'll put it manually. Please provide a solution. Thanks Ashish

  • Error message and bbm failure.

    Blackberry Curve 8520, OS5, carrier Orange. Three problems: 1.  When I turn on the phone I get an error message: "You are attemptin g to open a secure connection but the senders certificate is not trusted" Options are "Continue", "Close connection",

  • When backing up catalog it loops after about 70%-PS7

    Hi I am trying to do by first backup of the catalog of about 3000 files.To external drive Always, after about 70% it ends up in a loop with "Program not responding" shown in Task manager. I have     checked that the catalog does not need repairing