I have this simple MigLayout:
The two last components (JScrollPanes) should have the same width. But in fact, if I resize the window, they are randomly jumping. Is it possible to make their width equal? How else can I arrange components to make this look symmetrical?
You need your columns to use the same sizegroup/sg in your column constraints. This way both columns will always have the same width.
Like this:
setLayout(new MigLayout("", "[sizegroup main, grow][sizegroup main,grow]"[][][][grow]"));
See also the MigLayout Cheatsheet about sizegroup.
Most of the time I prefer to use the built-in facilities of java rather than mixing much further complex libraries and dependencies for such simple cases. I think when you can achieve the solution with a trivial effort like this it's not needed to use third party libraries such as MIG. This preference comes from the situation you are in: not so many people work with a purchased tools, so you can not get so many help from the community.
I know this question asks about MigLayout but I preferred to show that there is no need to use that for this simple situation. MIG library is rich and have some useful components which can make your life easier but when it comes to layouts I prefer pure java.
Sampling your layout using pure BorderLayout and GridLayout:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class TestMain {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame();
f.setBounds(50, 50, 500, 400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(createSpacerPanel(10, 10), BorderLayout.NORTH);
f.add(createSpacerPanel(10, 10), BorderLayout.SOUTH);
f.add(createSpacerPanel(10, 10), BorderLayout.EAST);
f.add(createSpacerPanel(10, 10), BorderLayout.WEST);
f.add(new MainPanel());
f.setVisible(true);
}
private static JPanel createSpacerPanel(int width, int height){
JPanel spacer = new JPanel();
spacer.setPreferredSize(new Dimension(width, height));
return spacer;
}
}
class MainPanel extends JPanel{
public MainPanel() {
init();
}
private void init() {
JPanel northPanel = new JPanel(new BorderLayout(10, 10));
northPanel.setPreferredSize(new Dimension(100, 60));
northPanel.add(new JLabel("Class Expression: "), BorderLayout.NORTH);
JTextArea classExpressionTextArea = new JTextArea();
classExpressionTextArea.setSize(10, 40);
northPanel.add(new JScrollPane(classExpressionTextArea), BorderLayout.CENTER);
JButton calculateButton = new JButton("Calculate");
northPanel.add(calculateButton, BorderLayout.EAST);
JPanel definitionPanel = new JPanel(new BorderLayout(10,10));
definitionPanel.add(new JLabel("Definitions Found: "), BorderLayout.NORTH);
JTextArea definitionsTextArea = new JTextArea();
definitionPanel.add(new JScrollPane(definitionsTextArea), BorderLayout.CENTER);
JPanel signaturePanel = new JPanel(new BorderLayout(10,10));
signaturePanel.add(new JLabel("Target Signature: "), BorderLayout.NORTH);
JTextArea targetTextArea = new JTextArea();
signaturePanel.add(new JScrollPane(targetTextArea), BorderLayout.CENTER);
GridLayout gridLayout = new GridLayout(1,1,10,10);
JPanel centerPanel = new JPanel(gridLayout);
centerPanel.add(definitionPanel);
centerPanel.add(signaturePanel);
setLayout(new BorderLayout(10,10));
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
}
}
And the fully resizable output:
The same can be achieved using GridBagLayout which is similar to MigLayout in the way of thinking about the layout and positioning and spanning components over grid cells.
Hope this would be helpful.
Related
I'm trying to get some of the buttons to be bigger and be able to move them around to get them more to what my professor wants them to be but I'm not sure how to do it.
I decided to use a GridBagLayout but my professor never talked about it so I'm not sure if I'm missing anything or how exactly it works.
The image is what he wants us to get it too. Exactly like this.
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI {
public static JPanel buttonPanel;
private static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("Layout Question");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
frame.add(mainPanel);
buttonPanel = new JPanel();
mainPanel.add(buttonPanel);
buttonPanel.add(new JButton("hi"));
buttonPanel.add(new JButton("long name"));
buttonPanel.add(new JButton("bye"));
buttonPanel.add(new JButton("1"));
buttonPanel.add(new JButton("2"));
buttonPanel.add(new JButton("3"));
buttonPanel.add(new JButton("4"));
buttonPanel.add(new JButton("5"));
buttonPanel.add(new JButton("6"));
buttonPanel.add(new JButton("7"));
buttonPanel.add(new JButton("Cancel"));
}
}
There are some improvements you can do to your code:
Don't use static variables, and place your program on the EDT, an easy way to do this is:
public static void main(String[] args) {
SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
}
Don't call setSize(...) on your JFrame, it's going to make your window smaller than you think, it's taking the frame decorations into the calculation for the size, instead call frame.pack(), or override the getPreferredSize() of your JPanel, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for a more in-depth explanation.
Don't call frame.setVisible(true) before you've added all your components to your JFrame, otherwise you'll get strange bugs related to invisible components, that line should be the last one on your code.
Divide and conquer, you can use multiple JPanels with different Layout Managers, and you can combine them and join them together later on.
One possible approach (which isn't exactly as your teacher wants it to be but is close enough) is this one:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LayoutManagersExample {
private JFrame frame;
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
}
private void createAndShowGUI() {
frame = new JFrame("Layout Question");
pane = new JPanel();
JPanel topPanel = getTopPanel();
JPanel boxesPanel = getBoxesPanel();
JPanel buttonsPanel = getButtonsPanel();
pane.add(boxesPanel);
pane.add(buttonsPanel);
frame.add(pane);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(new JButton("Cancel"), BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500, 500);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel getButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 2));
buttonsPanel.add(new JButton("1"));
buttonsPanel.add(new JButton("2"));
buttonsPanel.add(getInnerButtonsPanel());
buttonsPanel.add(new JButton("7"));
return buttonsPanel;
}
private JPanel getInnerButtonsPanel() {
JPanel innerButtonsPanel = new JPanel();
innerButtonsPanel.setLayout(new GridLayout(2, 2));
innerButtonsPanel.add(new JButton("3"));
innerButtonsPanel.add(new JButton("4"));
innerButtonsPanel.add(new JButton("5"));
innerButtonsPanel.add(new JButton("6"));
return innerButtonsPanel;
}
private JPanel getBoxesPanel() {
JPanel boxesPanel = new JPanel();
boxesPanel.setLayout(new BoxLayout(boxesPanel, BoxLayout.PAGE_AXIS));
boxesPanel.add(new JCheckBox("Bold"));
boxesPanel.add(new JCheckBox("Italic"));
boxesPanel.add(new JCheckBox("Underline"));
boxesPanel.add(new JCheckBox("Strikeout"));
return boxesPanel;
}
private JPanel getTopPanel() {
JPanel topPanel = new JPanel();
JPanel topButtonsPanel = new JPanel();
topButtonsPanel.setLayout(new GridLayout());
topButtonsPanel.add(new JButton("hi"));
topButtonsPanel.add(new JButton("long name"));
topButtonsPanel.add(new JButton("bye"));
topPanel.add(new JLabel("Buttons: "));
topPanel.add(topButtonsPanel);
return topPanel;
}
}
Play around with the code, and try to find a different approach by combining the layouts, divide each piece of the window in your head and see how to apply a different layout manager to each of them, divide them in methods as I did to make things easier to follow.
Find a way to left align the elements in the JCheckBoxes for example, and other things
This question is very similiar to this: JScrollPane doesn't top align when there is more than enough space to show the content I tried this solution, but it does not work.
When I add a jlabel to jscrollpane, when the jlabel is small, the label becomes centered. It works normally when the scrollbar shows. Setting boxlayout does not change anything. I feel like this isn't working properly because I'm setting a perferred size to the panel? But if I remove the line panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150)); The label becomes small when there is no text, and grows to accomdate text, which I don't want. If I add the panel instead of the label, it makes the screen scrollable even though there isn't text?
This is my code:
public class Test {
// JFrame
static JFrame frame = new JFrame("Test");
//panel 1
static JPanel panel = new JPanel(new BorderLayout());
// label to display text
static JLabel label = new JLabel();
//scroll panel in main method
public static void main(String[] args) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(label, BorderLayout.NORTH);
panel.add(Box.createVerticalGlue());
panel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 50), screenSize.height-150));
panel.setBorder(new EmptyBorder(5, 5, 5, 10));
JScrollPane jspanel = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//jspanel.add(label, BorderLayout.CENTER);
jspanel.setPreferredSize(new Dimension((int)(screenSize.width*0.7 - 70), screenSize.height-180));
jspanel.getVerticalScrollBar().setUnitIncrement(20);
jspanel.setBorder(BorderFactory.createEmptyBorder());
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
//panel.setPreferredSize(new Dimension(640, 480));
//frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(jspanel);
frame.setSize((int)(screenSize.width*0.7), screenSize.height - 50);
frame.revalidate();
frame.pack();
frame.setVisible(true);
}
}
jspanel.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
jspanel.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
That will align the scrollpane in its parent container, depending on the layout manager being used. It does not affect the alignment of any component added to the scrollpane. It is not needed.
the label becomes centered
The label is sized to fill the entire space available, so you need to customize how the text of the label is painted.
If you don't want it centered then you can place it at the top using:
label.setVerticalAlignment( SwingConstants.TOP );
After reworking your code, I came up with the following GUI.
I added a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
I eliminated all static references, except for the main method.
I reworked your code into methods so I could focus on one part of the GUI at a time.
Here's the complete runnable example. This is a minimal reproducible example.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class JScrollPaneTestGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JScrollPaneTestGUI());
}
#Override
public void run() {
JFrame frame = new JFrame("JScrollPane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jspanel = createJScrollPane();
frame.add(jspanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JScrollPane createJScrollPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.YELLOW);
panel.setPreferredSize(new Dimension(400, 300));
JLabel label = new JLabel();
label.setBorder(new EmptyBorder(5, 5, 5, 20));
label.setText("any text makes it centered beyond 40 lines");
//create panel
panel.add(label, BorderLayout.NORTH);
JScrollPane jspanel = new JScrollPane(panel);
return jspanel;
}
}
I have 7 JPanel containers in total. I'd like to add a png image that I generate, or buffer it with the help of the button(charger image) in the JPanel(imagePan)
Most of the examples I've seen so far in the Swing Tutorials use ImageIcon
The images generated are at 326X254
How to properly add an image to a panel?
Here you'll find the code generating the window below:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class View {
private JFrame frame;
private JPanel globalPan, firstHorisontalPan, secondhorisontalPan,
calibrationPan, imagePan, manipPan, solutionPan; // susp
private JButton raproche, ecarter, sauvgarder, demarrer, stop, charger;
private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane,
BorderFirstHorisontalPan, BorderResolPan, BorderCalibPan,
BorderChargerPan;
private JTextArea console;
private Box calibrationBox, solutionBox;
public void init() {
// declaration de JFrame
frame = new JFrame("Rubi's Cube IHM");
// JPanle
globalPan = new JPanel();
firstHorisontalPan = new JPanel();
secondhorisontalPan = new JPanel();
imagePan = new JPanel();
manipPan = new JPanel();
calibrationPan = new JPanel();
solutionPan = new JPanel();
//
calibrationBox = Box.createVerticalBox();
solutionBox = Box.createVerticalBox();
// borderLayout
BorderGlobalePan = new BorderLayout();
BorderSecondPane = new BorderLayout();
BorderManipPane = new BorderLayout();
BorderFirstHorisontalPan = new BorderLayout();
BorderResolPan = new BorderLayout();
BorderCalibPan =new BorderLayout();
BorderChargerPan = new BorderLayout();
// JButton
raproche = new JButton("raprocher");
ecarter = new JButton("ecarter");
sauvgarder = new JButton("sauvgarder");
demarrer = new JButton("demarrer");
stop = new JButton("stop");
charger = new JButton("charger image");
console = new JTextArea();
//add JPanel names
firstHorisontalPan.setBorder(BorderFactory.createTitledBorder("Etat"));
calibrationPan.setBorder(BorderFactory.createTitledBorder("calibration"));
solutionPan.setBorder(BorderFactory.createTitledBorder("résolution & manipulation"));
imagePan.setBorder(BorderFactory.createTitledBorder("visualisation"));
// definition of JButton size
raproche.setPreferredSize(new Dimension(200, 30));
ecarter.setPreferredSize(new Dimension(200, 30));
sauvgarder.setPreferredSize(new Dimension(200, 30));
demarrer.setPreferredSize(new Dimension(200, 30));
stop.setPreferredSize(new Dimension(200, 30));
charger.setPreferredSize(new Dimension(200, 30));
//definition of JPanel size
globalPan.setPreferredSize(new Dimension(1024, 600));
firstHorisontalPan.setPreferredSize(new Dimension(1024, 130));
secondhorisontalPan.setPreferredSize(new Dimension(1024, 480));
imagePan.setPreferredSize(new Dimension(850, 480));
manipPan.setPreferredSize(new Dimension(150, 480));
calibrationPan.setPreferredSize(new Dimension(200, 200));
solutionPan.setPreferredSize(new Dimension(200, 100));
calibrationBox.setPreferredSize(new Dimension(200, 200));
solutionBox.setPreferredSize(new Dimension(200, 100));
firstHorisontalPan.setLayout(BorderFirstHorisontalPan);
firstHorisontalPan.add(console);
//image
// JPane calibration
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(raproche);
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(ecarter);
calibrationBox.add(Box.createVerticalStrut(10));
calibrationBox.add(sauvgarder);
calibrationPan.setLayout(BorderCalibPan);
calibrationPan.add(calibrationBox, BorderLayout.CENTER);
// JPane resolution & manipulation
solutionBox.add(Box.createVerticalStrut(10));
solutionBox.add(demarrer);
solutionBox.add(Box.createVerticalStrut(10));
solutionBox.add(stop);
solutionPan.setLayout(BorderResolPan);
solutionPan.add(solutionBox, BorderLayout.CENTER);
//JPane ManipPane
manipPan.setLayout(BorderManipPane);
manipPan.add(calibrationPan, BorderLayout.NORTH);
BorderManipPane.setVgap(20);
manipPan.add(solutionPan, BorderLayout.CENTER);
//JPane secondPane
secondhorisontalPan.setLayout(BorderSecondPane);
secondhorisontalPan.add(manipPan, BorderLayout.WEST);
BorderSecondPane.setHgap(7);
secondhorisontalPan.add(imagePan, BorderLayout.CENTER);
//JPane GlobalHorisontalPane
globalPan.setLayout(BorderGlobalePan);
globalPan.add(firstHorisontalPan, BorderLayout.NORTH);
BorderGlobalePan.setVgap(10);
globalPan.add(secondhorisontalPan, BorderLayout.CENTER);
//Jpane imagePan
BorderChargerPan.setVgap(10);
imagePan.add(charger);
// window
frame.add(globalPan);
frame.setSize(1024, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setTitle("cubeBerry");
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
IHM
How to proprely add an image to a Jpanel?
Create an ImageIcon.
Add the icon to a JLabel.
Add the label to the JPanel.
Read the section from the Swing tutorial on How to Use Icons for more information and working examples.
Also, from your posted code, get rid of all the setPreferredSize() statements. The layout manager will determine the preferred size of the component. Swing was designed to be used with layout managers. Let the layout manager do its job.
console = new JTextArea();
When creating a JTextArea do something like:
console = new JTextArea(5, 30);
The will suggest the size should be 5 rows and 30 columns. Now the layout manager can calculate a preferred size based on this information.
private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane, ...
Variable names should NOT start with an upper case character. Most of your variable are correct, but not all. Be consistent!!!
frame.setSize(1024, 600);
Don't hard code a size. You don't know what the resolution of my computer is. Instead use the pack() method and let the layout managers do their job.
So I'm trying to set up a Gui in Java which holds a list of checkboxes. What determines the length of the list is the highlighted checkboxes. However, when I add more things to the list the checkboxes just get smaller to fit the panel. I've added a vertical scrollbar, but this just doesn't do anything. Is there something I have to do to stop the GridLayout from resizing what it holds or is it the wrong layout?
package darrt;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestForScrollBat {
public static void main(String[] args){
new TestForScrollBat();
}
public TestForScrollBat(){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(0, 1));
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JLabel label = new JLabel(" Soc Categories");
JCheckBox soc1 = new JCheckBox("Blood and Lymphatic System Disorder");
JCheckBox soc2 = new JCheckBox("Cardiac Disorders");
JCheckBox soc3 = new JCheckBox("Congenital, familial and Genetic Disorders");
JButton jbtn = new JButton("Go!");
panel.add(label);
panel.add(soc1);
panel.add(soc2);
panel.add(soc3);
panel.add(jbtn);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
I had it before so that it would add a scroll to this panel, but now it doesn't even do that.. It just creates a new JPanel on the JFrame
Your problem is about the following lines in your code:
scrollPane.setBounds(50, 30, 300, 50);
You should not set static sizes and locations when using layouts. You are telling a specific size and location to the scrollPane while you had add it to the center of the contentPane before. These two are in conflict.
And next problem is about this line:
frame.add(panel);
This line will detach the panel from you JScrollPane and add it directly to the contentPane of the JFrame.
By deleting/commenting these lines, your problem will be solved.
I would like to make a layout using Java Swing which looks like the following drawing.
(source: braun-abstatt.de)
On the left is a JPanel which is drawn through paintComponent() in a way that the graphics automatically scale when the window is resized. (The question isn't about that panel. That one's already done.)
Now I need some buttons (the black boxes, added in Photoshop for the drawing) to the right of the JPanel mentioned before. The height of the reddish areas at the top and bottom, next to which there should be just empty space, is calculated along the lines of CONSTANT_FACTOR * getHeight(). Next to each compartment on the left, there should be a group of buttons, lined up to the center of the respective compartment (see the blue lines).
The JPanel containing the buttons knows about the CONSTANT_FACTOR and the number of compartments, so it should be possible to feed this information into a layout manager.
Which layout manager would I best use to achieve this layout? I've read about all the different layout managers, but I can't quite figure out which one or which combination of them best fits in this case.
For example, by use of a different LayoutManager, a very easy and simple container, takes no more than 15-20 minutes:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class ThinLineFrame {
private JFrame frame = new JFrame();
private JScrollPane scrollPane;
private JPanel panel = new JPanel();
private JPanel panelNorth = new JPanel();
private JPanel panelCenter = new JPanel();
private JPanel panelCenterCh1 = new JPanel();
private JPanel panelCenterCh2 = new JPanel();
private JPanel panelCenterCh3 = new JPanel();
private JPanel panelCenterCh4 = new JPanel();
private JPanel panelCenterCh5 = new JPanel();
private JPanel panelSouth = new JPanel();
public ThinLineFrame() {
panelNorth.setBackground(Color.red.darker());
panelNorth.setPreferredSize(new Dimension(80, 30));
//
panelCenter.setBackground(Color.darkGray);
panelCenter.setLayout(new GridLayout(5, 1, 2, 2));
//
panelCenterCh1.setLayout(new BorderLayout());
JButton panelCenterCh1Button = new JButton();
panelCenterCh1.add(panelCenterCh1Button, BorderLayout.CENTER);
//
JButton panelCenterCh2Button1 = new JButton();
JButton panelCenterCh2Button2 = new JButton();
panelCenterCh2.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh2.add(panelCenterCh2Button1);
panelCenterCh2.add(panelCenterCh2Button2);
//
JButton panelCenterCh3Button1 = new JButton();
JButton panelCenterCh3Button2 = new JButton();
panelCenterCh3.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh3.add(panelCenterCh3Button1);
panelCenterCh3.add(panelCenterCh3Button2);
//
JButton panelCenterCh4Button1 = new JButton();
JButton panelCenterCh4Button2 = new JButton();
panelCenterCh4.setLayout(new GridLayout(2, 1, 2, 2));
panelCenterCh4.add(panelCenterCh4Button1);
panelCenterCh4.add(panelCenterCh4Button2);
//
panelCenterCh5.setLayout(new BorderLayout());
JButton panelCenterCh5Button = new JButton();
panelCenterCh5.add(panelCenterCh5Button, BorderLayout.CENTER);
//
panelCenter.add(panelCenterCh1);
panelCenter.add(panelCenterCh2);
panelCenter.add(panelCenterCh3);
panelCenter.add(panelCenterCh4);
panelCenter.add(panelCenterCh5);
//
panelSouth.setBackground(Color.red.darker());
panelSouth.setPreferredSize(new Dimension(80, 30));
//
panel.setLayout(new BorderLayout(2, 2));
panel.add(panelNorth, BorderLayout.NORTH);
panel.add(panelCenter, BorderLayout.CENTER);
panel.add(panelSouth, BorderLayout.SOUTH);
//
scrollPane = new JScrollPane(panel);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(80, 600));
frame.setLocation(100, 150);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ThinLineFrame dlg = new ThinLineFrame();
}
});
}
}
You should try looking at MigLayout. It's a super flexible LayoutManager that is also very simple.
The code would look something like:
MigLayout layout = new MigLayout("flowy");
panel.setLayoutManager(layout);
panel.add(button1);
panel.adD(button2);
etc..
Try adding debug, flowy to the constructor to get a visual idea of what is going on.
GBC without an anchor, just with plain vanilla GridBagConstraints and preferred size.
Centered JButton with fixed size:
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainWithFixSize {
public static void main(String[] argv) throws Exception {
JFrame frame = new JFrame();
Container container = frame.getContentPane();
GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
JButton component = new JButton();
component.setPreferredSize(new Dimension(25, 25));
gbl.setConstraints(component, gbc);
container.add(component);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(40, 90));
frame.pack();
frame.setVisible(true);
}
private MainWithFixSize() {
}
}