8 Replies - 1512 Views - Last Post: 29 November 2012 - 04:40 PM Rate Topic: -----

#1 M-rhodes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 131
  • Joined: 24-September 10

Adjusting layout of objects within a tab that is part of tabbedPane

Posted 03 November 2012 - 12:53 PM

Hi, Currently I have a tabbedPane object that contains 4 sub tabs. Each tab will contain objects e.g. Textboxes, labels etc. I have been adding objects to each tab and the layout is not how I want it to be. How do I apply the layout manager to each sub tab?

All of the code is within a class that extends JFrame and implements the actionListener

The button is aligned in the top middle of tab 2.


       //The tabbed pane object is constructed
       JTabbedPane tabs = new JTabbedPane();

       //This creates the template for the multiple tabs using JTabbedPane
       getContentPane().add(tabs);

       JPanel panelAddEmployee = new JPanel();//This will create the first tab "Add Employee"

       JPanel panelSearchEmployee = new JPanel();//This will create the second tab "Search for an Employee"
        
       JPanel panelRemoveEmployee = new JPanel();//This will create the third tab "Remove an Employee"
       
       JPanel panelSysOptions = new JPanel();//This will create the fourth tab "System options"

       //This adds the first, second, third and final tab to the tabbedPanel Object
       tabs.addTab("Add Employee", panelAddEmployee);
       tabs.addTab("Search for an Employee", panelSearchEmployee);
       tabs.addTab("Remove an Employee", panelRemoveEmployee);
       tabs.addTab("System Options", panelSysOptions);
       

       //This creates a new button called "Press" and adds it to the second tab
       JButton test = new JButton("Press");
       panelSearchEmployee.add(test);
       



Is This A Good Question/Topic? 0
  • +

Replies To: Adjusting layout of objects within a tab that is part of tabbedPane

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 03 November 2012 - 03:16 PM

First it contains 4 tabs not sub tabs.
You can apply the layout you want to all of these JPanel, they don't need to be the same
Was This Post Helpful? 1
  • +
  • -

#3 M-rhodes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 131
  • Joined: 24-September 10

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 22 November 2012 - 11:58 AM

View Postpbl, on 03 November 2012 - 03:16 PM, said:

First it contains 4 tabs not sub tabs.
You can apply the layout you want to all of these JPanel, they don't need to be the same


I've been working on this in my spare time. I've hit a brick wall. Currently, I have 5 tabs, I want to use gridbag layout for each of the tabs. I'am having an alignment issue within the first tab, I have added 3 labels and they are in a gridbased format, each object within a cell is aligned to the left. But all of the objects are aligned in the centre of the Jpanel.

Help, as always, is appreciated.

Matt
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 22 November 2012 - 12:24 PM

As I said it has nothing to do with the fact that you use a JTabbedPane
A JPanel is a JPanel and will behave the same way if it is displayed in a JFrame, within another JPanel, in a JTabbedPane, inside an Applet, ...

I am surely not the competent peron to help you with a GridBagLayout as I never had to use them but stop focusing on the Tab part. For test pupose you can use a simple JFrame with that JPanel in the BorderLayout.CENTER region of it and debug there
Was This Post Helpful? 1
  • +
  • -

#5 M-rhodes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 131
  • Joined: 24-September 10

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 27 November 2012 - 09:11 AM

Hi, as you can see I have aligned each object within a cell to the left. But the entire gridbag is aligned to the center. Im wondering whether to use another layout manager on top of the gridbag layout.


import java.awt.*;

import javax.swing.*;


