Results 1 to 6 of 6
- 03-15-2012, 09:25 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 13
- Rep Power
- 0
JPanel and GridBagLayout position
Hello, so i'm quite new to Java, but i have been going through basics, me and some guys from school have this project going,
we have to make a "virtuel" company, and we choose to makeing a computer company, who makes custom build PC's,
and for that i thought i could make a little java program showing different option for each category (CPU, power supply, case, etc),
but i have a hard time position the JComboBoxes and JLabels, if i move my head Label, the comboboxes moves.
This is my code so far:
Java Code:import java.awt.*; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; public class AppStart { final static boolean shouldFill = true; final static boolean shouldWeightX = true; final static boolean RIGHT_TO_LEFT = false; final static String[] CPU = { "Intel Core i5 2500K Sandy brigde", "Intel Core i7 3820", "Intel Core i7 2600K Sandy Bridge", "Intel Core i7 2700K Sandy Brigde", "Intel Core i7 3930K", "AMD FX-8150", "AMD FX-4100", "AMD Phenom II X6 1045T" }; final static String[] Motherboard = { "ASUS P8Z68-V LX", "ASUS P8Z68-V PRO/GEN3", "ASUS SABERTOOTH X79" }; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } JComboBox comboBox = new JComboBox(); JLabel label = new JLabel(); pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); if (shouldFill) { c.fill = GridBagConstraints.RELATIVE; } //Label for Title label = new JLabel("Choose which parts you want for your computer:"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 100; c.gridy = 0; pane.add(label, c); //Label for CPU label = new JLabel("Choose a CPU"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 0; c.gridy = 10; pane.add(label, c); //ComboBox for CPU comboBox = new JComboBox(CPU); comboBox.setSelectedIndex(-1); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 30; c.gridy = 10; pane.add(comboBox, c); //Label for Motherboard label = new JLabel("Choose a Motherboard"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 0; c.gridy = 20; pane.add(label, c); //ComboBox for Motherboard comboBox = new JComboBox(Motherboard); comboBox.setSelectedIndex(-1); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 30; c.gridy = 20; pane.add(comboBox, c); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayoutDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addComponentsToPane(frame.getContentPane()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
- 03-16-2012, 02:21 AM #2
- 03-16-2012, 08:05 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 13
- Rep Power
- 0
Re: JPanel and GridBagLayout position
The position of it on the screen, like if i move to father along the x axis, it changes the position of some of the Comboboxes too. And sometimes it dosn't change at all,
is this because GridBagLayout is formed by a grid, and the positioning of objects is acting wierd?
Naxix
- 03-16-2012, 04:17 PM #4
Re: JPanel and GridBagLayout position
My question was 'How?'
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-16-2012, 04:28 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 13
- Rep Power
- 0
Re: JPanel and GridBagLayout position
This is my "head" label code:
I used the gridx and y to move it, those are the ones i though i had to use to move JLabels with GridBagLayout,Java Code://Label for Title label = new JLabel("Choose which parts you want for your computer:"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.RELATIVE; c.gridx = 100; c.gridy = 0; pane.add(label, c);
but when i run my code the comboboxes and labels are all over the screen, it's like i can't get them to move with x amount of pixels i put in
gridx and y, and if it does move, the comboboxes moves further to the right, so it's like i can't get the label and boxes to line up.
- 03-16-2012, 05:05 PM #6
Re: JPanel and GridBagLayout position
GridBagConstraints#gridx/y are not pixel positions -- they are column/row indexes.
I suggest you go through the tutorial on How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Change JPanel text of Parent JPanel from JDialog
By bikashlama in forum AWT / SwingReplies: 7Last Post: 12-09-2011, 03:47 AM -
GridBagLayout and multiple JPanel's on a single JFrame
By Cybex in forum AWT / SwingReplies: 5Last Post: 11-09-2009, 06:12 PM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 01:36 AM -
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks