Skip to Main Content

gridbag layout problem on a tabbedpane

843807Jun 6 2010 — edited Jun 7 2010
I am not sure whether we can do this or not but, I am trying to have a gridbag layout on a TabbedPane object.

I have a JFrame on which I am adding a TabbedPane object called "t" and on this TabbedPane I am adding a tab called "Insert" which is an object of class "Insert Data". Then, I add the TabbedPane t on the Container cp, which is inside the JFrame.

In the InsertData Class (a JPanel), I need to have the gridbag layout. With this gridbag layout object, I am trying to place different objects like buttons, at various places, on this JPanel. But nothing moves on this panel.

In short, please let me know how can I have a gridbag layout on a Tabbedpane.

The Main Class is as follows:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener, ChangeListener{
			
	Main(){
			setPreferredSize(new Dimension(1200,600));
			
			
			Container cp = getContentPane();
			
			JTabbedPane t = new JTabbedPane();
			
			
			// insert
			InsertData insertOptions = new InsertData();
		
			t.addTab("Insert",insertOptions);
			
			cp.add(t);
			
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			pack();
			setVisible(true);
		}

		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void stateChanged(ChangeEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
		public static void main(String args[]){
			new Main();
		}
			
	}
The InsertDataClass is:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class InsertData extends JPanel{
	InsertData(){
		setPreferredSize(new Dimension(1200,600));
		setBackground(Color.blue);
		
		//setLayout(new GridBagLayout());
		GridBagLayout gb = new GridBagLayout();
		setLayout(gb);
		GridBagConstraints c = new GridBagConstraints();
		//c.insets = new Insets(2, 2, 2, 2);

		//JPanel p1= new JPanel();
		//p1.setPreferredSize(new Dimension(200,200));
		JButton b1 = new JButton("here i am!!!");
		//p1.add(b1);
		//c.fill = GridBagConstraints.HORIZONTAL;
		//c.anchor = GridBagConstraints.WEST;
		c.gridx=0;
		c.gridy=1;
		add(b1,c);
		
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Jul 5 2010
Added on Jun 6 2010
6 comments
675 views