Search code examples
javaswingjframejpaneljtabbedpane

Adding new Tab with JTabbedPane through event


I have two classes here: MainScreen and QueryScreen. MainScreen has already implemented one JTabbedPane on int. QueryScreen extended the MainScreen.

I tried to add one tab calling one event through QueryScreen but its not coming up on the app. Checkout please the sample code:

QueryScreen:

 public class QueryScreen extends MainScreen {

        private JSplitPane engineList;
        final JPanel queryList = new JPanel();

        public QueryScreen(){

            tabbedPane.addTab( "Query List", queryList );
            add( tabbedPane, BorderLayout.CENTER );

        }
    }

MainScreen:

 public class MainScreen extends JFrame implements ActionListener {
        /**
         * 
         */
        JMenuBar bar;
        JMenu file, register;
        JMenuItem close, search;
        ImageIcon image1= new ImageIcon("rsc/img/logo.jpg");
        JLabel lbImage1;
        JTabbedPane tabbedPane = new JTabbedPane();
        final JPanel entrance = new JPanel();


        /**
         * 
         */

public MainScreen()
        {           
                lbImage1= new JLabel(image1, JLabel.CENTER);
            entrance.add(lbImage1);
            tabbedPane.addTab( "Entrance", entrance );
            add( tabbedPane, BorderLayout.CENTER );

            bar= new JMenuBar();
            file= new JMenu("File");
            register= new JMenu("Search");

            close= new JMenuItem("Close");
            close.addActionListener(this);

            search= new JMenuItem("Request Query");
            search.addActionListener(this);


            //Keyboard Shortcut
            register.setMnemonic(KeyEvent.VK_S);
            file.setMnemonic(KeyEvent.VK_F);
            search.setMnemonic(KeyEvent.VK_R);




            //Ibimage1.setVerticalTextPosition(SwingConstants.CENTER);

            bar.add(file);
            bar.add(register);
            file.add(close);
            register.add(search);
            setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
            setTitle("SHST");
            setJMenuBar(bar);
            setDefaultCloseOperation(0);

                WindowListener J=new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                System.exit(0);
                }
            }; 

            addWindowListener(J);
    }

        public void actionPerformed(ActionEvent e){
            if(e.getSource()==close){
                System.exit(0);
            }

            if(e.getSource()==search){
                Search s= new Search();
                s.setVisible(true);
            }

            }
    }

ps: the MainScreen object and the setVisible from it is coming from the run class which has only the call for this MainScreen.

How am I able to add this new tab?

Thanks in advance

Edit One:

enter image description here


Solution

  • In the future please post an SSCCE instead of copy/pasting some classes.

    Here's an SSCCE of your MainScreen, with the non-essentials stripped out, and a main method added:

    import java.awt.*;
    import javax.swing.*;
    
    public class MainScreen extends JFrame
    {
      JTabbedPane tabbedPane = new JTabbedPane();
      final JPanel entrance = new JPanel();
    
      public MainScreen()
      {
        tabbedPane.addTab("Entrance", entrance);
        add(tabbedPane, BorderLayout.CENTER);
    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            JFrame frame = new MainScreen();
            frame.setSize(300, 200);
            frame.setVisible(true);
          }
        });
      }
    }
    

    ... and here's an SSCCE for QueryScreen:

    import java.awt.*;
    import javax.swing.*;
    
    public class QueryScreen extends MainScreen
    {
      final JPanel queryList = new JPanel();
    
      public QueryScreen()
      {
        tabbedPane.addTab("Query List", queryList);
        //add( tabbedPane, BorderLayout.CENTER );    /* not needed */
      }
    
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            JFrame frame = new QueryScreen();
            frame.setSize(300, 200);
            frame.setVisible(true);
          }
        });
      }
    }
    

    As you can see, this works, and for the most part, all I did was remove unnecessary code and added a main to each.

    If you're still having problems, please update your question with an SSCCE and post the specific problem you're having.