"JTabbedPane & WindowClosing"

Hi All Java Programmers,
I would appreciate if you can provide me with any kind of article, souceCode, demo anything.
This is My Scenarion:
I have a JTabbedPane. I have some editable JTextFields there. If the user make any mistake I display error messages and disable the cancel button so they can't close the window until and unless they correct the errors. Inspite of trying my best I couldn't solve the following three issues(I am not sure if any of them is bug or what).
Problem 1: If there is an error and I try to change the tab, it allows me to do that and the error message pops up after changing the tab. How can I protect this so I won't be able to change the tab until and unless I correct the error? And the error message will also pop up anytime I try to change the tab.
Problem 2: If there is an error it allows me to close the window and none of the error messages pop up. How can I protect this?
Problem 3: Let's say there is an error. I go to another application. When I come back the application is frozen and the cursor is not there anymore.
Any kind of help will be appreciated.
Thanks,
N

Here are some general suggestions of how to handle the problems
Problem one:
set last_tabpane_selected to 0 to initialize
on the tabbed pane set a change listener
tabPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// verify the fields on the last_tabpane_selected
// if you do not pass verification use the tabbedPane changeSelection function to force
// the correct pane and then display your error.
// if passed set last_tabpane_selected to current one
Keep last_tabpane_selected
Problem two:
On the frame you are using add an internal frame listener
/* Initialize exit control */
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addInternalFrameListener(new InternalFrameAdapter() {
public void internalFrameActivated(InternalFrameEvent e){}
public void internalFrameClosed(InternalFrameEvent e){}
public void internalFrameClosing(InternalFrameEvent e){
int selectedPane = tabPane.getSelectedIndex(); //identify current table on displayed
// here call routine to verify fields on the selectedPane tabPane
// if they pass
dispose();
// else just continue
public void internalFrameDeactivated(InternalFrameEvent e){}
public void internalFrameDeiconified(InternalFrameEvent e){}
public void internalFrameIconified(InternalFrameEvent e){}
public void internalFrameOpened(InternalFrameEvent e){}
Problem three:
I have no answer but have had similar problems.
rykk

Similar Messages

  • Plz help me out!! Custom components not showed in JTabbedPane....

    Hi!!
    I am extending a JPanel and adding some components to it and finally when I create an object and add it to the JTAbbedPane its not being shown....If i do it disrectly on JPanel instead of my class its is being shown.can anybody...please help me out and tell where the problem is ...My code is as follows....
    public class TabColorExample extends JPanel {
    public TabColorExample() {
    setLayout(new BorderLayout());
    UIManager.put("TabbedPane.selected", Color.green);
    JTabbedPane tabbedPane = new JTabbedPane();
    String tabs[] = {"One", "Two", "Three", "Four"};
    Color[] colors = {null, Color.red, null, null};
    for (int i=0;i<tabs.length;i++) {
    tabbedPane.addTab(tabs, createPane(tabs[i]));
    tabbedPane.setBackgroundAt(i, colors[i]);
    tabbedPane.setSelectedIndex(0);
    add(tabbedPane, BorderLayout.CENTER);
    JPanel createPane(String s) {
    return new panel(s);
    public static void main(String[] args) {
    JFrame frame = new JFrame("Tab color Example");
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    System.exit(0);
    frame.getContentPane().add( new TabColorExample() );
    frame.setSize( 200, 100 );
    frame.setVisible(true);
    class panel extends javax.swing.JPanel
         panel(String s)
              super();
              JPanel p = new JPanel();
              p.add(new JLabel(s));
    Thanx in advance...
    Krtrao

    Hi there,
    Having that string in a mapping of DSO when DSO will process the SIDs it doesn't like that string and therefore throws an error.
    You could for example transform that field in for example start routine or customer exit R/3 side, like for example split string with commas, and concatenate everything like this: 01,02,03 should retrieve 010203.
    Other option is to first create master data texts datasource for that field PDP and for each key retrieves a string, like this for example:
    010203 - text 01,02,03, or text MDTDWD
    than when you fill the DSO with that field aready fixed in the form 010203, you can in the query represent it with text, it should represent in the form 01,02,03 or MDTDWD, see the picture?
    You can do this kind of fixing of the filed 01,02,03 or in the start routine, or the transfer structure.
    And to only get those records with the field being there, just write in the start routine something like:
    DELETE <SOURCE_PACKAGE> where your_field is initial.
    Hope this helps,
    Diogo.

  • Doubt in JTabbedPane ..

    Hi,
    I am having a JTabbedPane and using the following code to add Panels to JTabbedPane.
    public class IntegratedUI extends JPanel{
         public IntegratedUI() {
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel usagepanel = UsageConsole.makeFrame();
    tabbedPane.addTab("UsageConsole",usagepanel);
    tabbedPane.setSelectedIndex(0);
    JPanel deploymentpanel =Generator.makedeploymentFrame();
    tabbedPane.addTab("Deployment",deploymentpanel);
    JPanel generatorpanel =WGenarator.makegeneratorFrame();
    tabbedPane.addTab("Generator",generatorpanel);
    setLayout(new GridLayout(1,1));
    add(tabbedPane);
    public static void main(String[] args) {
    JFrame frame = new JFrame("XYZ");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new IntegratedUI()
    frame.setBounds(0,0,800,400);
    frame.setVisible(true);
    My JTabbedPane is displaying fine but my first Panel USageConsole is getting displayed.My second and third panel is not getting displayed.While i tried to debug using breakpoints.its calling the first class fine.The second and third class is not getting called.What could be the problem..
    Thanks for your reply in advance.

    It looks like your Generator and WGenerator classes have static methods that are supposed to return JPanels, hopefully with components on them. I would write a small test app to verify that you can show these on their own; say, add each one to a JPanel with GridBagLayout, one panel per row. This way you can really see what's going on. I don't see any obvious problems in your tabbed pane code.

  • JTabbedPane with JTextField

    Can anyone provide me with an example on how to incorporate a JTextField into a JTabbedPane? There are many examples of creating JButtons as components of a JTabbedPanes but nothing for JTextFields. Can this be done? I get some wierd compiler errors when I try it?
    Any help will be much appreciated. Thanks.

    Thanks for the quick response but...
    My question was "how to incorporate these fields into a JTabbedPane."
    I'm new to Java so I have "borrowed" a Sun Tutorial JTabbedPane example which includes one JLable component per tab and tried to add a JTextField.
    Here is "my" code.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TabbedPaneDemo extends JFrame {
    // Initialize Screen Text Fields
    private JTextField lastName = null;
    //Vector recordData = new Vector(20);
    public TabbedPaneDemo() {
    ImageIcon icon = new ImageIcon("images/middle.gif");
    JTabbedPane tabbedPane = new JTabbedPane();
    Component panel1 = makeRecordPanel("Teacher Record");
    tabbedPane.addTab("Teacher", icon, panel1, "Update Teacher Information");
    tabbedPane.setSelectedIndex(0);
    Component panel2 = makeRecordPanel("Parent Record");
    tabbedPane.addTab("Parent", icon, panel2, "Update Parent Information");
    Component panel3 = makeRecordPanel("Child Record");
    tabbedPane.addTab("Child", icon, panel3, "Update Child Information");
    //Add the tabbed pane to this panel.
    setLayout(new GridLayout(1, 1));
    add(tabbedPane);
    protected Component makeRecordPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel label1 = new JLabel("Last Name");
    JTextField lastName = new JTextField(20);
    label1.setHorizontalAlignment(JLabel.LEFT);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(label1);
    panel.add(lastName);
    return panel;
    public static void main(String[] args) {
    JFrame frame = new JFrame("Maintain DayCare Records");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new TabbedPaneDemo(),
    BorderLayout.CENTER);
    frame.setSize(800, 600);
    frame.setVisible(true);
    This compiles OK but when I try to run it I get the following run-time errors.
    java.lang.Error: Do not use TabbedPaneDemo.setLayout() use TabbedPaneDemo.getContentPane().setLayout() instead
         at javax.swing.JFrame.createRootPaneException(JFrame.java:333)
         at javax.swing.JFrame.setLayout(JFrame.java:394)
         at TabbedPaneDemo.<init>(TabbedPaneDemo.java:29)
         at TabbedPaneDemo.main(TabbedPaneDemo.java:53)
    Any thoughts?

  • Strangeness Using HTML with JTabbedPane

    I'm getting some weird behavior when using html format for a JTabbedPane. It seems when I create tabs using HTML, it works fine, but when I subsequently try to change the tab title using JTabbedPane.setTitleAt(), the title doesn't change. The title doesn't change only when I create it with html. If I use "normal" text, the title changes fine.
    I imagine JTabbedPane has a problem with html. I am using version 1.3 and compiling with JBuilder. Below is an example of my program. Uncomment mentioned lines to see it react strangely.
    Any help would be appreciated.
    -joel
    package junk;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class BugExample extends JFrame {
    BugExample() {
    super( "Bug Example" );
    JTabbedPane tabs = new JTabbedPane();
    JPanel aPan= new JPanel();
    //html for tab index 0
    tabs.addTab("<html><center><font face=\"Dialog\" size = -1><b>Rate 1: " +
    "<font color=#008040>5.6%</b></font></font></center></html>", new JPanel());
    //"Normal" text for tab index 0. If I use this, then the setTitleAt works
    // properly. To try this, make sure the previous addTab() is commented out
    // tabs.addTab("Rate 1:5.7%", new JPanel());
    //html for tab index 1
    tabs.addTab("<html><center><font face=\"Dialog\" size = -1><b>Rate 2: " +
    "<font color=#008040>9.2%</b></font></font></center></html>", new JPanel());
    getContentPane().add(tabs);
    //Uncomment next line and run again using the html style addTab() for index 0.
    // I would think the tab at index 0 would show this new text
    // In fact, it will if I do not use html for index 1.
    // tabs.setTitleAt(0, "Rate it again");
    setSize( 400, 220 );
    public static void main(String[] args) {
    BugExample frame = new BugExample();
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
         System.exit(0);
    System.err.println("Starting");
    frame.setVisible(true);

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class BugExample extends JFrame {
    BugExample() {
    super( "Bug Example" );
    JTabbedPane tabs = new JTabbedPane();
    JPanel aPan= new JPanel();
    //html for tab index 0
    tabs.addTab("<html><center><font face=\"Dialog\" size = -1><b>Rate 1: <font color=#008040>5.6%</b></font></font></center></html>", new JPanel());
    //"Normal" text for tab index 0. If I use this, then the setTitleAt works
    // properly. To try this, make sure the previous addTab() is commented out
    // tabs.addTab("Rate 1:5.7%", new JPanel());
    //html for tab index 1
    tabs.addTab("<html><center><font face=\"Dialog\" size = -1><b>Rate 2: <font color=#008040>9.2%</b></font></font></center></html>", new JPanel());
    getContentPane().add(tabs);
    //Uncomment next line and run again using the html style addTab() for index 0.
    // I would think the tab at index 0 would show this new text
    // In fact, it will if I do not use html for index 1.
    tabs.setTitleAt(0, "Rate it again");
    setSize( 400, 220 );
    public static void main(String[] args) {
    BugExample frame = new BugExample();
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    System.exit(0);
    System.err.println("Starting");
    frame.setVisible(true);

  • How to save JTabbedPane to file so it can be restored later

    I am trying to build a program that will allow the user to add/remove tabs and buttons in a JTabbedPane and then save this to a file so that it can be restored later. However when I try to restore it from the file I cannot get it to update the panel.
    public class LightCommander extends JFrame
         private Container windowContent;
         private JPanel mainPanel;
         // menubar items and actionlistener
         private JMenuBar mainMenu;
         private JMenu f,v,c,c1,h;
         private ButtonGroup view_group;
         private MenuHandler mlistener;
         private static JMenuItem f1,f2,f3,c11,c12,c2,c3,c4,c5,h1,h2,h3;
         private JRadioButtonMenuItem v1;
         // mainWindow items
         private JScrollPane scrollWindow;
         private TabbedView mainWindow;
         // infoBar items and mouselistener
         private JPanel infoBar;
         private JLabel info;
         private InfoHandler ilistener;
         // program variables
         private Color defaultColor;
         private File setupFile;
         private FileInputStream fis;
         private FileOutputStream fos;
         private ObjectInputStream ois;
         private ObjectOutputStream oos;
         public LightCommander()
              super("LightCommander2 - \"The Light Project\"");
              windowContent = getContentPane();
              setupFile = null;
              defaultColor = this.getBackground();
              // create the mainPanel of the program which will
              // contain all the programs gui elements
              mainPanel = new JPanel(new BorderLayout());
              mainPanel.setPreferredSize(new Dimension(800,600));
              mainPanel.setMaximumSize(new Dimension(1400,1050));
                   // create the menuBar and it's menus
                   mainMenu = new JMenuBar();
                   // create the file menu
                   f  = new JMenu("File");
                   f.setMnemonic(KeyEvent.VK_F);
                   f1 = new JMenuItem("New");
                   f1.setMnemonic(KeyEvent.VK_N);
                   f1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + n) for power users
                   f2 = new JMenuItem("Open");
                   f2.setMnemonic(KeyEvent.VK_O);
                   f2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + o) for power users
                   f3 = new JMenuItem("Exit");
                   f3.setMnemonic(KeyEvent.VK_X);
                   f3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + x) for power users
                   f.add(f1);
                   f.add(f2);
                   f.addSeparator();
                   f.add(f3);
                   // create the view menu
                   v  = new JMenu("View");
                   v.setMnemonic(KeyEvent.VK_V);
                   v1 = new JRadioButtonMenuItem("Tabs");
                   view_group = new ButtonGroup();
                   view_group.add(v1);
                   v1.setSelected(true);
                   v.add(v1);
                   // create the command menu
                   c   = new JMenu("Command");
                   c.setMnemonic(KeyEvent.VK_C);
                   c1  = new JMenu("Add");
                   c1.setMnemonic(KeyEvent.VK_A);
                   c11 = new JMenuItem("Light");
                   c11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 1) for power users
                   c12 = new JMenuItem("Switch");
                   c12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 2) for power users
                   c1.add(c11);
                   c1.add(c12);
                   c2  = new JMenuItem("Remove");
                   c2.setMnemonic(KeyEvent.VK_R);
                   c2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 3) for power users
                   c3  = new JMenuItem("Reset");
                   c3.setMnemonic(KeyEvent.VK_E);
                   c3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 4) for power users
                   c4  = new JMenuItem("All On");
                   c4.setMnemonic(KeyEvent.VK_N);
                   c4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_5, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 5) for power users
                   c5  = new JMenuItem("All Off");
                   c5.setMnemonic(KeyEvent.VK_F);
                   c5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_6, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + 6) for power users
                   c.add(c1);
                   c.add(c2);
                   c.add(c3);
                   c.addSeparator();
                   c.add(c4);
                   c.add(c5);
                   // create the help menu
                   h  = new JMenu("Help");
                   h.setMnemonic(KeyEvent.VK_H);
                   h1 = new JMenuItem("Help Topics");
                   h1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.CTRL_MASK));     // set shortcut = (Ctrl + F1) for power users
                   h2 = new JMenuItem("Technical Support");
                   h3 = new JMenuItem("About LightCommander2...");
                   h.add(h1);
                   h.add(h2);
                   h.addSeparator();
                   h.add(h3);
                   // add actionlistener and infolistener to the menuitems
                   mlistener = new MenuHandler();
                   ilistener = new InfoHandler();
                   f.addMouseListener(ilistener);
                   f1.addActionListener(mlistener);
                   f1.addMouseListener(ilistener);
                   f2.addActionListener(mlistener);
                   f2.addMouseListener(ilistener);
                   f3.addActionListener(mlistener);
                   f3.addMouseListener(ilistener);
                   v.addMouseListener(ilistener);
                   v1.addActionListener(mlistener);
                   v1.addMouseListener(ilistener);
                   c.addMouseListener(ilistener);
                   c11.addActionListener(mlistener);
                   c11.addMouseListener(ilistener);
                   c12.addActionListener(mlistener);
                   c12.addMouseListener(ilistener);
                   c2.addActionListener(mlistener);
                   c2.addMouseListener(ilistener);
                   c3.addActionListener(mlistener);
                   c3.addMouseListener(ilistener);
                   c4.addActionListener(mlistener);
                   c4.addMouseListener(ilistener);
                   c5.addActionListener(mlistener);
                   c5.addMouseListener(ilistener);
                   h.addMouseListener(ilistener);
                   h1.addActionListener(mlistener);
                   h1.addMouseListener(ilistener);
                   h2.addActionListener(mlistener);
                   h2.addMouseListener(ilistener);
                   h3.addActionListener(mlistener);
                   h3.addMouseListener(ilistener);
                   // disable menu items with no functionality as of yet
    //               f1.setEnabled(false);
    //               f2.setEnabled(false);
    //               v1.setEnabled(false);
                   c1.setEnabled(false);
                   c11.setEnabled(false);
                   c12.setEnabled(false);
                   c2.setEnabled(false);
                   c3.setEnabled(false);
                   c4.setEnabled(false);
                   c5.setEnabled(false);
                   h1.setEnabled(false);
                   h2.setEnabled(false);
                   h3.setEnabled(false);
                   // add the menus to the menubar
                   mainMenu.add(f);
                   mainMenu.add(v);
                   mainMenu.add(c);
                   mainMenu.add(h);
                   // create the mainWindow for displaying
                   // switch and light elements
                   mainWindow = new TabbedView();
                   scrollWindow = new JScrollPane(mainWindow);
                   // create the infoBar to display
                   // the mouseOver information of components
                   infoBar = new JPanel(new BorderLayout());
                   infoBar.setPreferredSize(new Dimension(800,20));
                   info = new JLabel("");
                   info.setPreferredSize(new Dimension(400,20));
                   info.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.GRAY));
                   infoBar.add(info,BorderLayout.WEST);
              // add the items to the mainPanel
              mainPanel.add(mainMenu,BorderLayout.NORTH);
              mainPanel.add(scrollWindow,BorderLayout.CENTER);
              mainPanel.add(infoBar,BorderLayout.SOUTH);
              windowContent.add(mainPanel);
              pack();
              show();
         public static void main(String[] args)
              LightCommander GUI = new LightCommander();
              GUI.addWindowListener(
                        new WindowAdapter()
                             public void windowClosing(WindowEvent e)
                             { f3.doClick(); }
         // inner class that handles all Events
         // created by the menu items in the JFrame
         public class MenuHandler implements ActionListener
              public void actionPerformed(ActionEvent e)
                   if(e.getSource() == f1)
                   //* New File:                                                                                *
                   //*      updates any currently open file, creates a fileBrowser for user          *
                   //*      input, and then creates the file and enables additional menu items     *
                        //*** check to see if a file is currently open and if so save it
                        if(setupFile != null)
                             try
                                  fos = new FileOutputStream(setupFile);
                                  oos = new ObjectOutputStream(fos);
                                  oos.writeObject(mainWindow);
                                  oos.close();
                                  fos.close();
                                  setupFile = null;
                             catch(Exception f) { JOptionPane.showMessageDialog(mainPanel,"ERROR: " + f); }
                        //*** create popup window to request file name and location (fileBrowser)
                        JFileChooser chooser = new JFileChooser();
                        int result = chooser.showSaveDialog(mainMenu);
                        if(result == JFileChooser.CANCEL_OPTION) return;
                        setupFile = chooser.getSelectedFile();
                        try
                             setupFile.createNewFile();
                             mainWindow = new TabbedView();     // reset the view here
                             mainWindow.repaint();
                             // enable the menu items that should now be functional
                             c1.setEnabled(true);
                             c12.setEnabled(true);
                        catch(Exception f){ JOptionPane.showMessageDialog(mainPanel, "ERROR: " + f); }
                   if(e.getSource() == f2)
                   //* Open File:                                                                                     *
                   //*          updates any currently open file, creates a fileBrowser for user               *
                   //*          input, opens the file, builds the view from file, enables menu items     *
                        //*** check to see if a current file is open and if so save it
                        if(setupFile != null)
                             try
                                  fos = new FileOutputStream(setupFile);
                                  oos = new ObjectOutputStream(fos);
                                  oos.writeObject(mainWindow);
                                  oos.close();
                                  fos.close();
                                  setupFile = null;
                             catch(Exception f) { JOptionPane.showMessageDialog(mainPanel,"ERROR: " + f); }
                        //*** create popup window (file browser) to allow user to select the file to be opened
                        JFileChooser chooser = new JFileChooser();
                        int result = chooser.showOpenDialog(mainMenu);
                        if(result == JFileChooser.CANCEL_OPTION) return;
                        try
                             //*** open the selected file and fill the view
                             setupFile = chooser.getSelectedFile();
                             fis = new FileInputStream(setupFile);
                             ois = new ObjectInputStream(fis);
                             mainWindow = (TabbedView) ois.readObject();
                             mainWindow.reset();
                             ois.close();
                             fis.close();
                             // finally, enable the menu items that should now be functional
                             c1.setEnabled(true);
                             c11.setEnabled(true);
                             c12.setEnabled(true);
    //                         c2.setEnabled(true);
    //                         c3.setEnabled(true);
    //                         c4.setEnabled(true);
    //                         c5.setEnabled(true);
                        catch(Exception f){ JOptionPane.showMessageDialog(mainPanel,"ERROR: " + f); }
                   if(e.getSource() == f3)
                   //* Exit Program:                                        *
                   //*          update the currently open file and exit     *
                        //*** check to see if a current file is open and if so save it
                        if(setupFile != null)
                             try
                                  fos = new FileOutputStream(setupFile);
                                  oos = new ObjectOutputStream(fos);
                                  oos.writeObject(mainWindow);
                                  oos.close();
                                  fos.close();
                                  setupFile = null;
                             catch(Exception f) { JOptionPane.showMessageDialog(mainPanel,"ERROR: " + f); }
                        System.exit(0);
                   if(e.getSource() == c11)
                   // add a lightObject to the selected switch
                   // and also add the lightObject to each view
                        //*** get the selected switchObject
                             // if the current view is tabbed find the selected tab
                             // if the current view is matrix then find the selected button
                        //*** add a lightObject to the selected switchObject
                        //*** finally add the lightObject as a button to both views
                   if(e.getSource() == c12)
                   // add a switchObject to the current setup
                   // and also add a tab to the mainWindow
                        if(!mainWindow.addSwitch())     JOptionPane.showMessageDialog(mainPanel,"ERROR: MAX number of switches are installed.");
                        // enable menu items that should now be functional
                        c11.setEnabled(true);
    //                    c2.setEnabled(true);
    //                    c3.setEnabled(true);
    //                    c4.setEnabled(true);
    //                    c5.setEnabled(true);
                   if(e.getSource() == c2){ System.out.println("event - remove"); }
                   if(e.getSource() == c3){ System.out.println("event - reset"); }
                   if(e.getSource() == c4){ System.out.println("event - all on"); }
                   if(e.getSource() == c5){ System.out.println("event - all off"); }
                   if(e.getSource() == h1){ System.out.println("event - help topics"); }
                   if(e.getSource() == h2){ System.out.println("event - technical support"); }
                   if(e.getSource() == h3){ System.out.println("event - about lightcommander2..."); }
         // displays mouseover information in the infoPanel
         public class InfoHandler implements MouseListener
              // the mouse has entered a mouselistener object
              public void mouseEntered(MouseEvent e)
                   if(e.getSource() == f ) { info.setText(" file menu");     }
                   if(e.getSource() == f1) { info.setText(" create a new setup file"); }
                   if(e.getSource() == f2) { info.setText(" open an existing setup file"); }
                   if(e.getSource() == f3) { info.setText(" exit the program");     }
                   if(e.getSource() == v ) { info.setText(" view menu");     }
                   if(e.getSource() == v1) { info.setText(" change the main window layout to tabular layout"); }
                   if(e.getSource() == c ) { info.setText(" command menu"); }
                   if(e.getSource() == c11){ info.setText(" add a light to the current lighting setup"); }
                   if(e.getSource() == c12){ info.setText(" add a switch to the current lighting setup"); }
                   if(e.getSource() == c2) { info.setText(" remove the selected item from the lighting setup");     }
                   if(e.getSource() == c3) { info.setText(" reset the selected item in the lighting setup"); }
                   if(e.getSource() == c4) { info.setText(" turn on all the lights"); }
                   if(e.getSource() == c5) { info.setText(" turn off all the lights"); }
                   if(e.getSource() == h ) { info.setText(" help menu");     }
                   if(e.getSource() == h1) { info.setText(" open a list of common help topics"); }
                   if(e.getSource() == h2) { info.setText(" open a window with company contact information"); }
                   if(e.getSource() == h3) { info.setText(" open a window displaying program info"); }
              public void mouseExited(MouseEvent e){ info.setText(""); }
              public void mousePressed(MouseEvent e){  }
              public void mouseReleased(MouseEvent e){  }
              public void mouseClicked(MouseEvent e){  }
    public class TabbedView extends JTabbedPane implements Serializable
         private SwitchObject[] switches;
         private int numSwitches;
         public TabbedView()
              super();
              switches = new SwitchObject[128];
              numSwitches = 0;
         public boolean addSwitch()
              if(numSwitches < 128)
                   for(int i=0; i<128; i++)
                        if(switches[i] == null)
                             SwitchObject x = new SwitchObject("S" + Integer.toString(i), i);
                             switches[i] = x;
                             this.addTab(x.getLabel(), x);
                             break;
                   numSwitches++;     // increment the number of switches installed
                   return true;     // signal a successful operation
              else return false;     // signal that all available switch addresses are used
    public class SwitchObject extends JPanel implements Serializable
         private String s_label;
         private int s_addr;
         private int numLights;
         private LightObject[] lights;
         // SwitchObject Constructor
         public SwitchObject(String label, int addr)
              super();
              s_label = label;
              s_addr = addr;
              numLights = 0;
              lights = new LightObject[128];     // create an empty array of lights
              for(int i=0; i<3; i++) Tx();     // send the switches addr 3 times in order to setup the hardware addr
         public String getLabel(){ return s_label; }
         public int getAddr(){ return s_addr; }
         // add a light to the switch
         public boolean addLight()
              if(numLights < 128)
                   for(int i=0; i<128; i++)
                        if(lights[i] == null)
                             lights[i] = new LightObject("L" + Integer.toString(i), i+128);
                             break;
                   numLights++;
                   return true;
              else return false;
         // transmit the switches addr to the hardware
         public void Tx()
              //*** Add Code Here ***
              System.out.println("Tx: " + Integer.toString(s_addr));     // output for debugging
    public class LightObject extends JButton implements Serializable,ActionListener
         private int l_addr;
         private boolean isOn;
         public LightObject(String label, int addr)
              super(label);
              this.addActionListener(this);
              this.setBackground(Color.YELLOW);
              l_addr = addr;
              for(int i=0; i<3; i++) Tx();          // send the light addr 3 times to setup the hardware addr
              isOn = true;
         public int getAddr(){ return l_addr; }
         // Transmit the light's addr to the hardware
         public void Tx()
              //*** Add Code Here ***
              System.out.println("Tx: " + Integer.toString(l_addr));
         public void actionPerformed(ActionEvent e)
              Tx();
              if(isOn)
                   isOn = false;
                   this.setBackground(Color.GRAY);
              else
                   isOn = true;
                   this.setBackground(Color.YELLOW);
    }

    You're welcome for the help I gave you in your last posting on this topic.
    Why are you posting 200 lines of code? 90% of the code is not related to saving/restoring a component. Create a simple demo program and then maybe someone will take a look. We are not here to debug you entire application.
    How to create a [url http://www.physci.org/codes/sscce.jsp]Short, Self Contained, Correct (Compilable), Example

  • JTabbedPane background color doesn't work in Win XP - SP 1 with JRE 1.4.2

    Setting background color for JTabbedPane doesn't
    work in "Windows XP with SP 1" when using JRE 1.4.2.
    For example, following call doesn't set the
    tab to green:
    tabbedPane.setBackgroundAt(0, Color.green);
    It always displays TABS with the white background.
    Is anybody aware of this problem?
    Thanks-in-advance for any help / suggestions!
    Sundar

    Thanks, bbritta and vairavan.
    I tried setOpaque also, but still doesn't work,
    On my system also:
    UIManager.getSystemLookAndFeelClassName() = com.sun.java.swing.plaf.windows.WindowsLookAndFeel
    I have given below the sample code that doesn't display tab color. Please let me know if you suspect something is wrong here, or any suggestions to make this work under Windows XP - Service Pack 1.
    Thanks,
    Sundar
    // CODE STARTS HERE
    import javax.swing.JTabbedPane;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import java.awt.*;
    import java.awt.event.*;
    public class TEMP_TABBED_PANE extends JPanel {
    public TEMP_TABBED_PANE() {
    JTabbedPane tabbedPane = new JTabbedPane();
         tabbedPane.setOpaque(true);
    Component panel1 = makeTextPanel("Howdy");
    tabbedPane.addTab("One", panel1);
    tabbedPane.setSelectedIndex(0);
    tabbedPane.setBackgroundAt(0, Color.green);
    Component panel2 = makeTextPanel("Howdy Howdy");
    tabbedPane.addTab("Two", panel2);
    tabbedPane.setBackgroundAt(1, Color.yellow);
    Component panel3 = makeTextPanel("Howdy Howdy Howdy");
    tabbedPane.addTab("Three", panel3);
    tabbedPane.setBackgroundAt(2, Color.red);
    Component panel4 = makeTextPanel("Howdy Howdy Howdy Howdy");
    tabbedPane.addTab("Four", panel4);
    tabbedPane.setBackgroundAt(3, Color.cyan);
    //Add the tabbed pane to this panel.
    setLayout(new GridLayout(1, 1));
    add(tabbedPane);
    protected Component makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    //panel.setOpaque(true); <<-- UNCOMMENTING THIS ALSO DOESN'T HELP :(
    return panel;
    public static void main(String[] args) {
    JFrame frame = new JFrame("Tabbed Pane Demo for Color");
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    JOptionPane.showMessageDialog(frame,
              "UIManager.getSystemLookAndFeelClassName() = " +
              UIManager.getSystemLookAndFeelClassName(),
              "Look And Feel",
              JOptionPane.INFORMATION_MESSAGE);
    catch(Exception e) {
    e.printStackTrace();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new TEMP_TABBED_PANE(),
    BorderLayout.CENTER);
    frame.setSize(400, 125);
    frame.setVisible(true);
    // CODE ENDS HERE

  • Custom tabs for JTabbedPane?

    Is there no way to customize the contents of a tab for a JTabbedPane? I mean, sure, icon and label are nice. But say I want a "X" button on it to close the tab. Is there no way to do something like that?
    It would seem like a useful thing to have a JTab component that is a container which displays the tab itself. The default contents could be a JLabel for the icon/label, but then if I wanted to add a button, I could do so.
    Maybe I'm missing something, but I don't think so. I've searched around and don't see any way (I'm not even sure that a customized TabbedPaneUI would even work here)...

    Check this out: http://www.infonode.net/index.html?itp
    This JTabbedPane alternative is designed just as you described: each tab is a separate component, containing the label, icon, etc., plus the "content" component. Adding a button that removes the tab is trivial, as shown below. Another feature I'm particularly fond of is the "compression" layout policy, which squeezes the tabs to fit the available width. I much prefer that scheme to scrolling tabs, and they've implemented it very nicely.
    The only quibble I have with it is the way it handles keyboard focus. Either I'm missing something, or it just doesn't work sensibly. The sample code includes a workaround that forces the selected content component to always have the focus.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import net.infonode.tabbedpanel.*;
    import net.infonode.tabbedpanel.titledtab.*;
    public class TabbedPanelDemo
      public static void main(String[] args)
        // Reduce padding around tabs, especially to the right of the
        // the "remove" button.  If there's a way to do this through
        // the TabbedPanel API, I haven't found it.
        UIManager.put("TabbedPane.tabInsets", new Insets(0, 5, 1, 1));
        JFrame frame = new JFrame("TabbedPanelDemo");
        frame.addWindowListener(
          new WindowAdapter()
            public void windowClosing(WindowEvent e)
              System.exit(0);
        final TabbedPanel tp = new TabbedPanel();
        tp.getProperties().setTabLayoutPolicy(TabLayoutPolicy.COMPRESSION);
        tp.addTab(makeTab("First Panel",  "Panel 1"));
        tp.addTab(makeTab("Second Panel", "Panel 2"));
        tp.addTab(makeTab("Third Panel",  "Panel 3"));
        tp.addTab(makeTab("Fourth Panel", "Panel 4"));
        tp.addTab(makeTab("Fifth Panel",  "Panel 5"));
        // Keep the focus in the selected textarea.  If you comment out
        // this statement, the focus will shift to the the Close button
        // every time you select a different tab.
        tp.addTabListener(
          new TabAdapter()
            public void tabSelected(TabStateChangedEvent evt)
              tp.getSelectedTab().getContentComponent().requestFocus();
            public void tabRemoved(TabRemovedEvent evt)
              tp.getSelectedTab().getContentComponent().requestFocus();
        frame.add(tp, BorderLayout.CENTER);
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        JButton btn = new JButton("Close");
        btn.addActionListener(
          new ActionListener()
            public void actionPerformed(ActionEvent evt)
              System.exit(0);
        box.add(btn);
        frame.add(box, BorderLayout.SOUTH);
        frame.setSize(400, 150);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      private static Icon removeIcon = UIManager.getIcon(
          "InternalFrame.paletteCloseIcon");
      private static Insets zeroInsets = new Insets(0,0,0,0);
      static Tab makeTab(String label, String text)
        JTextArea content = new JTextArea(text);
        final TitledTab tab = new TitledTab(label, null, content, null);
        // This is all you have to do to add a "remove" button to a tab
        JButton b = new JButton(removeIcon);
        b.setMargin(zeroInsets);
        b.addActionListener(
          new ActionListener()
            public void actionPerformed(ActionEvent evt)
              tab.getTabbedPanel().removeTab(tab);
        b.setFocusable(false);
        tab.setTitleComponent(b);
        return tab;
    }

  • JTabbedPane SelectedTab Color

    I have this problem: I would like to change the Default Tab Background Color in a JTabbedPane (not the setBackgroundAt method!!!).
    Thanks to all

    The key is:
    UIManager.put("TabbedPane.selected", Color.green);
    .. as this example (found in www.esus.com/javaindex/j2se/jdk1.2/javaxswing/generalpurposecontainers/jtabbedpane/jtabbgcolor.html)
    shows:
    import javax.swing.plaf.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class Main extends JPanel {
    public Main() {
    setLayout(new BorderLayout());
    UIManager.put("TabbedPane.selected", Color.green);
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i=0;i<10;i++) {
    tabbedPane.addTab("Tab #" + i, new JLabel("Tab #" + i));
    tabbedPane.setBackgroundAt(i, new Color(25*i, 25*i, 25*i));
    add(tabbedPane, BorderLayout.CENTER);
    JPanel createPane(String s) {
    JPanel p = new JPanel();
    p.add(new JLabel(s));
    return p;
    public static void main(String[] args) {
    JFrame frame = new JFrame("JTabbedPane Selected Color Demonstration");
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    System.exit(0);
    frame.getContentPane().add(new Main());
    frame.setSize(200, 100);
    frame.setVisible(true);
    bye!

  • MouseListener in JTabbedPane jdk1.4

    Hi guys,
    I am trying to use JTabbedPane with jkd1.4. my problem is I am not able to detect mouse click if i use new feature of JTabbedPane.SCROLL_TAB_LAYOUT. because I thinkg it is already used for up and arrow. If i comment out that line form the following code mouse click works fine. Any way to have both the feature on .i.e. mouseclick detection and scrollable tabs.
    Thanks for help.
    import javax.swing.table.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import javax.swing.text.*;
    public class TabbedPaneDemo extends JPanel implements MouseListener {
    public TabbedPaneDemo() {
    ImageIcon icon = new ImageIcon("images/middle.gif");
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    tabbedPane.setTabPlacement(SwingConstants.LEFT);
    for(int i =0;i< 50;i++){
    tabbedPane.addTab( "panel " + i, icon, makeTextPanel("Blah blah blah " + i), "Still does nothing");
    // System.out.println(i);
         tabbedPane.addMouseListener(this);
    //Add the tabbed pane to this panel.
    setLayout(new GridLayout(1, 1));
    add(tabbedPane);
    public void mouseClicked(MouseEvent e){
         System.out.println("HI I AM IN MOUSE EVENT");
    public void mouseEntered(MouseEvent e){};
    public void mouseExited(MouseEvent e){};
    public void mousePressed(MouseEvent e){};
    public void mouseReleased(MouseEvent e){};
    protected Component makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    return panel;
    public static void main(String[] args) {
    JFrame frame = new JFrame("TabbedPaneDemo");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new TabbedPaneDemo(),
    BorderLayout.CENTER);
    frame.setSize(800, 400);
    frame.setVisible(true);

    This is a known bug which is not going to be fixed until the 1.5 release:
    http://developer.java.sun.com/developer/bugParade/bugs/4465870.html

  • JTabbedPane content inserted at selection time

    Hi,
    I have a jtabbedpane with a few tabs. I want to insert the content of each tab at selection time. That means by default the content of the first tab is inserted. When I select the next tab it should retreive data from a DB and update this selected tab. But I can't do it. In my current implementation I load all data in all tabs at start time. But since the amount of data is large it takes a lot of time till the tabbedpane is visible.
    Is there anyone who can help me with this.
    Thanks in advance
    grkh

    Hi,
    add ChangeListener on the JTabbedPane and listen for stateChanged event. look at the following example:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class TestFrame extends JFrame
      JTabbedPane jTabbedPane1 = new JTabbedPane();
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
      public TestFrame()
        try
          jbInit();
        catch(Exception e)
          e.printStackTrace();
      public static void main(String[] args)
        (new TestFrame()).show();
      private void jbInit() throws Exception
        this.setTitle("Test Frame");
        this.addWindowListener(new java.awt.event.WindowAdapter()
          public void windowClosing(WindowEvent e)
            this_windowClosing(e);
        this.setSize(450, 350);
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener()
          public void stateChanged(ChangeEvent e)
            jTabbedPane1_stateChanged(e);
        this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        jTabbedPane1.add(jPanel1, "jPanel1");
        jTabbedPane1.add(jPanel2, "jPanel2");
      void this_windowClosing(WindowEvent e)
        dispose();
        System.exit(0);
      void jTabbedPane1_stateChanged(ChangeEvent e)
        System.out.println("Tab Index: " + jTabbedPane1.getSelectedIndex());
    }

  • JTabbedPane single event handling class.

    I have a list of proxyNames which are stored in a Vector.
    These proxy names are then displayed as Tabs accordingly.
    For every tab there is a specific action to be performed.
    I want to write a single event handling class for handling all events.
    What I have is this:
    while(eNum.hasMoreElements()){
    Object proxyName = eNum.nextElement();
    tabbedPane.addTab(proxyName.toString(), null, null, "Proxy");
    tabbedPane.addChangeListener(itemHandler);
    panel4.add(tabbedPane2);
    class ItemHandler implements ChangeListener{
    public void stateChanged(ChangeEvent e){
    for(int i=0; i < v.size(); i++){
    // Perform action on choosing the concerned tab...
    How do I have a single event listener?

    Yikes! Is that a minimal program? When I am trying to do something new, or facing a problem that causes me to get stuck for more than an hour, I create a short program to solve just that problem. In time, you create a directory of test programs that's useful, and with practice, solving a problem in isolation is a faster (and generally better) way to go about things that doing it all in a larger application.
    Any way, here is your code, with a change listener added in method makeSubpanel. If you don't want the listener called the very first time, add it at the end of this method. I also fixed some bugs in createProxyList.
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TrialTabbed extends JPanel {
        // Will hold all file names read from the /config directory.
        private Vector v,v1;
        JTabbedPane tabbedPane,tabbedPane2;
        Object proxyName;
        public TrialTabbed(){
            tabbedPane = new JTabbedPane();
            // Instantiate the Vector.
            v1 = new Vector();
            Component panel1 = makeTextPanel("Blah");
            tabbedPane.addTab("Debug Mode", null, panel1, "Debug");
            Component panel2 = makeSubPanel();
            tabbedPane.addTab("Normal Mode", null, panel2, "Normal");
            // Add the tabbed pane to this panel.
            setLayout(new GridLayout(1, 1));
            add(tabbedPane);
        protected Component makeTextPanel(String text) {
            JPanel panel = new JPanel(false);
            JLabel filler = new JLabel(text);
            filler.setHorizontalAlignment(JLabel.CENTER);
            panel.setLayout(new GridLayout(1, 1));
            panel.add(filler);
            return panel;
        private Component makeSubPanel(){
            JPanel panel4 = new JPanel(false);
            panel4.setLayout(new GridLayout(1, 1));
            tabbedPane2 = new JTabbedPane();
            //new code - a change listener for pane2
            tabbedPane2.addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e){
                    int tab = tabbedPane2.getSelectedIndex();
                    if (tab == -1)
                        System.out.println("no tab selected");
                    else {
                        String filename = tabbedPane2.getTitleAt(tab);
                        File file = new File("..", filename); //brittle!
                        if (file.isDirectory())  {
                            String[] contents = file.list();
                            int size = contents == null ? 0 : contents.length;
                            System.out.println(filename + " contains " + size + " files");
                        } else
                            System.out.println(filename + " has length " + file.length());
            //end of new code
            ItemHandler itemHandler = new ItemHandler();
            v = createProxyList();
            // Enumerate thru the Vector and put them as tab names.
            Enumeration eNum = v.elements();
            while(eNum.hasMoreElements()){
                proxyName = eNum.nextElement();
                tabbedPane2.addTab(proxyName.toString(), null, null, "Proxy");
                panel4.add(tabbedPane2);
            return panel4;
        // Display the file names in this directory as tab Names.
        //new code: changed dirName to "..", fixed some obvious bugd
        private Vector createProxyList(){
            String dirName = "..";
            File file = new File(dirName);
            if(file.isDirectory()){
                String[] s = file.list();
                for(int i=0; i< s.length; i++){
                    v1.addElement(s);
    } // End for.
    } // End of if.
    return v1;
    } // End createProxyList().
    class ItemHandler implements ChangeListener{
    public void stateChanged(ChangeEvent e){
    System.out.println(v.size());
    System.out.println("IN CHANGE LISETENER" + proxyName.toString());
    } // End actionPerformed.
    } // End Inner class ItemHandler.
    public static void main(String[] args) {
    JFrame frame = new JFrame("TabbedPaneDemo");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new TrialTabbed(),BorderLayout.CENTER);
    frame.setSize(500, 425);
    frame.setVisible(true);

  • Changed the background of one of my tab of a JTabbedPane

    Hi all.
    I have a JTabbedPane with 3 tabs ('test1', 'test2', 'test 3').
    I would like to change the background of 'test1' pane (not the tab) by drawing an image.
    I would also like to add in 'test1' JInternlFrame (what I have done).
    My problem is that I can not do the two things simultaneously. When I want to repaint the 'test1' pane, it's added a new tab.
    I would like both the image (the 'test1' pane background) and the JInternalFrame to be in a same tab.
    'test1', 'test2', 'test3' don't have the same component.
    If someone can help me please, it would be great..

    Thanks all.
    I have found the solution.
    Repaint the JDesktopPane using paintComponent will do the job.
    Here is the code if someone is interested in.import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class DesktopPane extends JDesktopPane{
         private static final long serialVersionUID = 1L;
         public static JTabbedPane desktopTab = new JTabbedPane();
         public static DesktopPane desktopPane=null;
         static JInternalFrame internal = new JInternalFrame("Test++",true,true,true,true);
         ImageIcon m_tabimage = new ImageIcon("img/logo.png");
         public DesktopPane(JInternalFrame internalFrame,String title){
              super();
              add(internalFrame);
              desktopPane = this;
              this.add(internal);
              desktopTab.addTab(title,this);
              setOpaque(true);
         public JTabbedPane getDesktopTab(){
              return desktopTab;
         public void addInternalFrame(DesktopPane desktop, String title) {
              desktopTab.add(desktop);
         public void paintComponent(Graphics g) {
              super.paintComponent(g);
              Graphics2D g2D =(Graphics2D)g;
              Image mIcon = m_tabimage.getImage();
              int aX = getWidth()/2  - mIcon.getWidth(null)/2;
              int aY = getHeight()/2 - mIcon.getHeight(null)/2;
              g2D.fill(new Rectangle(aX-2,aY-2,mIcon.getWidth(null)+2,
                             mIcon.getHeight(null)+2)); // iconWidth
                        Rectangle rect = new Rectangle(aX-2,aY-2,mIcon.getWidth(null)+2, mIcon.getHeight(null)+2);
              g2D.draw(rect);
              rect = new Rectangle(aX-1,aY-1, mIcon.getWidth(null)+1,
                        mIcon.getHeight(null)+1);
              g2D.draw(rect);
              g2D.drawImage(mIcon,aX,aY,null);     
         public static void main(String[] args){
              JInternalFrame internalFrame = new JInternalFrame("Test",true,true,true,true);
              internalFrame.setSize(new Dimension(300,300));
              internalFrame.setVisible(true);
              internalFrame.setLocation(new Point(20,20));
              internalFrame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
              //desktopTab.addTab("Test", new JPanel());
              internal.setSize(new Dimension(300,300));
              internal.setVisible(true);
              internal.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
              //desktopTab.addTab("Desktop", internal);
              DesktopPane pane = new DesktopPane(internalFrame,"Desktop");
              //pane.addInternalFrame(new DesktopPane(internal, "Desktop"),"Desktop");
              JFrame frm = new JFrame("Test");
    //          Changelistener for tabbedpane
              desktopTab.addChangeListener(new DesktopTabChangeListener());
              JButton newButton = new JButton();
              newButton.addActionListener(new NewButtonListener());
              frm.getContentPane().setLayout(new BorderLayout());
              frm.getContentPane().add(pane.getDesktopTab(),BorderLayout.CENTER);
              frm.getContentPane().add(newButton,BorderLayout.SOUTH);
              frm.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);
              frm.pack();
              frm.setVisible(true);
         }

  • Mnemonics not diabled on disabled tab of JTabbedPane

    Does anybody know a simple way around this? I have a JTabbedPane with a disabled tab that has a mneumonic key on it. Clicking the mneumonic (ALT-T in my example) goes to the disabled tab, which defeats the purpose of having it disabled (clicking the tab with the mouse works as expected). This is obviously an overlooked bug. Running 1.4_02. Thanks in advance.
    Sample code:
    import java.awt.event.KeyEvent;
    import java.awt.BorderLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JTabbedPane;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    public class TabPaneExample extends JFrame
        /** Creates a new instance of TabPaneExample */
        public TabPaneExample()
            super("TabPaneExample");
            JTabbedPane tabpane = new JTabbedPane();
            tabpane.addTab("One", new JButton("Panel One"));
            tabpane.addTab("Two", new JButton("Panel Two"));
            tabpane.setMnemonicAt(0, KeyEvent.VK_O);
            tabpane.setMnemonicAt(1, KeyEvent.VK_T);
            tabpane.setEnabledAt(1, false);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(tabpane, BorderLayout.CENTER);
            getContentPane().add(panel, BorderLayout.CENTER);
        public static void main (String args[])
            TabPaneExample frame = new TabPaneExample();
            frame.addWindowListener(new WindowAdapter()
                public void windowClosing(WindowEvent e)
                    System.exit(0);     
            frame.setSize(280, 230);  
            frame.setVisible(true);
    }

    http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2Bjtabbedpane+%2Bmnemonic+%2Bdisabled&col=javabugs&x=36&y=9

  • JTabbedPane focus question with buttons

    Hi. I have a JTabbedPane with an OK button on each pane. clicking the OK button will remove that pane from the JTabbedPane. But if I don't move the mouse, and click the OK button to remove the next pane, I have to click TWICE, OR move the mouse just a little bit before clicking, then it will go away. Here's the code. How can I make this work so one click will do it? Thanks...
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TabbedFocusTest extends JPanel implements ActionListener {
    private JTabbedPane tabbedPane;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    public TabbedFocusTest() {
    ImageIcon icon = new ImageIcon("images/middle.gif");
    tabbedPane = new JTabbedPane();
         panel1 = new JPanel();
    Component text1 = makeTextPanel("Blah");
         JButton button1 = new JButton("OK");
         button1.setActionCommand("1");
         panel1.setLayout(new BorderLayout());
         panel1.add(text1, BorderLayout.NORTH);
         panel1.add(button1, BorderLayout.SOUTH);
    tabbedPane.addTab("One", icon, panel1, "Does nothing");
    tabbedPane.setSelectedIndex(0);
         panel2 = new JPanel();
    Component text2 = makeTextPanel("Blah 2");
         JButton button2 = new JButton("OK");
         button2.setActionCommand("2");
         panel2.setLayout(new BorderLayout());
         panel2.add(text2, BorderLayout.NORTH);
         panel2.add(button2, BorderLayout.SOUTH);
    tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing");
         panel3 = new JPanel();
    Component text3 = makeTextPanel("Blah 3");
         JButton button3 = new JButton("OK");
         button3.setActionCommand("3");
         panel3.setLayout(new BorderLayout());
         panel3.add(text3, BorderLayout.NORTH);
         panel3.add(button3, BorderLayout.SOUTH);
    tabbedPane.addTab("Three", icon, panel3, "nothing 3");
         panel4 = new JPanel();
    Component text4 = makeTextPanel("Blah 4");
         JButton button4 = new JButton("OK");
         button4.setActionCommand("4");
         panel4.setLayout(new BorderLayout());
         panel4.add(text4, BorderLayout.NORTH);
         panel4.add(button4, BorderLayout.SOUTH);
    tabbedPane.addTab("Four", icon, panel4, "Nothing 4");
         button1.addActionListener(this);
         button2.addActionListener(this);
         button3.addActionListener(this);
         button4.addActionListener(this);
    //Add the tabbed pane to this panel.
    setLayout(new GridLayout(1, 1));
    add(tabbedPane);
    protected Component makeTextPanel(String text) {
    JPanel panel = new JPanel(false);
    JLabel filler = new JLabel(text);
    filler.setHorizontalAlignment(JLabel.CENTER);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(filler);
    return panel;
    public void actionPerformed(ActionEvent e) {
         System.out.println(e.getActionCommand());
         if (e.getActionCommand().equals("1"))
         tabbedPane.remove(panel1);
         if (e.getActionCommand().equals("2"))
         tabbedPane.remove(panel2);
         if (e.getActionCommand().equals("3"))
         tabbedPane.remove(panel3);
         if (e.getActionCommand().equals("4"))
         tabbedPane.remove(panel4);
    public static void main(String[] args) {
    JFrame frame = new JFrame("TabbedFocusTest");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}
    frame.getContentPane().add(new TabbedFocusTest(),
    BorderLayout.CENTER);
    frame.setSize(400, 125);
    frame.setVisible(true);

    Thanks for the suggestion - I added this line at the end of actionPerformed() but it did not help:
    tabbedPane.getSelectedComponent().requestFocus();

Maybe you are looking for

  • Macbook Air Late 2010 model, 10.6.7, 2.13 4g ram. Trackpad is not working properly and in sometimes not at all.

    Macbook Air Late 2010 model, 10.6.7, 2.13 4g ram. Trackpad is not working properly and in sometimes not at all. Need suggestions to resolve this issue?  I have applied all Mac updates and still having problems with the trackpad.

  • Error while importing a soap webservice

    I was consuming a webservice with flex 3 and it works perfectly. But when i tried to consume it using flash builder 4 when i try to connect to show me this error: Unable to save the data model Reason: An error occurred while attemping to resolve the

  • Urgent please help

    hi all, we are running two instance in two different machines A and B (oracle 9.2.0.1.0/win2000). we exported the data from A and copied to machine B.data has not been modified since 'copy'. now the problem is , the following query select count(*) fr

  • How to apply Time Remap to multiple clips simultaneously

    Quite often after trimming many different clips for a wedding video I will then want the majority of them slowed down to a 50% speed. I know how to apply the Time Remap Speed % change in the Motion Tab, but have yet to figure out how to make the chan

  • About Memory Leak in JAVA Any body pls help me

    Hi All, In our application, with JAVA we are using c, c++ native methods also. The problem is, Eventhough the application is in idle state, the Virtual Memory occupation is getting increased. For 24 hours, 30 MB is increased, while the application is