Using FlowLayout manager to keep 3 panels in distinct rows - java

I used a GridLayout mananger to create my gui, but I don't like the extra space that makes my buttons look tiny.
My GUI
I want it to look more like this one.
Good GUI
I have tried placing the first two rows in another gridlayout and putting it all together in a flow layout, but it did not work. They just end up being side by side, unless the window gets really small, as it should since it is a flowlayout. Any Ideas?
Standard Services Panel constructor
public StandardServices()
{
// Create GridLayout manager with 5 rows and 1 column
setLayout(new GridLayout(5,1));
// Create the check boxes.
iHardDrive = new JCheckBox("Install Hard Drive ($25.00)");
ram = new JCheckBox("Install Ram ($15.00)");
virus = new JCheckBox("Remove Virus ($50.00)");
fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
labourQuote = new JCheckBox("Hourly Labour Qoute ($10.00)");
//Add a border around the panel.
setBorder(BorderFactory.createTitledBorder("Standard Services"));
// Add the checkboxes to the panel.
add(iHardDrive);
add(ram);
add(virus);
add(fHardDrive);
add(labourQuote);
}
Hourly Services Panel constructor
public HourlyService()
{
// Created grid layout with 2 rows, 1 column
setLayout(new GridLayout(2,1));
// Create labels to display instructions.
cost = new JLabel("Parts Cost:");
labour = new JLabel("Hours of Labour:");
// Create two text fields 10 characters wide.
costTextField = new JTextField(10);
labourTextField = new JTextField(10);
// Place a 0 in the text fields.
costTextField.setText("0");
labourTextField.setText("0");
// Add a border around the layout
setBorder(BorderFactory.createTitledBorder("Hourly Service"));
// Add labels and text fields to the panel.
add(cost);
add(costTextField);
add(labour);
add(labourTextField);
}
LU Store GUI constructor
public MyStoreGui()
{
//Display a title
setTitle("LU Computer Store");
//Specify a default action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the window
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Create a GridLayout manager
setLayout(new GridLayout(3,1));
//setLayout(new FlowLayout());
// create custom panels
standard = new StandardServices();
hourly = new HourlyService();
//Create the button panel
buildButtonPanel();
add(standard);
add(hourly);
add(buttonPanel);
// Display the window
setVisible(true);
}

Instead of using a GridLayout which will give equal spacing to all the components based on the largest component, use a GridBagLayout which will not only provide you with more control, but which will honour the preferred size of the individual components
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyStoreGui());
frame.pack();
frame.setVisible(true);
}
});
}
public final class MyStoreGui extends JPanel {
public MyStoreGui() {
setLayout(new GridBagLayout());
//setLayout(new FlowLayout());
// create custom panels
StandardServices standard = new StandardServices();
HourlyService hourly = new HourlyService();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
//Create the button panel
JPanel buttonPanel = buildButtonPanel();
add(standard, gbc);
add(hourly, gbc);
add(buttonPanel, gbc);
// Display the window
setVisible(true);
}
protected JPanel buildButtonPanel() {
JPanel panel = new JPanel();
panel.add(new JButton("Calclate Changes"));
panel.add(new JButton("Exit"));
return panel;
}
}
public class StandardServices extends JPanel {
public StandardServices() {
// Create GridLayout manager with 5 rows and 1 column
setLayout(new GridLayout(5, 1));
// Create the check boxes.
JCheckBox iHardDrive = new JCheckBox("Install Hard Drive ($25.00)");
JCheckBox ram = new JCheckBox("Install Ram ($15.00)");
JCheckBox virus = new JCheckBox("Remove Virus ($50.00)");
JCheckBox fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
JCheckBox labourQuote = new JCheckBox("Hourly Labour Qoute ($10.00)");
//Add a border around the panel.
setBorder(BorderFactory.createTitledBorder("Standard Services"));
// Add the checkboxes to the panel.
add(iHardDrive);
add(ram);
add(virus);
add(fHardDrive);
add(labourQuote);
}
}
public class HourlyService extends JPanel {
public HourlyService() {
// Created grid layout with 2 rows, 1 column
setLayout(new GridLayout(2, 1));
// Create labels to display instructions.
JLabel cost = new JLabel("Parts Cost:");
JLabel labour = new JLabel("Hours of Labour:");
// Create two text fields 10 characters wide.
JTextField costTextField = new JTextField(10);
JTextField labourTextField = new JTextField(10);
// Place a 0 in the text fields.
costTextField.setText("0");
labourTextField.setText("0");
// Add a border around the layout
setBorder(BorderFactory.createTitledBorder("Hourly Service"));
// Add labels and text fields to the panel.
add(cost);
add(costTextField);
add(labour);
add(labourTextField);
}
}
}

