Results 1 to 6 of 6
  1. #1
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default 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();
                }
            });
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JPanel and GridBagLayout position

    Thread moved from New to Java.

    Quote Originally Posted by Naxix View Post
    i have a hard time position the JComboBoxes and JLabels, if i move my head Label, the comboboxes moves.
    "Move" your head label? How?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default 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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JPanel and GridBagLayout position

    My question was 'How?'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Naxix is offline Member
    Join Date
    Jul 2011
    Posts
    13
    Rep Power
    0

    Default Re: JPanel and GridBagLayout position

    This is my "head" label code:
    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);
    I used the gridx and y to move it, those are the ones i though i had to use to move JLabels with GridBagLayout,
    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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 03:47 AM
  2. Replies: 5
    Last Post: 11-09-2009, 06:12 PM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •