Adding content to JPanel component of JTabbedPane :- Problem

Hi

I want to be able to the control the layout of the Jpanel I am adding as a component when using Jtabs. This will vary from tab to tab and ideally I would like to be using gridBagLayout to get the most control.

My problem is I do not know at what stage I should be setting the layout constraints and adding the content which will be text boxes, buttons, sliders etc...

Below is my code so far. Main is in abother class I have pasted it at the bottom.

Thanks in advance.



import javax.swing.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;

public class GUI
    extends JFrame {

  JMenuBar menuBar;
  JMenu menu;
  JMenuItem menuItem;


  public GUI() {
    super("Asys");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//------------------------------------------------------------------------------
//Menu Bar

    menuBar = new JMenuBar();

//File Menu
    menu = new JMenu("File");
    menu.setMnemonic('F');

    //File Menu Items
    menuItem = new JMenuItem("Print");
    menu.add(menuItem);

    menuItem = new JMenuItem("Open Flight");
    menu.add(menuItem);

    menuItem = new JMenuItem("Close Flight");
    menu.add(menuItem);

    menuBar.add(menu);
//------------------------------------------------------------------------------
//Side Tool Bar Buttons
    ImageIcon image1 = new ImageIcon("C:/Documents and Settings/Ben Attwood.GORDON/My Documents/ASys/Asys/classes/asys/load.gif");
    JButton button1 = new JButton(image1);
    ImageIcon image2 = new ImageIcon("C:/Documents and Settings/Ben Attwood.GORDON/My Documents/ASys/Asys/classes/asys/save.gif");
    JButton button2 = new JButton(image2);
    ImageIcon image3 = new ImageIcon("C:/Documents and Settings/Ben Attwood.GORDON/My Documents/ASys/Asys/classes/asys/subscribe.gif");
    JButton button3 = new JButton(image3);

//------------------------------------------------------------------------------
//Tabbed Pane
    JTabbedPane tabbedPane = new JTabbedPane();
    Component flightPanel1 = new JPanel();
 
    tabbedPane.addTab("Flight Info", image1, flightPanel1, "Flight Details");
    tabbedPane.setSelectedIndex(0);

    //Flight Tab layout
    //Don't know what to put here?

    JLabel usernameLabel = new JLabel("Username: ");
    JTextField username = new JTextField();


   



    //flightPanel1.add(button1);
    tabbedPane.add(button1);

    Component passengerPanel1 = new JPanel();
    tabbedPane.addTab("Passenger Info", image1, passengerPanel1,
                      "Passenger Details");

    Component groupPanel1 = new JPanel();
    tabbedPane.addTab("Group Info", image1, groupPanel1,
                      "Group Details");

    Component layoutPanel1 = new JPanel();
    tabbedPane.addTab("Layout Info", image1, layoutPanel1,
                      "Layout Details");

//------------------------------------------------------------------------------
//ToolBar
    JToolBar bar = new JToolBar(null, JToolBar.VERTICAL);
    bar.add(button1);
    bar.add(button2);
    bar.add(button3);

//------------------------------------------------------------------------------
//Pane Layout
    JTextArea edit = new JTextArea(18, 40);
    //JScrollPane scroll = new JScrollPane(edit);
    JPanel pane = new JPanel();
    BorderLayout bord = new BorderLayout();
    pane.setLayout(bord);
    pane.setPreferredSize(new Dimension(800, 600));
    pane.add("West", bar);
    pane.add("North", menuBar);
    //pane.add("Center", scroll);
    pane.add("Center", tabbedPane);
    setContentPane(pane);

  } //public GUI

  void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh,
                        int wx, int wy) {
    gbc.gridx = gx;
    gbc.gridy = gy;
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    gbc.weightx = wx;
    gbc.weighty = wy;
  }

} //GUI



The main is

public static void main(String args[]) {

    //Set the look and feel for the GUI by finding out what the present
    //system look and feel is like
    try {
      UIManager.setLookAndFeel(
          UIManager.getSystemLookAndFeelClassName());
    } //try
    catch (Exception e) {
      System.out.println("The following error has occured: " + e);
    }

    GUI mainFrame = new GUI();
    loginGUI loginFrame = new loginGUI();


    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int width = screenSize.width / 2;
    int height = screenSize.height / 2;

    loginFrame.setLocation(width, height);
    loginFrame.setSize(300, 200);
    loginFrame.setVisible(true);

    mainFrame.setSize(mainFrame.getToolkit().getScreenSize());
    mainFrame.setVisible(true);

  } //main


LVL 1
brjattwoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
You can add the components you want the way you want to your JPanel.
As if you don't know that JPanel will be added to a JTabbedPane.
0
zzynxSoftware engineerCommented:
So:

    JPanel flightPanel1 = new JPanel();

    // Here you can add whatever you want to your flightPanel1
    // eg.
    flighPanel1.setLayout(new GridbagLayout());
    GridbagConstraints gbc = new GridbagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    flighPanel1.add(new JLabel("Hello world"), gbc);

    tabbedPane.addTab("Flight Info", image1, flightPanel1, "Flight Details");
    tabbedPane.setSelectedIndex(0);

Tell me if I didn't understand your Q.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
>> My problem is I do not know at what stage I should be setting the layout constraints and adding the content
It isn't even necessary to do it before

       tabbedPane.addTab("Flight Info", image1, flightPanel1, "Flight Details");

You can also construct the flightPanel1 afterwards,
as long as it is *before* the tabbed pane is made visible.
0
zzynxSoftware engineerCommented:
So the following is OK:

    JTabbedPane tabbedPane = new JTabbedPane();

    JPanel flightPanel1 = new JPanel();
    tabbedPane.addTab("Flight Info", image1, flightPanel1, "Flight Details");

    Component passengerPanel1 = new JPanel();
    tabbedPane.addTab("Passenger Info", image1, passengerPanel1,
                      "Passenger Details");

    Component groupPanel1 = new JPanel();
    tabbedPane.addTab("Group Info", image1, groupPanel1,
                      "Group Details");

    Component layoutPanel1 = new JPanel();
    tabbedPane.addTab("Layout Info", image1, layoutPanel1,
                      "Layout Details");

    constructFlightPanel(flightPanel1);
    constructPassengerPanel(passengerPanel1);
    constructGroupPanel(groupPanel1);
    constructLayoutPanel(layoutPanel1);

with e.g.

    private void constructFlightPanel(JPanel flightPanel) {
        flighPanel.setLayout(new GridbagLayout());
        GridbagConstraints gbc = new GridbagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        flighPanel.add(new JLabel("Hello world"), gbc);
       
        // adding of other components...
    }
0
zzynxSoftware engineerCommented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.