4

I am developing a Java Swing application. I have a small issue on how to resize the layout in a JFrame to the same size of the JFrame in the Design tab in Eclipse.

The layout's space is not enough to effectively design my intended user interface. I can add items in a crammed manner and those items display in the final output but at design time I would like the layout to expand as much as the JFrame. Following image would describe my problem:

Two text fields one in layout other is not.

I tried this code but it does not seem to work:

getContentPane().setPreferredSize(new Dimension(800, 600));

This is my code:

public class ExployeeScreen extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ExployeeScreen frame = new ExployeeScreen();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ExployeeScreen() {
        setBounds(100, 100, 450, 300);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        getContentPane().setPreferredSize(new Dimension(800, 600));

    }

}

Any help regarding this is highly appreciated. Thanks :)

howlger
  • 31,050
  • 11
  • 59
  • 99
Lucy Ferrier
  • 202
  • 1
  • 2
  • 11
  • 5
    Don't use "absolute layout" or a "null layout". Swing was designed to be used with layout managers. When you use layout managers properly, Swing components will adjust sizes as required. Read the Swing tutorial on [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more information and working examples to get your started. – camickr Sep 16 '17 at 18:50
  • Thanks @camickr I would look into that :) – Lucy Ferrier Sep 16 '17 at 18:54
  • 2
    You can also call `setSize()` on a JFrame to set it to a particular size. Don't call `pack()` (that will change the size). Once you find a size you like your layout can be fit into that. Don't call `setBounds()`, it's almost always the wrong thing to do. – markspace Sep 16 '17 at 18:54
  • @markspace I want my JFrame to be fullscreen so that is why I used the method setExtendedState(JFrame.MAXIMIZED_BOTH); . Yes setBounds() seems like a wrong choice. Will look into your suggestion as well :) – Lucy Ferrier Sep 16 '17 at 18:59
  • 1
    @markspace `setSize` calls `setBounds` - so it's useless as the other – MadProgrammer Sep 16 '17 at 20:53

0 Answers0