Use the method pack() to remove unnecessary spaces..
Use it at as the last line.

Related

Set minimum width in side panel of Swing BorderLayout

I understand some parts of BorderLayout -- e.g., the EAST/WEST (or BEGINNING_OF_LINE/END_OF_LINE) panel component stays one width and its length is stretched with the length of the window.
I want to put a panel on the WEST side that itself has multiple components - a panel of buttons and a JList of things the buttons control, in this case. I would like to allocate a minimum width for the strings in that JList, but something (probably BorderLayout) prevents me from setting a minimum or preferred width.
When I run the code below, the list in the left panel is wide enough for "LongNameGame 3", but only because I added the string before rendering the list. I would like to set the width of that JList to accommodate strings of the width of my choice. Later I'll put it in a ScrollPane for strings wider than that, but that's a different problem.
My question is not answered by referring me to other layout managers -- I want to know how to do this with BorderLayout, if possible.
package comm;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class BLPlay
{
public static void main(String ... arguments)
{
JFrame frame = buildLoungeFrame();
frame.setVisible(true);
}
private static JFrame buildLoungeFrame()
{
JFrame loungeFrame = new JFrame("BLPlay");
loungeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loungeFrame.setLayout(new BorderLayout(10,10));
// left panel is another BorderLayout panel with buttons and a list of games
JPanel gameListControlPanel = new JPanel(new BorderLayout());
Border innerBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
Border outerBorder = BorderFactory.createEmptyBorder(3,3,3,3);
gameListControlPanel.setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
String[] gamePanelButtonLabels = { "New", "Join", "Leave", "End" };
JPanel gamePanelButtons = new JPanel(new GridLayout(gamePanelButtonLabels.length,1));
addButtons(gamePanelButtons, gamePanelButtonLabels);
JPanel gamePanelButtonsContainerPanel = new JPanel();
gamePanelButtonsContainerPanel.add(gamePanelButtons);
gameListControlPanel.add(gamePanelButtonsContainerPanel, BorderLayout.WEST);
Vector<String> gameList = new Vector<>();
gameList.add("Game 1");
gameList.add("Game 2");
gameList.add("LongNameGame 3");
JList<String> gameJList = new JList<>(gameList);
JPanel gameListPanel = new JPanel(new FlowLayout());
gameListPanel.setMinimumSize(new Dimension(600,600)); // <-- has no effect
gameListPanel.add(gameJList);
gameListControlPanel.add(gameListPanel, BorderLayout.EAST);
loungeFrame.add(gameListControlPanel, BorderLayout.WEST);
// center panel in the lounge is for chat messages; it has a separate border layout,
// center for accumulated messages, bottom for entering messages
JPanel chatMessagePanel = new JPanel(new BorderLayout());
// Border chatMessagePanelBorder = BorderFactory.createEmptyBorder(7,7,7,7);
// chatMessagePanel.setBorder(chatMessagePanelBorder);
JTextArea chatMessages = new JTextArea();
chatMessagePanel.add(chatMessages, BorderLayout.CENTER);
// debug
chatMessages.append("message one\n");
chatMessages.append("message two\n");
chatMessages.append("message three\n");
// and lower panel is for entering one's own chat messages
JPanel chatMessageEntryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField chatMessageEntryField = new JTextField(35);
JButton chatMessageEntryButton = new JButton("Enter");
chatMessageEntryPanel.add(chatMessageEntryField);
chatMessageEntryPanel.add(chatMessageEntryButton);
chatMessagePanel.add(chatMessageEntryPanel, BorderLayout.SOUTH);
loungeFrame.add(chatMessagePanel, BorderLayout.CENTER);
loungeFrame.pack();
return loungeFrame;
}
private static void addButtons(JPanel panel, String ... labels)
{
for (String label : labels)
{
JButton button = new JButton(label);
panel.add(button);
}
}
}
Give the JList a prototype cell value that is wide enough to display what you need. e.g.,
gameJList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
The prototype value (here a String because the list has been declared as a JList<String>) is used to set the list's preferred size, but is not displayed in the JList. You can use as large or small a list as you need. Also be sure to set visible row count for the same purpose in the horizontal dimension:
gameJList.setVisibleRowCount(20); // for example

