Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java - GridBagLayout, strange problem

  • 25-11-2009 03: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, Registered Users 2 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, Registered Users 2 Posts: 1,126 ✭✭✭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, Registered Users 2 Posts: 1,126 ✭✭✭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