Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By doWhile

Thread: How to add multiple components to a tab pane?

  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Question How to add multiple components to a tab pane?

    I'm using the following code to make a tab for my program (just as a test):

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class CreateTabbedPane{
      public static void main(String[] args){
      JFrame frame = new JFrame("Tabbed Pane Frame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JTabbedPane tab = new JTabbedPane();
      frame.add(tab, BorderLayout.CENTER);
      JButton button = new JButton("1");
      tab.add("Tab 1", button);
      button = new JButton("2");
      tab.add("Tab 2", button);
      frame.setSize(400,400);
      frame.setVisible(true);
      }
    }
    I don't quite understand how to add multiple components to the same tab. For example let's say I wanted to have a button and a textbox in tab 1 and a label and a checkbox in tab 2.

    Help is much appreciated!

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to add multiple components to a tab pane?

    Add the components to a JPanel, then add that JPanel to the JTabbedPane

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: How to add multiple components to a tab pane?

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Thumbs up Re: How to add multiple components to a tab pane?

    Ah, so simple!
    Thanks

    Here's my finished code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
     
    public class CreateTabbedPane{
      public static void main(String[] args){
      //New Frame
      JFrame frame = new JFrame("Tabbed Pane Frame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //Componenets
      JTabbedPane tab = new JTabbedPane();
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JLabel label1 = new JLabel("This is in tab 1.");
      JLabel label2 = new JLabel("This is in tab 2.");
      JButton button1 = new JButton("Tab 1 Button");
      JButton button2 = new JButton("Tab 2 Button");
      //Adding
      frame.add(tab);
      //For Tab 1
      panel1.add(label1);
      panel1.add(button1);
      //For Tab 2
      panel2.add(label2);
      panel2.add(button2);
      //Add Tabs
      tab.addTab("Tab 1", panel1);
      tab.addTab("Tab 1", panel2);
    
      //Frame Final Touches
      frame.setSize(400,400);
      frame.setVisible(true);
      }
    }

Similar Threads

  1. adding multiple jpanels to content pane
    By tooktook22 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2011, 06:40 PM
  2. Multiple components in Jpanel
    By shrutisurulkar in forum AWT / Swing
    Replies: 1
    Last Post: 01-07-2011, 02:13 PM
  3. Jtable with Multiple Components in a ROw
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 12:44 PM
  4. Trying to Draw Multiple Components
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 11:06 PM
  5. Multiple components as one node in JTree
    By Amnuriak in forum AWT / Swing
    Replies: 8
    Last Post: 11-04-2009, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •