Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - GridBagLayout, strange problem

Options
  • 25-11-2009 3:19am
    #1
    Closed Accounts Posts: 267 ✭✭


    I generated a simple GridBagLayout, but when I put the result in a frame, it looks weird.

    The components are vertical, but they also replicate horizontally. I'm at my wits end with this and can't see what's wrong, help!
    public class GaussianNodePanel extends NodePanel {
        public GaussianNodePanel() {
            initComponents();
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.add(new GaussianNodePanel());
            frame.pack();
            frame.setVisible(true);
        }
        
        private JTextField coefficientsTextField;
        private JTextField sigmaTextField;
        private JTextField offsetTextField;
        
        @Override
        protected void initComponents() {
            JLabel label1 = new JLabel();
            coefficientsTextField = new JTextField();
            JLabel label2 = new JLabel();
            sigmaTextField = new JTextField();
            JLabel label3 = new JLabel();
            offsetTextField = new JTextField();
    
            //======== this ========
            setBorder(new TitledBorder("Properties"));
            setLayout(new GridBagLayout());
            ((GridBagLayout)getLayout()).columnWidths = new int[] {88, 190, 0};
            ((GridBagLayout)getLayout()).rowHeights = new int[] {23, 0, 0, 0};
            ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 1.0E-4};
            ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};
    
            //---- label1 ----
            label1.setText("Coefficients:");
            add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                new Insets(0, 0, 5, 5), 0, 0));
            add(coefficientsTextField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));
    
            //---- label2 ----
            label2.setText("Sigma:");
            add(label2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                new Insets(0, 0, 5, 5), 0, 0));
            add(sigmaTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 5, 0), 0, 0));
    
            //---- label3 ----
            label3.setText("Offset:");
            add(label3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
                new Insets(0, 0, 0, 5), 0, 0));
            add(offsetTextField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        }
    }
    


Comments

  • Registered Users Posts: 3,766 ✭✭✭Reku


    Are you sure gridbag is the best layout manager for this?
    Do you even need to use a layout manager for this, there aren't many things to be placed?


  • Registered Users Posts: 1,083 ✭✭✭Rulmeq


    I just ran your code, and changed the NodePanel to JPanel (I don't have access to your NodePanel, obviously) and it worked. Perhaps the issue is with the NodePanel class.

    XGiZ5.png


  • Closed Accounts Posts: 267 ✭✭waitinforatrain


    Ah, I totally overlooked that!
    public abstract class NodePanel extends JPanel {
        
        public NodePanel() {
            initComponents();
        }
        
        protected abstract void initComponents();
    }
    

    I was calling initComponents() in both constructors.

    Thanks a lot!


  • Registered Users Posts: 1,083 ✭✭✭Rulmeq


    I was calling initComponents() in both constructors.
    Cool, glad it worked out.

    As a general rule you should only call final methods from a constructor (which would mean this problem wouldn't happen)


Advertisement