Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set size wont work in java

Tags:

java

swing

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 application. (the server etc, is done by others).

It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.

For example:

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

Won't work.

Thanks for the help.

like image 329
amana Avatar asked Apr 19 '11 09:04

amana


People also ask

How do I fix frame size in Java?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.

What happens if setSize () is not called on JFrame?

What happens if setSize is not called on a window? 1) The window is displayed at its preferred size. 2) It is a syntax error. 3) The window is not displayed.

How do I change the size of a content pane in Java?

In Java 5 and later this is the easiest method: JFrame frame = new JFrame("Content Pane Size Example"); frame. getContentPane(). setPreferredSize(new Dimension(400, 300)); frame.


3 Answers

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

Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.

Try calling setPreferredSize() and setMinimumSize().

like image 174
Joachim Sauer Avatar answered Oct 24 '22 01:10

Joachim Sauer


Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.

check resizing text area in a JFrame

like image 1
Bastardo Avatar answered Oct 24 '22 01:10

Bastardo


but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()

like image 1
mKorbel Avatar answered Oct 24 '22 02:10

mKorbel