Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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);
   };
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top