user3633989
user3633989

Reputation: 29

Can not set size for frame

i want to set a size for frame but i cant, why? the frame is in createandshowgui method i did set for frame,table everything but didnt do anything, i want to set 1024x768

public static class DisplayResult extends JPanel {

public DisplayResult() {
    super(new GridLayout(1,0));

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    final JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setFillsViewportHeight(true);

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
}


public static void createAndShowGUI() {
    final JButton button1=new JButton("Vissza");
    JPanel panel=new JPanel();
    final JFrame frame = new JFrame("Database Data");
    frame.setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1024,768);
    DisplayResult newContentPane = new DisplayResult();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.add(panel);
    panel.add(button1);
    frame.pack();
    frame.setVisible(true);


}

Upvotes: 0

Views: 79

Answers (2)

user3127896
user3127896

Reputation: 6563

You are using both frame.setSize() and frame.pack(). You can use only one of them at one time.

Using setSize() you give the size of frame you want but if you use pack() it will be automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier.

Remove frame.pack() o put it before setSize().

Upvotes: 1

Masudul
Masudul

Reputation: 21961

The problem is on pack method. Never use pack method with setSize;

frame.setSize(1024,768);

.........
//frame.pack(); Comment this line.

Upvotes: 0

Related Questions