Forcing minimum JPanel size

I'm trying to code a layout for a small phonebook app using Java Swing. I came across a problem with size I cannot fix. I want filters (displayed under search results) to be wide enough to show whole title when search phrase is short. Here's what it looks like:
As you can see name is shortened. Surname is carrying longer text and is displayed just as I want.
Filter class:
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
public class Filter
{
private JLabel label;
private JPanel filterPane;
Filter(String name)
{
// Filter text
label = new JLabel();
label.setForeground(new Color(0xAA0000));
// Closing button
JButton closeButton = new JButton("X");
closeButton.setFont(new Font("Dialog", Font.BOLD, 20));
closeButton.setMargin(new Insets(0, 0, 0, 0));
filterPane = new JPanel();
filterPane.setLayout(new BoxLayout(filterPane, BoxLayout.X_AXIS));
// Frame with title + 50px padding on the right side for the next filter
filterPane.setBorder(new CompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 0, 50),
BorderFactory.createTitledBorder(name)));
filterPane.add(closeButton);
filterPane.add(Box.createRigidArea(new Dimension(5, 0)));
filterPane.add(label);
filterPane.setVisible(false);
}
public void setValue(String value) {
if (value == null || value.isEmpty())
filterPane.setVisible(false);
else
{
this.label.setText(value);
filterPane.setVisible(true);
}
}
public JPanel getFilterPane() {
return filterPane;
}
}
Main class:
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
public class TestWindow
{
private static void init()
{
JFrame frame = new JFrame("Stackoverflow test window");
frame.setSize(new Dimension(480, 320));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// Main panel - 20 px padding from each side
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Create panel for filters
JPanel filterPane = new JPanel();
filterPane.setLayout(new BoxLayout(filterPane, BoxLayout.X_AXIS));
filterPane.setPreferredSize(new Dimension(0, 60));
// Add sample filters
Filter filter = new Filter("name");
filter.setValue("i");
filter.getFilterPane().setAlignmentY(Component.BOTTOM_ALIGNMENT);
filterPane.add(filter.getFilterPane());
filter = new Filter("surname");
filter.setValue("loooooong text");
filter.getFilterPane().setAlignmentY(Component.BOTTOM_ALIGNMENT);
filterPane.add(filter.getFilterPane());
// Add everything to main panel
pane.add(new JTextArea("Nothing fancy here, just to fill out space"), BorderLayout.CENTER);
pane.add(filterPane, BorderLayout.SOUTH);
frame.setContentPane(pane);
frame.setVisible(true);
}
public static void main (String [] args) throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeLater(TestWindow::init);
}
}
I tried setMinimumSize and setPreferredSize both for label and filterPane in Filter constructor, but it didn't help. Could you help me please?
The problem is actually composed of two problems.
When using TitledBorder, the component does not take the size of the title into account when calculating its preferred size.
When using BoxLayout, the preferred width is ignored, unless you add "horizontal glue" after the component.
So, to fix the first problem, you need to override the getPreferredSize() method of JPanel. But for this to work, you also need to bypass the second problem by adding glue.
To override the getPreferredSize(), you can use something like:
class MinSizePanel extends JPanel {
public Dimension getPreferredSize() {
Dimension original = super.getPreferredSize();
TitledBorder titleBorder = (TitledBorder)((CompoundBorder)getBorder()).getInsideBorder();
int width = (int)Math.max( original.getWidth(), 60 + (int)titleBorder.getMinimumSize(this).getWidth());
return new Dimension( width, (int)original.getHeight() );
}
}
This gets the TitledBorder from the CompoundBorder, gets its minimum size (this is a method that takes the title width into consideration), adds some extra for the empty border and insets (you can add the appropriate calculations, I did a shortcut and just used 60...), and uses that (if it's more than the width of the component as-is).
Instead of
filterPane = new JPanel();
Use
filterPane = new MinSizePanel();
(You should really move all the rest of the construction of the panel into that class as well).
And in the TestWindow class, after the last
filterPane.add(filter.getFilterPane());
Also add
filterPane.add(Box.createHorizontalGlue());

Java Swing UI Layout

I am creating a basic user interface in Swing and was hoping for some help. Below is a screenshot of what I am trying to achieve:
My code currently is as follows:
package testui;
import java.awt.Container;
import javax.swing.*;
public class TestUI{
private JTextField outputArea = new JTextField();
private JTextField errorReportArea = new JTextField();
private JPanel inputPanel = new JPanel();
private JLabel nameLabel = new JLabel("Item Name");
private JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private JLabel priceLabel = new JLabel("Price per unit (Or L) in pence");
private JTextField nameField = new JTextField(10);
private JTextField numberField = new JTextField(10);
private JTextField priceField = new JTextField(10);
private JButton addVolumeButton = new JButton("Add by Volume");
private JButton addNumberButton = new JButton("Add by number of units");
public TestUI() {
JFrame frame = new JFrame("Fuel Station");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputArea.setEditable(false);
errorReportArea.setEditable(false);
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(addVolumeButton);
inputPanel.add(addNumberButton);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(outputArea);
contentPane.add(errorReportArea);
contentPane.add(inputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
TestUI test1 = new TestUI();
}
}
Which looks like this:
So what I would like to do is set a specific size for the top two JTextFields, as the top one will contain multiple lines of text, and the one below will contain just one line of text. I am unsure how to do this without using setSize, as I have been told it is bad coding practice to use this.
I would also like to add some padding between the JLabels, JTextFields and JButtons in the bottom JPanel.
If anyone could give me some suggestions on resizing these components I would be most grateful
Since you want your textfields to be multilined, use JTextAreas. JTextFields are single lined only.
Your components are right next to each other which isn't the same look as your intended outcome. There may be some method that gives your components some breathing room before you would call frame.pack()
Look for any method that can make a component fill the total amount of room it's given; especially when you want something to fill a large chunk of space.
You can set the number of columns instead of using setSize() for your JTextFields/JTextAreas. Just saying.
Reviewing all of Java's Layout Managers would help you get a grasp of the capabilities and use cases for each layout manager
There are a few layout managers that are flexible enough to perform this, such as Mig, Gridbag, and SpringLayout.
In your case, you'd have the following constraints:
outputarea - south border constrained to be ###px from the north border of the contentPane
errorReportArea - north border constrained to be 0px from outputarea's south, and south border constrained to be 0px from inputPanel's north.
inputPanel - north border constrained to be ##px from the south border of the contentPane.
GUI builders such as WindowBuilder will allow you to do this pretty quickly. You just drop in the layout onto the contentPane and then set the constraints.
If you have to use a box layout look at the glue and rigidArea methods in Box. If you can use other layouts, go with those suggested by the other answers.
I have created a solution with the MigLayout manager.
Here are some recommendations:
Put application code outside the constructor; in the solution, the code
is placed in the initUI() method.
The application should be started on EDT by calling the
EventQueue.invokeLater(). (See the main() method of the provided solution.)
Use a modern, flexible layout manager: MigLayout, GroupLayout, or FormLayout.
Take some time to study them to fully understand the layout management process. It
is important have a good understanding of this topic.
Shorten the labels; use more descriptive tooltips
instead.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutSolution extends JFrame {
private JTextArea outputArea;
private JTextField errorReportField;
private JLabel nameLabel;
private JLabel numberLabel;
private JLabel priceLabel;
private JTextField nameField;
private JTextField numberField;
private JTextField priceField;
private JButton addVolumeButton;
private JButton addNumberButton;
public MigLayoutSolution() {
initUI();
}
private void initUI() {
setLayout(new MigLayout());
outputArea = new JTextArea(10, 20);
errorReportField = new JTextField(15);
nameLabel = new JLabel("Item name");
numberLabel = new JLabel("# of units");
numberLabel.setToolTipText("Number of units (or Volume in L)");
priceLabel = new JLabel("Price per unit");
priceLabel.setToolTipText("Price per unit (Or L) in pence");
nameField = new JTextField(10);
numberField = new JTextField(10);
priceField = new JTextField(10);
addVolumeButton = new JButton("AddVol");
addVolumeButton.setToolTipText("Add by Volume");
addNumberButton = new JButton("AddNum");
addNumberButton.setToolTipText("Add by number of units");
add(new JScrollPane(outputArea), "grow, push, wrap");
add(errorReportField, "growx, wrap");
add(nameLabel, "split");
add(nameField);
add(numberLabel);
add(numberField);
add(priceLabel);
add(priceField);
add(addVolumeButton);
add(addNumberButton);
pack();
setTitle("Fuel station");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MigLayoutSolution ex = new MigLayoutSolution();
ex.setVisible(true);
}
});
}
}