public class Main_Window_Frame extends JFrame{

		
	public Main_Window_Frame() {
		
		super("Employee Record System");
		
        setSize(750,450); //set the size of the form
        //The tabbed pane object is constructed
        JTabbedPane TabGroup = new JTabbedPane();
        
        //This creates the template for the multiple tabs using JTabbedPane
        getContentPane().add(TabGroup);
       
        JPanel panelAddEmployee = new JPanel((LayoutManager)new FlowLayout(FlowLayout.LEFT));//This will create the first tab "Add Employee"

        JPanel panelSearchEmployee = new JPanel();//This will create the second tab "Search for an Employee"
        
        JPanel panelRemoveEmployee = new JPanel();//This will create the third tab "Remove an Employee"
       
        JPanel panelListAllEmployees = new JPanel();//This will create the fourth tab "List all Employees"
       
        JPanel panelSysOptions = new JPanel();//This will create the fifth tab "System options"
      
      
        
        //Add objects that are related to the Add employee panel
       
        //Create the Add Employee label
        JLabel lblAddEmployee = new JLabel(); //Construct label "Add Employee"
        lblAddEmployee.setText("Add Employee"); //Set the text to "Add Employee"
        lblAddEmployee.setFont(new Font("Arial",Font.BOLD,24)); //Sets the font type to Arial bold, size 24
     
        //Create the Employee ID label
        JLabel lblBlank = new JLabel(); 
        lblBlank.setText("1"); 
 
       
        //Create the Employee ID label
        JLabel lblEmpID = new JLabel(); 
        lblEmpID.setText("Employee ID:"); 
        lblEmpID.setFont(new Font("Arial",Font.PLAIN,12));
        lblEmpID.setVisible(true);
       
        //Implements the grid bag layout for the Add employee panel
        panelAddEmployee.setLayout(new GridBagLayout());
 
        //gbc is used to specify a components display area and defines positioning of objects
        GridBagConstraints gbc = new GridBagConstraints();
       
        //Setup external padding of objects
        gbc.insets = new Insets(10,10,10,10);
        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 0;
        gbc.gridy = 0;
        
        panelAddEmployee.add(lblAddEmployee,gbc);
        
        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 0;
        gbc.gridy = 1;
       
        panelAddEmployee.add(lblEmpID,gbc);
        gbc.anchor = (gbc.gridx == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
        gbc.gridx = 1;
        gbc.gridy = 1;
       
        panelAddEmployee.add(lblBlank,gbc);
        
        //This adds the first, second, third and final tab to the tabbedPanel Object
        TabGroup.addTab("Add Employee", panelAddEmployee);
        TabGroup.addTab("Search for an Employee", panelSearchEmployee);
        TabGroup.addTab("Remove an Employee", panelRemoveEmployee);
        TabGroup.addTab("List all Employees", panelListAllEmployees);
        TabGroup.addTab("System Options", panelSysOptions);
		
	}
}




Forgot to add a screenshot of the jframe.

Attached image(s)

  • Attached Image

This post has been edited by M-rhodes: 27 November 2012 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 M-rhodes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 131
  • Joined: 24-September 10

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 28 November 2012 - 02:21 PM

Still at a stand still with this problem. I have re-read the gridbag layout example on the Oracle site and I have tried using 1 JPanel within a JFrame.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 28 November 2012 - 08:16 PM

I have read GridBagLayout documentation 15 years ago
found it over complicated for nothing (was designed for GUIO Builder let them use it)
didn't found where it can be useful
never used it, never feel that I would require it
Was This Post Helpful? 0
  • +
  • -

#8 M-rhodes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 131
  • Joined: 24-September 10

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 29 November 2012 - 04:05 AM

I managed to solve the problem. The issues was with regards to the last objects position.

I created a loop that adds the objects to the Jpanel.
Labels are stored in a Label array "addEmployeeLabels".
When the last one is visited [12], it is given specific gridbag constraints.

if (i == 12){
   c.weighty = 1.0; //vertical weight is 0.5
   c.anchor = GridBagConstraints.PAGE_START; //Ensures last label is at the edge of its display area
   c.gridx = 0;  //Place in column 0     
   c.gridy = 12; //Place in column 12
   addEmployeeLabels[12].setFont(new Font("Arial",Font.PLAIN,12));

}



This post has been edited by M-rhodes: 29 November 2012 - 04:07 AM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Adjusting layout of objects within a tab that is part of tabbedPane

Posted 29 November 2012 - 04:40 PM

good for you :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1