0

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


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

1

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().

user3127896
  • 6,323
  • 15
  • 39
  • 65
0

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

frame.setSize(1024,768);

.........
//frame.pack(); Comment this line.
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • 1
    If you elect to use [`setSize()`](http://stackoverflow.com/a/7229662/230513), do so _after_ `pack()`. – trashgod May 25 '14 at 13:02