Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change size of block in BorderLayout

I created simple app with Border Layout and added into it two buttons and JTable. I use JSplitPane between button2 and JTable. I would like redefine default size of block where is situated button1. How can I to solve this task?

Here is my code:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class Sample {

    public Sample() {
        JFrame app = new JFrame("Sample");
        app.setSize(new Dimension(800,600));

        JPanel panel = new JPanel();
        app.add(panel);

        BorderLayout borderlayout = new BorderLayout();
        panel.setLayout(borderlayout);

        JButton but1 = new JButton("1");
        JButton but2 = new JButton("2");
        but2.setMinimumSize(new Dimension(250,0));
        String[] colNames = {"Name","Number","Scores"};
        Object[][] data = {
                { "Mark",11,12},
                {"Tommy",23,34},
                {"John",34,45}
        };
        JTable table = new JTable(data, colNames);

        JScrollPane scrollpane = new JScrollPane(table);
        JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,but2,scrollpane);

        panel.add(but1,BorderLayout.NORTH);
        panel.add(jsplitpane,BorderLayout.CENTER);

        app.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Sample();
            }
        });
    }
}

enter image description here

like image 664
veinhorn Avatar asked Apr 27 '13 13:04

veinhorn


People also ask

How does Java's BorderLayout work?

Class BorderLayout. A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH , SOUTH , EAST , WEST , and CENTER .

How do you make a JPanel not resizable?

Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. : JFrame Window « Swing « Java Tutorial. 14.80.

What happens when adding a component to a BorderLayout If you do not specify the region in which the component should be placed?

Figure 7.5: BorderLayout with missing regions If the name is not "North", "South", "East", "West", or "Center", the component is added to the container but won't be displayed. Otherwise, it is displayed in the appropriate region.

How do I change the layout of a panel in Java?

You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.


2 Answers

Components in the BorderLayout.PAGE_START location have the height of their preferred sizes respected. Therefore, you can override the preferred size of JButton but1

JButton but1 = new JButton("1") {
   public Dimension getPreferredSize() {
      return new Dimension(100, 80);
   };
};
like image 165
Reimeus Avatar answered Sep 30 '22 04:09

Reimeus


If you are willing to use GridBagLayout for the said purpose, then I guess this Layout and do this job for you, as stated in the below pasted code example :-)

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class Sample {

    public Sample() {
        JFrame app = new JFrame("Sample");
        app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        app.setSize(new Dimension(800,600));

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setLayout(new BorderLayout(5, 5));
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());        

        JButton but1 = new JButton("1");
        JButton but2 = new JButton("2");
        but2.setMinimumSize(new Dimension(250,0));
        String[] colNames = {"Name","Number","Scores"};
        Object[][] data = {
                { "Mark",11,12},
                {"Tommy",23,34},
                {"John",34,45}
        };
        JTable table = new JTable(data, colNames);

        JScrollPane scrollpane = new JScrollPane(table);
        JSplitPane jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,but2,scrollpane);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.3;

        centerPanel.add(but1, gbc);        

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridy = 1;
        gbc.weighty = 0.7;

        centerPanel.add(jsplitpane, gbc);

        panel.add(centerPanel, BorderLayout.CENTER);

        app.add(panel);
        app.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Sample();
            }
        });
    }
}

Here is the output of the same :

GRIDBAGEXAMPLE

like image 20
nIcE cOw Avatar answered Sep 30 '22 05:09

nIcE cOw