I want to position a JButton "Start" in the lower center of my GUI.
Sadly the GridBagConstraints that I set seem to have no effect.
I'm working with jdk1.8.0_191 and the Eclipse IDE.
EDIT: I switched my snippet to a "minimal reproducible example".
Here the snippet of my code:
package gui.main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(646, 509);
frame.setResizable(false);
frame.setTitle("Adventure Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 5;
JButton button = new JButton("Start");
button.setBackground(Color.black);
button.setForeground(Color.white);
button.setPreferredSize(new Dimension(110, 60));
button.setMinimumSize(new Dimension(110, 60));
button.setVisible(true);
panel.add(button, gbc);
panel.setVisible(true);
frame.add(panel);
frame.setVisible(true);
}
}
Here is a picture where I approximately want the "Start" Button instead.
I created a simple, runnable example of a GUI with a start button on the lower fourth of the JPanel. Here's the example GUI.
Use Swing components. Don't extend Swing components, or any other Java class, unless you intend to override one of the methods in the class.
I used a BorderLayout for the JPanel. I used a calculated empty border to shift the start button down from the center.
I set the size of the JPanel in this example. I'm assuming you'll set the size of the game panel based on the size of your image.
Here's the code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StartButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StartButtonExample());
}
#Override
public void run() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
Dimension panelSize = new Dimension(400, 400);
panel.setPreferredSize(panelSize);
JButton button = new JButton("Start");
Dimension buttonSize = button.getPreferredSize();
int height = panelSize.height - buttonSize.height;
int width = panelSize.width - buttonSize.width;
int top = height * 3 / 4;
int left = (width - buttonSize.width) / 2;
int bottom = height / 4;
int right = left;
panel.setBorder(BorderFactory.createEmptyBorder(
top, left, bottom, right));
panel.add(button, BorderLayout.CENTER);
return panel;
}
}
In my amateurish opinion:
I ended up using different insets for my small application.
It is more clean looking in my opinion.
//margin 400 top, rest 3
gbc.insets = new Insets(400,3,3,3);
gbc.gridy = 0;
getWrapperPanel().add(getBtnStart02(), gbc);
//margin 3 at all sides for all following gui objects
gbc.insets = new Insets(3,3,3,3);
gbc.gridy = 1;
getWrapperPanel().add(getBtnLoad(), gbc);
Works like a charm.
Related
I tried create some window application. I have container which consist a JFrame and two JPanel (white square and red square. The last square inside white square).
I want change position red square ( any place on my work window( for example, left or right side)). I tried do it, but i didn't have success.
Could you help me?
It's my code)
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main_window extends JFrame {
public static void main(String\[\] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
frame.getContentPane().setLayout(new GridLayout(1, 1));
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
frame.getContentPane().add(panel);
JPanel panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(200, 200));
panel_1.setBackground(Color.red);
panel.add(panel_1);
frame.setSize(800,800);
frame.setVisible(true);
}
}
GridBagLayout will give you the greatest amount of control over the layout itself.
For example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBackground(Color.WHITE);
setLayout(new GridBagLayout());
JPanel pane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
};
pane.setBackground(Color.RED);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
// Left
//gbc.anchor = GridBagConstraints.WEST;
// Right
//gbc.anchor = GridBagConstraints.EAST;
// Top
//gbc.anchor = GridBagConstraints.NORTH;
// Bottom
//gbc.anchor = GridBagConstraints.SOUTH;
// Top/left
//gbc.anchor = GridBagConstraints.NORTHWEST;
// Top/Right
//gbc.anchor = GridBagConstraints.NORTHEAST;
// Bottom/left
//gbc.anchor = GridBagConstraints.SOUTHHWEST;
// Bottom/Right
//gbc.anchor = GridBagConstraints.SOUTHEAST;
// Middle
gbc.anchor = GridBagConstraints.CENTER;
add(pane, gbc);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
It's also the most complex layout manager, so it might take some time and experimentation to get it just right.
Start by having a look at How to Use GridBagLayout
You can use FlowLayout to control the position of the red square.I have set layout to the outer panel with white background to control the position of the inside panel panel_1 which is the red square.Check my answer.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
frame.getContentPane().add(panel);
JPanel panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(200, 200));
panel_1.setBackground(Color.red);
panel.add(panel_1);
//FlowLayout.RIGHT, LEFT, CENTER
panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(800,800);
frame.setVisible(true);
You need to perform some action in order to move the square e.g. I have added a button in my code which when clicked, will cause the square to move. For the button to perform some action, you need to implement ActionListener. As you can see in the actionPerformed method, I am generating random x and y coordinates of the top left corner of the rectangle and setting new coordinates by using the method, setBounds. Note that I have maintained the same width and height of the rectangle when it is moved to new coordinates.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainWindow extends JFrame implements ActionListener {
JPanel panel, panel_1;
JButton btnMoveSquare;
Random random;
MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.black);
getContentPane().setLayout(new GridLayout(1, 1));
panel = new JPanel();
panel.setBackground(Color.WHITE);
getContentPane().add(panel);
panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(200, 200));
panel_1.setBackground(Color.red);
panel.add(panel_1);
btnMoveSquare = new JButton("Move Square");
btnMoveSquare.addActionListener(this);
panel.add(btnMoveSquare);
setSize(800, 800);
random = new Random();
}
#Override
public void actionPerformed(ActionEvent e) {
int x = random.nextInt(800);
int y = random.nextInt(800);
panel_1.setBounds(x, y, panel_1.getWidth(), panel_1.getHeight());
}
public static void main(String[] args) {
new MainWindow().setVisible(true);
}
}
Feel free to comment if you have any doubt with the code.
I want to align my two of my components to the top left corner of the window.
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
public MainFrame() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gridbagPanel = new JPanel();
this.setLayout(new BorderLayout());
gridbagPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel nameLabel = new JLabel(player.getName());
nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
nameLabel.setFont(new Font("Serif",Font.PLAIN,24));
mainPanel.add(nameLabel, BorderLayout.NORTH);
JLabel money = new JLabel("Pinigai: "+new Integer(player.getMoney()).toString());
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.PAGE_START;
gc.insets = new Insets(2,0,0,2);
gridbagPanel.add(money,gc);
JLabel job = new JLabel("Darbas: "+new Integer(player.getSkin()).toString());
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(2,0,0,2);
gc.anchor = GridBagConstraints.LINE_START;
gridbagPanel.add(job, gc);
mainPanel.setBorder(new EmptyBorder(10,10,10,10));
mainPanel.add(gridbagPanel,BorderLayout.WEST);
add(mainPanel);
getContentPane().revalidate();
}
}
It currently looks like this:
And I would like the lines with numbers in the top left corner.
Note that I'm aware that both JFrame("this" class) and the mainPanel are using BorderLayouts.
Another non-related question: When should I create a new GridBagConstraints object? Why can't I just store one in a private instance variable for all usage needed?
The beauty of GridBagLayout, is that column/row sizes are not fixed (ie, not every row/column has to be the same size, like in GridLayout)
You can "encourage" certain components to occupy more space within their given area then others.
Things like weightx and weighty describe how much of the available space a row/column should occupy. Add in the NORTHWEST anchor constraint and you should begin to see the desired effect.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public MainFrame() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gridbagPanel = new JPanel();
this.setLayout(new BorderLayout());
gridbagPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel nameLabel = new JLabel("Bebras");
nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
nameLabel.setFont(new Font("Serif", Font.PLAIN, 24));
mainPanel.add(nameLabel, BorderLayout.NORTH);
JLabel money = new JLabel("Pinigai: " + new Integer(66484).toString());
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.NORTHWEST;
gc.insets = new Insets(2, 0, 0, 2);
gridbagPanel.add(money, gc);
JLabel job = new JLabel("Darbas: " + new Integer(126).toString());
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(2, 0, 0, 2);
gc.anchor = GridBagConstraints.NORTHWEST;
gc.weightx = 1;
gc.weighty = 1;
gridbagPanel.add(job, gc);
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
mainPanel.add(gridbagPanel, BorderLayout.WEST);
add(mainPanel);
getContentPane().revalidate();
}
}
Normally, I would add a "filler" component into the component, using it to push all the other components to where I want them, but this will come down to what it is you want to achieve.
Take a look at How to use GridBagLayout for more details
Im more of a fan of Box Layouts to achieve this type of static positioning in swing. see the example below:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class main extends JFrame {
public static void main(String[] args) throws InterruptedException {
main m = new main();
m.setVisible(true);
}
public main() {
// setup stuff
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(100, 100, 500, 500);
// this is the panel I will add to the frame
JPanel innerPanel = new JPanel();
// give it a Y axis to stuff is added top to bottom
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
// this is a temp panel ill used to add the labels
JPanel tPanel = new JPanel();
// its an x axis to add stuff left to right
tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
// create and add a label to the temp panel
JLabel label = new JLabel("Some text");
tPanel.add(label);
// use our stretchy glue to fill the space to the right of the label
tPanel.add(Box.createHorizontalGlue());
// add the temp panel to the inner panel
innerPanel.add(tPanel);
// create a spacer with 0 width and 10 height
innerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
// reinitialize the temp panel for another label
tPanel = new JPanel();
tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
label = new JLabel("Some other text");
// add the other label to the temp panel
tPanel.add(label);
// more stretchy glue
tPanel.add(Box.createHorizontalGlue());
// add the temp panel
innerPanel.add(tPanel);
// add verticle stretchy glue to fill the rest of the space below the
// labels
innerPanel.add(Box.createVerticalGlue());
// add to the frame
this.add(innerPanel);
}
}
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ships {
public static JPanel init(JPanel radioPanel){
radioPanel.add(addShips(2));
radioPanel.add(addShips(3));
radioPanel.add(addShips(4));
radioPanel.add(addShips(5));
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
but.setPreferredSize(new Dimension((40*size),40));
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setVisible(true);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); \\line 4
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
I see that the buttons are placed in one single line. Need to place the buttons one after the other according to BoxLayout.Y_AXIS. When I remove //line 4, it creates correctly according to FlowLayout.
Try this:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ships {
public static JPanel init(JPanel radioPanel){
radioPanel.add(addShips(2));
radioPanel.add(addShips(3));
radioPanel.add(addShips(4));
radioPanel.add(addShips(5));
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
Dimension d = new Dimension((40*size),40);
but.setPreferredSize(d);
but.setMinimumSize(d);
but.setMaximumSize(d);
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); //line 4
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
When you pack components in JFrame, your layout manager may not honour preferred size of components, try setting minimum and maximum sizes too.
In vertical layout (y-axis), BoxLayout tries to make all components wide as widest component. As there is no text or icon in all buttons, button will shrink to default size, and all will have same width. So instruct box layout for particular sizes using maximum and minimum sizes.
I have changed your LayoutManager to GridBagLayout and it works fine. Is it suitable for you? :
public class Ships {
public static JPanel init(JPanel radioPanel){
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
radioPanel.add(addShips(2),c);
c.gridy = 1;
radioPanel.add(addShips(6),c);
c.gridy = 2;
radioPanel.add(addShips(4),c);
c.gridy = 3;
radioPanel.add(addShips(5),c);
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
but.setPreferredSize(new Dimension((40*size),40));
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setVisible(true);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridBagLayout());
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
EDIT: change horizontal to vertical align.
Result:
I want to have my screen split in two so I used a BorderLayout with East and West sections. I had problems resizing and here I eventually found out that width is not changed in the East and West panels and height is not changed in the North and South panels and both are changed in the Center panel.
However, I want both width and height to be changed upon resize, and have two panels side by side. I have tried various levels of nesting to try getting it to work but I do not think it will work with BorderLayout.
It seems like this should be easy for the default layout manager but maybe I should try a different layout (e.g. BoxLayout) to achieve what I want.
Also here is some code which replicates the problem I am talking about (try resizing the window):
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
public static void main(String[] args) {
JFrame window = new Main();
window.setVisible(true);
}
public Main() {
JButton east = new JButton("East");
JButton west = new JButton("West");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(east, BorderLayout.EAST);
content.add(west, BorderLayout.WEST);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Edit: I do not want the two sides to be equal, roughly 2:1 is the ratio which I want.
What you can use in your case is GridLayout, here two JButtons will resize themselves as the JFrame resizes.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame window = new Main();
window.setVisible(true);
}
});
}
public Main() {
JButton east = new JButton("East");
JButton west = new JButton("West");
JPanel content = new JPanel();
content.setLayout(new GridLayout(1, 2));
content.add(east);
content.add(west);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Moreover, it's always best to run your GUI related code from the EDT - Event Dispatch Thread, and not from the Main Thread. Do read Concurrency in Swing, for more info on the topic.
LATEST EDIT : As per requested comment
Use GridBagLayout to specify the size that you want to give
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
JFrame window = new Main();
window.setVisible(true);
}
});
}
public Main() {
JPanel east = new JPanel();
east.setOpaque(true);
east.setBackground(Color.WHITE);
JPanel west = new JPanel();
west.setOpaque(true);
west.setBackground(Color.BLUE);
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.3;
gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
content.add(east, gbc);
gbc.weightx = 0.7;
gbc.gridx = 1;
content.add(west, gbc);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Why don't you try with JSplitPane:
import javax.swing.*;
import java.awt.*;
public class AppDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, eastButton, westButton);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(splitPane, BorderLayout.CENTER);
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 400));
frame.pack();
frame.setVisible(true);
});
}
}
You will get this:
If you want to keep your BorderLayout you can use something like the following object:
public class ResizablePanel extends JPanel {
public ResizablePanel(JComponent body) {
setLayout(new BorderLayout());
JButton resize = new JButton();
resize.setPreferredSize(new Dimension(Integer.MAX_VALUE, 4));
resize.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
Dimension preferredSize = ResizablePanel.this.getPreferredSize();
ResizablePanel.this.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height-e.getY()));
ResizablePanel.this.revalidate();
}
});
add(resize, BorderLayout.PAGE_START);
add(body, BorderLayout.CENTER);
}
}
Now wrap the part you want to resize with an instance of ResizablePanel and you'll be able to resize it by dragging the thin button.
Note that this is code is for resizing the height of a panel that you put at the bottom (PAGE_END) part of a border layout, but it should be fairly straightforward to change it for resizing the width.
Sorry about replying to an old post.
My fix is to still use BorderLayout but to throw in the following line after the Component is resized
getLayout().layoutContainer(this);
I'm trying to figure out how to create a vertical TitledBorder in a JPanel.
I've got this situation:
I'd like to have "Actuators st..." placed vertically, so user can read it.
Is there a way to do it, or should I implement my own customized JPanel & TitledBorder?
maybe crazy idea but is possible with JSeparator too :-)
required proper LayoutManager, maybe GridBagLayout (JComponent placed without GBC can take PreferrredSize from JComponent, but isn't resiziable), not GridLayout
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
public class NestedLayout {
private JFrame frame = new JFrame();
private JPanel leftPanel = new JPanel();
private JSeparator sep = new JSeparator();
private JLabel label = new JLabel("<html> L<br>a<br>b<br>e<br>l<br></html>");
public NestedLayout() {
label.setOpaque(true);
sep.setOrientation(JSeparator.VERTICAL);
sep.setLayout(new GridLayout(3, 1));
sep.add(new JLabel());
sep.add(label);
sep.add(new JLabel());
leftPanel.setLayout(new BorderLayout());
leftPanel.setBorder(BorderFactory.createEmptyBorder(
10, //top
10, //left
10, //bottom
10)); //right
leftPanel.add(sep, BorderLayout.CENTER);
leftPanel.setPreferredSize(new Dimension(40, 220));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(leftPanel, BorderLayout.WEST);
//frame.add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NestedLayout nestedLayout = new NestedLayout();
}
});
}
}
As shown in How to Use Borders, you can create a compound border using an empty border and a titled border.
Addendum: As an alternative, you can use the border's getMinimumSize() method to ensure that the title is visible. See also this related Q&A.
f.add(createPanel("Actuator status"), BorderLayout.WEST);
f.add(createPanel("Indicator result"), BorderLayout.EAST);
...
private Box createPanel(String s) {
Box box = new Box(BoxLayout.Y_AXIS);
TitledBorder title = BorderFactory.createTitledBorder(null, s,
TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
box.setBorder(title);
for (int i = 0; i < 6; i++) {
JButton b = new JButton(null, UIManager.getIcon("html.pendingImage"));
b.setAlignmentX(JButton.CENTER_ALIGNMENT);
box.add(b);
}
box.validate();
Dimension db = box.getPreferredSize();
int max = Math.max(title.getMinimumSize(box).width, db.width);
box.setPreferredSize(new Dimension(max, db.height));
return box;
}