Results 1 to 5 of 5
- 07-04-2011, 08:55 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Positioning items with GridBagConstraints
Can anybody tell me why the following code sticks all of my items in the center of the window?
I want to push them all up closer to the top. How do I accomplish this?Java Code:public static void mainScreen() { JFrame frame = new JFrame(); pane = frame.getContentPane(); JButton addBtn = new JButton("Add Kid"); JLabel cabinLabel = new JLabel("Select a cabin"); String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4", "Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"}; JComboBox cabinList = new JComboBox(cabins); frame.setTitle("WOW"); frame.setSize(700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 0; pane.add(cabinLabel, c); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 1; c.insets = new Insets(0,0,0,200); pane.add(cabinList, c); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 2; pane.add(list, c); pane.add(addBtn); frame.validate(); addBtn.setMnemonic(KeyEvent.VK_A); addBtn.setActionCommand("Add new kid");
-
You carefully use GridBagConstraints when adding components but don't when adding the addBtn, why?
What do you wish your GUI to look like and what does it currently look like instead? Images would be helpful.
Create and post a small compilable program that demonstrates your problem. In your case here, all you'd need to do would be to declare some variables that you're using display the JFrame and add a main method to call this method.
- 07-04-2011, 10:19 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Just hadn't gotten around to doing it for the button yet. I've attached an image of what it currently looks like. I want the label, dropdown box, and button all moved up instead of centered. Here is the code
Java Code:public class WOW { public static final int MAX_KIDS = 500; static Hashtable<String, Kid> kids = new Hashtable<String, Kid>(MAX_KIDS); static JList list = new JList(); static Container pane = new Container(); public static void main(String[] args) { Kid kid1 = new Kid("Cody", 20, 3, "", "", "", "", "", "", "", "", "","","",""); Kid kid2 = new Kid("Joey", 20, 3, "", "", "", "", "", "", "", "", "","","",""); Kid kid3 = new Kid("George", 20, 3, "", "", "", "", "", "", "", "", "","","",""); Kid kid4 = new Kid("Daniel", 20, 3, "", "", "", "", "", "", "", "", "","","",""); kids.put(kid1.getName(), kid1); kids.put(kid2.getName(), kid2); kids.put(kid3.getName(), kid3); kids.put(kid4.getName(), kid4); mainScreen(); public static void mainScreen() { JFrame frame = new JFrame(); pane = frame.getContentPane(); JButton addBtn = new JButton("Add Kid"); JLabel cabinLabel = new JLabel("Select a cabin"); String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4", "Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"}; JComboBox cabinList = new JComboBox(cabins); frame.setTitle("WOW"); frame.setSize(700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 0; pane.add(cabinLabel, c); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 1; c.insets = new Insets(0,0,0,200); pane.add(cabinList, c); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 2; pane.add(list, c); pane.add(addBtn); frame.validate(); addBtn.setMnemonic(KeyEvent.VK_A); addBtn.setActionCommand("Add new kid"); //Action Listener for Add Button addBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { addKid(); } }); //Action Listener for Cabin List cabinList.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cabinList = (JComboBox)e.getSource(); String cabinSelected = (String)cabinList.getSelectedItem(); updateCabinList(cabinSelected); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setLayoutOrientation(JList.VERTICAL); //frame.validate(); } }); }
-
Your code doesn't compile for us and has code inside of it completely unrelated to the problem. If you're still having problems, please remove all references to Kid or any other non-essential classes (non-essential for your problem at hand), and just create a simple GUI that shows your layout issue. The idea is to create a very small class that we can run and modify and hopefully be able to help you with. Also what do you mean by "all moved up"? Again, a picture is worth a thousand words.
Last edited by Fubarable; 07-04-2011 at 10:39 PM.
- 07-04-2011, 11:17 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
See: How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)Can anybody tell me why the following code sticks all of my items in the center of the window?
You will find a comment that says:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.
which would appear to be your problem.
Similar Threads
-
Position 5 buttons with GridBagConstraints in Swing
By albertkao in forum AWT / SwingReplies: 0Last Post: 02-21-2011, 03:15 PM -
GridBagConstraints help
By noobgrammer in forum New To JavaReplies: 5Last Post: 07-03-2010, 04:12 AM -
[SOLVED] [newbie] java.awt.GridBagConstraints
By jon80 in forum New To JavaReplies: 0Last Post: 05-27-2009, 03:40 PM -
Positioning using setBounds
By thayalan in forum AWT / SwingReplies: 4Last Post: 04-28-2009, 01:59 AM -
GridBagConstraints
By Aswq in forum New To JavaReplies: 2Last Post: 07-21-2008, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks