Set size doesn't work in java

public void start_Gui() { JFrame window = new JFrame("Client Program"); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); window.setContentPane(panel); panel.setLayout(new GridLayout(1,2)); JLabel leftside = new JLabel(); leftside.setLayout(new GridLayout(2, 1)); JTextArea rightside = new JTextArea(); rightside.setEditable(false); //add scroll pane. rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK)); rightside.setLayout(new FlowLayout()); JTextArea client_text_input = new JTextArea(); client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK)); leftside.add(client_text_input); JLabel buttons_layer = new JLabel(); JButton login = new JButton("Login"); JButton logout = new JButton("Logout"); buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK)); buttons_layer.setLayout(new GridLayout(2, 1)); buttons_layer.add(login); buttons_layer.add(logout); leftside.add(buttons_layer); panel.add(leftside); panel.add(rightside); window.setSize(300, 400); window.setResizable(false); window.setVisible(true); } 

I am working on a simple java chat client gui client. (server, etc., done by others). A.

This is not a big project, but my only problem is that everything I do to resize any components in the above GUI will not work.

For example:

 JTextArea client_text_input = new JTextArea(); client_text_input.setSize(100,200); 

Does not work.

Thanks for the help.

+8
java swing
source share
3 answers

In Swing, you have two options for layout: do it all manually or let LayoutManager handle it for you.

The setSize() call will only work when you are not using LayoutManager . Since you are using GridLayout , you will have to use other methods to indicate what you want.

Try calling setPreferredSize() and setMinimumSize() .

+20
source share

Two things - firstly, you have to set your preferred scroll size, but secondly, trying to resize it inside the componentResized handler is not very effective, because resized events are not continuous.

check resize text area in jframe

+1
source share

but setXxxSize (for ContainersChilds) works like haima if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you need to call pack() to setVisible()

+1
source share

All Articles