?
Solved

Adding content to JPanel component of JTabbedPane :- Problem

Posted on 2004-11-09
5
Medium Priority
?
701 Views
Last Modified: 2012-08-13
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


0
Comment
Question by:brjattwood
  • 5
5 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12533946
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
 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 12533997
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12534030
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12534097
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12535074
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question