开发者

setSize of JPanel after initialisation

I am bringing in a ResultSet to show the contents of my table. thing is that though the results are showing i have been trying to resize the height of the JPanel so that it shows once it is populated. I tried using setSize(300, y) in the while(ResultSet.next) loop and also tried it outside of the loop. but nothing gives.

Here is my code:

package SimpleCRUD;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ShowContactsFrame extends JFrame {
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints Constraint = new GridBagConstraints();
    JLabel lblFname, lblLname, lblPhoneNumber;
    Statement st;
    String showAll = "SELECT * FROM `contacts`.`contacts`;";
    ResultSet rt;
    int y = 0;

    public ShowContactsFrame() {
        super("Contact List");
        try {
            AppCRUD initCRUD = new AppCRUD();
            rt = initCRUD.DBhandle.createStatement().executeQuery(showAll);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel = new JPanel(layout);
        add(panel);
        getContentPane().add(panel, BorderLayout.NORTH);

        lblFname = new JLabel("lblFname");
        lblLname = new JLabel("lblLname");
        lblPhoneNumber = new JLabel("lblPhoneNumber");

        Insets inset = new Insets(10, 10, 10, 10);

        Constraint.insets = inset;

        try {
            while (rt.next()) {
                Constraint.gridx = 0;
                Constraint.gridy = y;
                panel.add(new JLabel(rt.getString(2)), Constraint);
                Constraint.gridx = 1;
                Constraint.gridy = y;
                panel.add(new JLabel(rt.getString(3)), Constraint);
                Constraint.gridx = 2;
                Constraint.gridy = y;
                panel.add(new JLabel(rt.getString(4)), Constraint);
                y++;
                pan开发者_运维问答el.setSize(300, y);
            }

        } catch (Exception ex) {

            ex.printStackTrace();
        }
        JOptionPane.showMessageDialog(null, y);

    }

}


If you want to set the size of the panel you should use setPreferredSize() instead of setSize() in order as below.

panel.setPreferredSize(new Dimension(300, 300));
frame.pack();
frame.setVisible(true);

You have to do it this way since the component is controlled by a layout manager and therefore it ignores calls to setSize(). Which then is only used if you setLayout() to null, i.e. you use no manager.


What I would suggest is that you call pack() on the JFrame once you have built your GUI elements and set the sizes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