How can I place buttons in the center of the frame in a vertical line?

I will place these buttons in the center of the frame and above each other, like this.
BUTTON
BUTTON
BUTTON
I've searched multiple topics on this forum but everything I tried didn't work for so far. I hope that somebody has the solution.
This is my code for so far:
package ípsen1;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Paneel extends JPanel implements ActionListener {
Image achtergrond;
private JButton spelHervatten;
private JButton spelOpslaan;
private JButton spelAfsluiten;
public Paneel(){
//buttons
spelHervatten = new JButton("Spel hervatten");
spelHervatten.setPreferredSize(new Dimension(380, 65));
spelOpslaan = new JButton("Spel opslaan");
spelOpslaan.setPreferredSize(new Dimension(380, 65));
spelAfsluiten = new JButton("Spel afsluiten");
spelAfsluiten.setPreferredSize(new Dimension(380, 65));
//object Paneel luistert naar button events
spelAfsluiten.addActionListener(this);
add (spelHervatten);
add (spelOpslaan);
add (spelAfsluiten);
}
public void paintComponent(Graphics g) {
//achtergrond afbeelding zetten
achtergrond = Toolkit.getDefaultToolkit().getImage("hout.jpg");
//screensize
g.drawImage(achtergrond, 0,0, 1024,768,this);
}
//actie na klik op button
public void actionPerformed(ActionEvent e) {
if(e.getSource() == spelAfsluiten){
System.out.println("Spel afsluiten");
System.exit(0);
}
}
}
You could use a GridBagLayout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);
add(new JButton("Button"), gbc);
See How to Use GridBagLayout for more details
A BoxLayout might be what you're after. You can specify that you want to add components along the y-axis in the constructor for that particular layout manager.
You could add this line to the constructor of your Paneel class.
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
As for center-aligning everything, I don't know if it's good practice but you can set the horizontal alignment for each of your buttons individually. Example:
spelHervatten.setAlignmentX(CENTER_ALIGNMENT);
Uses a GridLayout for a single column of buttons of equal width.
The buttons stretch as the window's size increases. To maintain the button size, put the GridLayout as a single component into a GridBagLayout with no constraint. It will be centered.
The size of the buttons is increased by setting a margin.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
/*
* Uses a GridLayout for a single column of buttons of equal width.
* The buttons stretch as the window's size increases. To maintain
* the button size, put the GridLayout as a single component into a
* GridBagLayout with no constraint. It will be centered.
*/
public class CenteredSingleColumnOfButtons {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new GridLayout(0,1,10,10));
gui.setBorder(new EmptyBorder(20,30,20,30));
String[] buttonLabels = {
"Spel hervatten",
"Spel opslaan",
"Spel afsluiten"
};
Insets margin = new Insets(20,150,20,150);
JButton b = null;
for (String s : buttonLabels) {
b = new JButton(s);
b.setMargin(margin);
gui.add(b);
}
JFrame f = new JFrame("Centered Single Column of Buttons");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
f.setMinimumSize(f.getSize());
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
System.out.println(b.getSize());
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
I thought there is no way to do that.
You should get size of Panel/Frame then calculate manually to find to center position for your button.
Rephrased some parts:
You might want to try to put the buttons in JFrame's "wind direction"-style BorderLayout:
http://www.leepoint.net/notes-java/GUI/layouts/20borderlayout.html
Just create a block in the CENTER with one EAST and WEST block with a certain size around it. Then insert the buttons inside of the center block. If you don't want them to be the full size, just add another EAST and WEST.

