1

I'm a new in Swing. I'm trying to create a window 800x600 that will be divided into two equal parts (800x300): first one a immutable text-area and the second - button.

Problem: Button should occupy the bottom half of the window. But seems button size doesn't work correct.

Code:

import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        JFrame.setDefaultLookAndFeelDecorated(true);
        final JFrame frame = new JFrame("Lingvo frame");
        frame.setPreferredSize(new Dimension(800, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(4,4,4,4));
        final JButton showResultButton = new JButton();
        showResultButton.setPreferredSize(new Dimension(800, 300));
        showResultButton.setHorizontalAlignment(SwingConstants.CENTER);
        showResultButton.setVerticalAlignment(SwingConstants.BOTTOM);

        frame.add(panel);
        panel.add(showResultButton);

        frame.pack();
        frame.setVisible(true);

        showResultButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                showResultButton.setText("translation");
            }
        });
    }
} 
VB_
  • 45,112
  • 42
  • 145
  • 293

2 Answers2

2

Just set your frame to a GridLayout. Also note just set the size of JTextArea using this constructor JtextArea(rows, columns) and just pack() your frame. No need to set size to anything. TheGridLayout will handle the size of the JButton for you, based on the size of the JTextArea

Also Swing apps should be run from the Event Dispatch Thread. See Initial Threads.

Also see Laying out Components within a Container to learn more about layout managers.

Here's a running example

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                final JFrame frame = new JFrame("Lingvo frame");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JButton showResultButton = new JButton();
                final JTextArea textArea = new JTextArea(20, 60);

                frame.setLayout(new GridLayout(2, 1));
                frame.add(textArea);
                frame.add(showResultButton);

                frame.pack();
                frame.setVisible(true);

                showResultButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent arg0) {
                        showResultButton.setText("translation");
                    }
                });
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Can you align your alignt JTextArea text to center please? And set some beautiful font to it? – VB_ Feb 11 '14 at 17:26
  • What do you mean align to the center? Also see [**How to Use Fonts**](http://docs.oracle.com/javase/tutorial/2d/text/fonts.html). Once you learn that, just use `textArea.setFont(font)` – Paul Samsotha Feb 11 '14 at 17:29
  • I mean that if I set text to JTextArea it'll be shown at the left top corner of the text area – VB_ Feb 11 '14 at 17:31
  • So you _don't_ want it aligned in the top-left? – Paul Samsotha Feb 11 '14 at 17:33
  • I want it'll be aligned at the center (both horizontal and vertical) – VB_ Feb 11 '14 at 17:34
  • Ok that is a whole other SO question you should be asking. But you can take a look that [**this**](http://stackoverflow.com/a/3213361/2587435) and [**this**](http://java-sl.com/tip_center_vertically.html) – Paul Samsotha Feb 11 '14 at 17:37
1

The panel where the button is inserted has a GridLayout(4,4,4,4). This means that the Panel will contain 4*4 components, and all will have the same size (!).

Your code does not contain a text area at the moment. But based on the description, your layout could probably be achieved with

panel.setLayout(new GridLayout(2,1));
panel.add(theTextArea); // The top half
panel.add(button); // The bottom half

You'll have to read a little about Layout Managers ( http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html ) to become familiar with them. Particularly concerning the question of how they take the "preferred size" into account.

Marco13
  • 53,703
  • 9
  • 80
  • 159