Can't change size of block in BorderLayout
Asked Answered
G

2

5

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

Gentoo answered 27/4, 2013 at 13:18 Comment(1)
May you please specify a bit more regarding your needs, for someone to answer you on those lines :-) An image of exactly what you want, a rough sketch, is all what is needed to make an idea, of what line one must follow to understand the need of the question. +1 for SSCCEArrears
A
4

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

Arrears answered 27/4, 2013 at 14:45 Comment(0)
N
6

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);
   };
};
Nativity answered 27/4, 2013 at 13:40 Comment(3)
thx. Sorry I made error in title. But can I to change default size of BorderLayout.NORTH?Gentoo
Adjust the height parameter so that you see an appreciable difference on the default and modified sizes. See minor updateNativity
Seems to me you can only set the heightAldosterone
A
4

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

Arrears answered 27/4, 2013 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.