JLabel alignment in JPanel

I'm trying to align a JLabel to the right in a JPanel. I'm adding a JTabbedPane, a JPanel which contains my JLabel and JTextArea to a main JPanel.
I have searched SO and tried some methods like setAlignmentX, setHorizontalAlignment(SwingConstants.LEFT) and nested containers to no avail.
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelProblem
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Main = new JPanel();
Main.setLayout(new BoxLayout(Main, BoxLayout.Y_AXIS));
JPanel ComponentPanel = new JPanel();
JLabel label = new JLabel("Sample Text");
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setAlignmentX(Component.RIGHT_ALIGNMENT);
ComponentPanel.add(label);
JTabbedPane Tab = new JTabbedPane();
Tab.add("Document 1", new JPanel());
Main.add(Tab);
Main.add(ComponentPanel);
JTextArea Area = new JTextArea(10,10);
JScrollPane Scroll = new JScrollPane(Area);
frame.add(Main);
frame.add(Scroll, BorderLayout.SOUTH);
frame.setSize(450,450);
frame.setVisible(true);
}
}
How can I align my JLabel to the right?
Thanks!
So, the place of that label is determined by the layout of ComponentPanel. Since you didn't specify any layout it is using the default FlowLayout with a CENTER alignment. Assuming that you are ok with a FlowLayout it is a mere question of setting the alignment of the LEFT since this is possible with this layout.
Here's the code with the fix, however I suspect that as you put more elements to the ComponentPanel you will want to use another layout since FlowLayout is more adequate for menus and the like and not for displaying the main content.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
class LabelProblem
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
initGUI();
}
});
}
public static void initGUI()
{
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
JPanel componentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel label = new JLabel("Sample Text");
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
componentPanel.add(label);
JTabbedPane Tab = new JTabbedPane();
Tab.add("Document 1", new JPanel());
main.add(Tab);
main.add(componentPanel);
JTextArea area = new JTextArea(10, 10);
JScrollPane scroll = new JScrollPane(area);
frame.add(main);
frame.add(scroll, BorderLayout.SOUTH);
frame.setSize(450, 450);
frame.setVisible(true);
}
}
Result:
Note: I also changed the variable names to follow the java style convention: variable names should start with lower case to differenciate them from clases names, starting in upper case.
One simple approach is to set the label's horizontalAlignment to JLabel.RIGHT in the constructor.
import java.awt.*;
import javax.swing.*;
class LabelProblem {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1));
JTabbedPane tab = new JTabbedPane();
tab.add("Document 1", new JPanel());
frame.add(tab);
JLabel label = new JLabel("Sample Text", JLabel.RIGHT);
frame.add(label);
JTextArea area = new JTextArea(10, 10);
JScrollPane scroll = new JScrollPane(area);
frame.add(scroll);
frame.pack();
frame.setSize(450, 450);
frame.setVisible(true);
}
}
I think it may be a matter of you not actually setting layouts where you imagine you're setting layouts.
You have a JPanel with a vertically oriented BoxLayout (Main) enclosing another JPanel with default layout (ComponentPanel), finally enclosing your label. The reason why your label can't be pushed to the right is because is already is pushed to the right within it's enclosing container. If you set a colored border around ComponentPanel, you'll see what I mean -- it only occupies the same amount of space as the JLabel, giving the JLabel nowhere to move.
You need to set a layout and constraints for your intermediate ComponentPanel, allowing it to horizontally fill its parent container so that the label has someplace to go.
You haven't really specified how your layout is supposed to look, but if you change the layout on Main to X_AXIS, your label will pop over to the left (as will its parent container). Without knowing what you're really trying to do, I can't say much more.
I would however, suggest you throw your BoxLayout away entirely and look into using GridBagLayout, which gives you a high level control over your UI. GridBagLayout isn't the most concise construct, but that's the price of control.

Categories

Resources