0

I'm learning swing gui and I get this result when I use the following code:-

What I get

The code I use :-

private void initUI() {
        JTextArea visualize=new JTextArea();
        visualize.setEditable(false);

        //DEFINE BUTTONS....

        JButton[] buttons1={addition,subtraction,division,multiplication};
        JButton [] buttons2={expr,date,conversion};

        JPanel numerical=new JPanel(new FlowLayout());
        numerical.setPreferredSize(new Dimension(350, 50));
        for(int i=0;i<buttons1.length;i++){
            numerical.add(buttons1[i]);
        }
        numerical.setBorder(new TitledBorder("Numerical Operations"));

        JPanel nonnum=new JPanel(new FlowLayout());
        nonnum.setPreferredSize(new Dimension(500, 50));
        for(int i=0;i<buttons2.length;i++){
            nonnum.add(buttons2[i]);
        }
        nonnum.setBorder(new TitledBorder("Non-numerical Operations"));

        JPanel operations = new JPanel(new BorderLayout(2,2));
        operations.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        operations.setSize(800, 100);
        operations.add(numerical,BorderLayout.WEST);
        operations.add(nonnum,BorderLayout.EAST);

        JTable sheet = new JTable(10,5);



        add(visualize, BorderLayout.NORTH);
        add(sheet,BorderLayout.SOUTH);
        add(operations,BorderLayout.CENTER);
        pack();
        setSize(1000, 700);
        setTitle("Spreadsheet Application");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

But what I really want is this:- What I want

My questions :-

  1. Why is the operations panel too long?
  2. How can I change it's height?
  3. Doesn't "operations.setSize(..)" work?
S.Dan
  • 1,826
  • 5
  • 28
  • 55
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 02 '15 at 01:36

3 Answers3

2

Try using GroupLayout. I'm also new to swing gui and had similar problems - GroupLayout saved the day

    JPanel complete=new JPanel();
    GroupLayout gl=new GroupLayout(complete);
    complete.setLayout(gl);
    gl.setAutoCreateContainerGaps(true);

    gl.setHorizontalGroup(gl.createParallelGroup() //this is parallel bcz you need components vertically
            .addComponent(visualize)       //you MUST add components to both horizontal and vertical groups
            .addComponent(operations)
            .addComponent(sheet)
    );

    gl.setVerticalGroup(gl.createSequentialGroup() //NOTE that this is sequential
            .addComponent(visualize)
            .addComponent(operations)
            .addGap(50)        //you can add gaps if you want
            .addComponent(sheet)
    );

    add(complete);
Zoey
  • 68
  • 1
  • 5
1

Because that's how BorderLayout works, take a closer look at How to Use BorderLayout. The CENTRE position will occupy all the remaining space of the frame, where as the NORTH and SOUTH positions will try and honour the preferred sizes of the components.

You could use a GridBagLayout, which will allow you more control over the layout or use a series of compound layouts.

Something like...

setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;

add(visualize, gbc);
add(operations, gbc);
gbc.gridy = 1;
add(sheet, gbc);

It is generally discouraged to extend from a top level container like JFrame, instead you should use something like JPanel to define your UIs and then add them to the containers you want. This increases there re-usability and makes it easier to use on different containers

You may also like to take a look at How to Use Scroll Panes

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

SOME of us find GridBagLayout to be a royal pain, and you may not have to use it to do what you want to do.

The idea behind a layout manager is to let the layout manager control the size and position of its components so that you don't have to futz with them. MP is right, GridBag allows you a lot of control, but that also means you have to get a lot of things right to have it do what you want it to do.

So, an alternative: Make a JPanel to hold the visualize and operations panels; give this new panel a BoxLayout with a Y_AXIS orientation, then add them in the order you want them to appear, top-to-bottom.

Then put sheet in the BorderLayout.CENTER of the JFrame. In fact, I think you'll want to take MP's advice and go through a tutorial on JScrollPane; as best I remember, you create the panel, then create the JScrollPane instance with the panel as a construction parameter, then add the scrollpane instance to the JFrame (in the CENTER, in your case).

Being in the center, it will then expand and contract as the user changes window size.

Good luck. Swing takes some getting used to.

arcy
  • 12,845
  • 12
  • 58
  • 103