I feel I need to rephrase the question a bit.
Updated question below.
I have a JPanel that contains:
myjpanel.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));
It contains the following three panels:
JPanel with fixed size 'x' and 'y'
JPanel with no fixed size
JPanel with no fixed size and small height
The second JPanel contains a JTable so it expands to fill the full height and pushes the bottom panel all the way down, as expected.
Like this:
t
t
m
m
m
m
m
b
t = top panel,
m = middle panel,
b = bottom panel.
That works. But the bottom panel does not feel like filling the entire width of the parent which is a problem.
ttt
mmm
b
I would like either this, where the panel fills the entire width:
ttt
mmm
bbb
or this, where the bottom panel is left centered:
ttt
mmm
b
Old question below:
I have a JPanel that contains:
.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));
Within it, there are three more JPanels. The first two are of fixed size and the middle one isn't.
I want my bottom panel to take only the height it needs, but uses all the available width of the outer JPanel.
I have tried using glue but to no avail, and I would rather not set preferred and min/max sizes.
Is there a way to tell the component to "Fill the entire parents width" using just the layout manager and framework. I would rather not start to do hacks like setting sizes and overriding methods.
Note: I can't put any glue or filler in the inner panel, only the outer panel and its layout manager can be modified.
Attempt 1:
Using myPanel.setLayout(new GridLayout(3, 1)); did not produce the expected results. It made a grid like this:
XX
X
But I expected:
X
X
X
Attempt 2:
Using myPanel.setLayout(new GridLayout(0,1)); did not produce the expected results. It made a grid like this:
x
x
x
But all panels were of the same size, ignoring the restraints.
The easiest way would be to use another layout manager such as GridLayout that automatically sizes components to fill the parent container.
myPanel.setLayout(new GridLayout(0, 1));
If using a BorderLayout and the b panel is in the SOUTH or PAGE_END it does fill the entire width.
You can use GridBagLayout, for that, using
gridBagObject.fill = GridBagConstraints.HORIZONTAL
One example for your help, relating to GridBagLayout.
As asked in comments, related to that
The BoxLayout is another alternative, that respects the preferred sizes of the components. You can try that if GridBagLayout is that tough :-)
Code with GridBagLayout, for more clarity :
import javax.swing.*;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 1/10/13
* Time: 7:43 PM
*/
public class GridBagExample
{
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CustomPanel topPanel = new CustomPanel(Color.BLUE.darker().darker());
CustomPanel middlePanel = new CustomPanel(Color.CYAN.darker().darker());
CustomPanel bottomPanel = new CustomPanel(Color.DARK_GRAY);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 0.3;
contentPane.add(topPanel, gbc);
gbc.gridy = 1;
contentPane.add(middlePanel, gbc);
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
contentPane.add(bottomPanel, gbc);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new GridBagExample().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
public CustomPanel(Color backGroundColour)
{
setOpaque(true);
setBackground(backGroundColour);
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(200, 150));
}
}
OUTPUT :
Here some custom JPanel is used to expanding and shrinking with static input values.
Java timer is using to Delay and speed up the panel display time.
public class SlidingPanel extends JPanel {
private int paneHeight;
private int paneWidth;
private int comHeight;
private int comWidth;
private Timer everyspeedmillisec;
/**custom class for slide up and down actions for JPanel
*
*/
public SlidingPanel(LayoutManager layout,Dimension panedim) {
super(layout);
setPreferredSize(panedim);
paneHeight = (int)panedim.getHeight();;
paneWidth = (int) panedim.getWidth();
}
/**function for expanding Jpanel
*
*/
public void expand_sft_det(int speed ,JPanel slidingcom)
{
//Add Jpanel for sliding
this.add(slidingcom);
//Get height and width for panel ,that what we want to display as slide height.
comHeight=(int)slidingcom.getMinimumSize().getHeight();
comWidth=(int) slidingcom.getMinimumSize().getWidth();
//Intializing timer with some static values - Medium speed)
everyspeedmillisec = new Timer(30, new ActionListener() {
private int count_timer=0;
public void actionPerformed(ActionEvent e) {
count_timer++;
if( paneHeight < comHeight){
setPreferredSize(new Dimension(paneWidth, paneHeight));
paneHeight+=20;
repaint();
revalidate();
}
else
everyspeedmillisec.stop() ;
}
});
everyspeedmillisec.start();
}
/**function for Shrinking Jpanel
*
*/
public void shrink_sft_det(int speed ,JPanel slidingcom) {
comHeight = (int)slidingcom.getMinimumSize().getHeight();
comWidth = (int)slidingcom.getMinimumSize().getWidth();
//height for slide up to top position
paneHeight=0;
everyspeedmillisec = new Timer(30, new ActionListener() {
private int count_timer=0;
public void actionPerformed(ActionEvent e) {
count_timer++;
if( paneHeight < comHeight){
setPreferredSize(new Dimension(comWidth,comHeight));
comHeight-=20;
repaint();
revalidate();
}
else
everyspeedmillisec.stop() ;
}
});
everyspeedmillisec.start();
}
//Declare class and using shrinking onclick event
public class SoftPanel extends JPanel implements ActionListener
, MouseListener {
JPanel MoredetPane;
JPanel SlidedetPane;
private JLabel SoftDOCLabel;
private JLabel SoftLocation;
private JLabel SoftUpdates;
private boolean mr_det_flag =true;
MoredetPane = new SlidingPanel(new GridLayout(1,1,5,5),new Dimension(300,1));
MoredetPane.setOpaque(false);
//Add some components
SlidedetPane = new JPanel(new GridLayout(3,2,50,25));
SlidedetPane.setPreferredSize(new Dimension(300,200));
SlidedetPane.setOpaque(false);
SoftDOCLabel = new JLabel("Software Date") ;
SoftLocation = new JLabel("Software Location") ;
SoftUpdates = new JLabel("Software Updates") ;
SlidedetPane.add(SoftDOCLabel);
SlidedetPane.add(new JLabel(softbean.getSoftDOC()));
SlidedetPane.add(SoftLocation);
SlidedetPane.add(new JLabel(softbean.getSoftPath()));
SlidedetPane.add( SoftUpdates);
SlidedetPane.add(new JLabel(""));
//Onclick events
modedetails_Label.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent clickeve) {
if(mr_det_flag){
mr_det_flag=false;
((SlidingPanel)MoredetPane).expand_sft_det(200,SlidedetPane);
add(SlidingPanel);
}
else{
mr_det_flag=true;
((SlidingPanel) MoredetPane).shrink_sft_det(0,SlidedetPane);
}
});
add( MoredetPane );
}
Related
So I've got a custom JPanel which I use multiple instances of to fill a wrapper Panel inside a JScrollPane. The number of custom JPanel elements I use is dependent on the size of a list. The problem I'm running across is a part of my Custom JPanel has another invisible JPanel which expands when I click on it's parent. The behavior I'm trying to mimic is that of an accordian UI element. Before I was on this project I was primarily a webdev and while I have worked with Java a lot, I'm still relatively new to Swing.
Here is an example of the behavior - the scroll pane with all elements closed. (forgive me for the quick paint-job comments. I tried to emphasize what I see going wrong).
Next, is the image of the first element expanded - which unexpectedly expands all other elements.
It must be noted that I'm only targeting the first panel and setting the visibility, yet all other repeating panels length grows when I do this, but obviously the components inside stay invisible.
Finally, here is my final deired result:
Is there some sort of constraint in the JScrollPane that resizes it's child JPanel's components to retain the same height at all times? I can't seem to figure a way around this and I've played with all sorts of different wrappers and layouts, all to no avail.
Please let me know if anyone wants to see code snippets, but they'll have to be heavily redacted and stripped down due to the nature of the project.
Thanks,
Marek
PS: yes, I absolutely must use Swing.
Edit: Here is a static, quick and dirty, stripped down implementation of my code as suggested by Roddy of the Frozen Peas
ExampleScrollPane:
public class ExampleSrollPane extends JPanel {
private static ExampleSrollPane instance = null;
private JScrollPane contentScrollPanel = new JScrollPane();
private Vector<ExamplePanel> exPanels;
private JPanel wrapPanel = new JPanel();
public ExampleSrollPane() {
super();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white,
Color.white, new Color(115, 114, 105), new Color(165, 163, 151)));
exPanels = new Vector<ExamplePanel>();
init();
}
private void init() {
contentScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
contentScrollPanel.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new SoftBevelBorder(BevelBorder.LOWERED)));
this.add(contentScrollPanel, BorderLayout.CENTER);
initPanels();
}
public void initPanels() {
int numUnits = 15;
// Init one empty panel at least
if (numUnits == 0) numUnits = 15;
wrapPanel.setLayout(new GridLayout(numUnits, 1));
for (int i = 0; i < numUnits; i++) {
ExamplePanel exPan = new ExamplePanel(i);
exPanels.add(i, exPan);
wrapPanel.add(exPan);
}
contentScrollPanel.setPreferredSize(new Dimension(575, 100));
contentScrollPanel.getViewport().add(wrapPanel);
}
/**
* Method: viewPanel()
*
*/
private static void viewPanel() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setLocationRelativeTo(null);
frame.add(getInstance());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(new Dimension(600, 350));
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
public static ExampleSrollPane getInstance() {
if (instance == null) {
instance = new ExampleSrollPane();
}
return instance;
}
/**
* The main method.
*
* #param args the arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
viewPanel();
}
});
}
}
It's here in the showHideTable method which creates the problem.
ExamplePanel (my custom JPanel):
public class ExamplePanel extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static final Border STAT_BORDER = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white,
Color.white, new Color(115, 114, 105), new Color(165, 163, 151));
public static final EmptyBorder PAD_BORDER = new EmptyBorder(10, 10, 10, 10);
public int indx;
private JLabel unitLabel;
private JLabel statLabel;
private JLabel invLabel;
private JLabel targetLabel;
private JLabel timeLabel;
// Custom BasicArrowButton to expand/hide the "table"
private UnitToggleButton unitToggleButton;
// The expandable JPanel
public ExpanableTable elementTable;
private String id;
private String unitStatusString;
private String invStatusString;
private String targetString;
private String timeString;
public Color componentColor;
private JPanel topPanel = new JPanel();
public JPanel tablePanel = new JPanel();
public ExamplePanel(int index) {
super();
this.indx = index;
id = "Unit # 00000";
id = "Unit #00000";
unitStatusString = "PENDING";
invStatusString = "PENDING";
elementTable = new ExpanableTable();
targetString = "AZ501";
timeString = "11:18:27";
componentColor = this.getBackground();
init();
}
private void init() {
topPanel.setLayout(new GridBagLayout());
topPanel.setBorder(PAD_BORDER);
unitLabel = new JLabel(id); // TODO unit.getID();
statLabel = new JLabel(unitStatusString); // TODO: unit.getStatus();
invLabel = new JLabel(invStatusString); // TODO: unit.getInventoryStatus();
targetLabel = new JLabel(targetString);
timeLabel = new JLabel(timeString);
buildLabel(statLabel);
buildLabel(invLabel);
buildLabel(targetLabel);
buildLabel(timeLabel);
unitToggleButton = new UnitToggleButton(BasicArrowButton.EAST, indx);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.FIRST_LINE_END;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = .1;
gbc.weighty = 1;
gbc.insets = new Insets(0, 0, 5, 0);
// Add toggle button far-left, row 1
topPanel.add(unitToggleButton, gbc);
// Add empty space far-left, row 2
gbc.gridy = 1;
topPanel.add(new JLabel(" "), gbc);
// Add unit label row 1 column 2
gbc.gridy = 0;
gbc.gridx = 1;
gbc.weightx = .3;
topPanel.add(unitLabel, gbc);
// Add Status label row 1 column 3
gbc.gridx = 2;
topPanel.add(statLabel, gbc);
// Add inventory label row 1 column 4
gbc.gridx = 3;
topPanel.add(invLabel, gbc);
// Add tasking label row 2 column 2
gbc.gridy = 1;
gbc.gridx = 1;
topPanel.add(new JLabel(" Tasking: "), gbc);
// Add target label row 2 column 3
gbc.gridx = 2;
topPanel.add(targetLabel, gbc);
// Add mission Label row 2 column 4
gbc.gridx = 3;
topPanel.add(timeLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
topPanel.add(sep, gbc);
gbc.gridy = 3;
topPanel.add(elementTable, gbc);
revalidate();
this.setLayout(new BorderLayout());
this.add(topPanel, BorderLayout.NORTH);
this.add(tablePanel, BorderLayout.CENTER);
HSIUtils.setColoredBorder(tablePanel, Color.RED);
tablePanel.add(elementTable);
// Do NOT show the table on initialization
tablePanel.setVisible(false);
unitToggleButton.addActionListener(this);
}
/**
* Method: buildLabel()
*
* #param label
*/
private void buildLabel(JLabel label) {
label.setBorder(STAT_BORDER);
label.setMinimumSize(new Dimension(80, 20));
label.setPreferredSize(new Dimension(100, 25));
label.setOpaque(true);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setBackground(componentColor);
}
private void showHideTable(boolean show) {
tablePanel.setVisible(!show);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.unitToggleButton) {
showHideTable(unitToggleButton.isExpanded());
}
}
}
ExpandableTable:
public class ExpanableTable extends JPanel {
public ExpanableTable () {
super();
this.setLayout(new BorderLayout());
add(new JButton("Test1"), BorderLayout.WEST);
add(new JButton("Test2"), BorderLayout.CENTER);
add(new JButton("Test3"), BorderLayout.EAST);
}
}
Basically I want to be able expand/show/resize each Panel inside the scroll pane independently of the others. As it currently stands, if I show a hidden Panel on one, the other panel's height grows to match but does not show the component. Very strange to me but could be my ignorance of certain Swing components and the constraints they contain.
Is there some sort of constraint in the JScrollPane that resizes it's child JPanel's components to retain the same height at all times?
A scroll pane doesn't resize anything. It only displays the component added to the scroll panes and add scroll bars when the preferred size of the component added is greater than the size of the scroll pane.
wrapPanel.setLayout(new GridLayout(numUnits, 1));
On the other hand when you use a GridLayout, then yes all components added to the grid will be resized to the size of the largest component.
So you don't want to use a GridLayout for the wrapper panel.
I would suggest you could use a GridBagLayout or a BoxLayout. As the panel.
Then I would suggest that for your expandable panel you use a BorderLayout. You add the part that is always visible to the CENTER and the expandable part to the PAGE_END. Then when you want to make the panel expand you just change the visibility of the component in the PAGE_END.
Then the layout managers will do all the work recalculating the proper size of the all the panels.
I have a panel that contains three buttons. I want this panel to be placed in the upper left cell of a GridBagLayout.
My problem is that when I run the program the panel is not located in the upper left cell but it's in the middle of the layout. I have set both gridxand gridy to zero.
private JFrame frame;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem fileItem1;
private JMenuItem fileItem2;
private JPanel btnPanel;
private JButton btnRewind;
private JButton btnPlayPause;
private JButton btnFastForward;
private static boolean shouldFill = true;
private static boolean shouldWeightX = true;
private static boolean RIGHT_TO_LEFT = false;
public JPlayer() {
}
public void createAndShowGUI() {
frame = new JFrame();
frame.setTitle("JPlayer");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setVisible(true);
}
private void addComponentsToPane(Container pane) {
if(RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
if(shouldFill) {
gbc.fill = GridBagConstraints.HORIZONTAL;
}
btnRewind = new JButton();
try {
Image imgRewind = ImageIO.read(getClass().getResource("../utils/images/rewind.png"));
btnRewind.setIcon(new ImageIcon(imgRewind));
btnRewind.setOpaque(true);
btnRewind.setContentAreaFilled(false);
btnRewind.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnPlayPause = new JButton();
try {
Image imgPlay = ImageIO.read(getClass().getResource("../utils/images/play.png"));
Image imgPause = ImageIO.read(getClass().getResource("../utils/images/pause.png"));
btnPlayPause.setIcon(new ImageIcon(imgPlay));
btnPlayPause.setOpaque(true);
btnPlayPause.setContentAreaFilled(false);
btnPlayPause.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnFastForward = new JButton();
try {
Image imgFastForward = ImageIO.read(getClass().getResource("../utils/images/fast_forward.png"));
btnFastForward.setIcon(new ImageIcon(imgFastForward));
btnFastForward.setOpaque(true);
btnFastForward.setContentAreaFilled(false);
btnFastForward.setBorderPainted(false);
} catch(Exception e) {
e.printStackTrace();
}
btnPanel = new JPanel();
btnPanel.add(btnRewind);
btnPanel.add(btnPlayPause);
btnPanel.add(btnFastForward);
btnPanel.setSize(new Dimension(40, 40));
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
JButton btn = new JButton("Some Button");
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(btnPanel, gbc);
pane.add(btn, c);
}
Try GridBagConstraints.anchor constraint that has default value as CENTER positioned.
Directly from documentation on How to Use GridBagLayout
If a component's display area is larger than the component itself, then you can specify whereabouts in the display area the component will be displayed by using the GridBagConstraints.anchor constraint.
The anchor constraint's values can be absolute (north, south, east, west, and so on), or orientation-relative (at start of page, at end of line, at the start of the first line, and so on), or relative to the component's baseline.
Note:
You don't need to use multiple instances of GridBagConstraints. Just create one and update the constraints before adding the component in the panel. In this way you can reuse the existing constraints that is common for all.
Don't forget to set weightx and weighty to use anchor constraints properly.
Directly from same documentation
weightx, weighty
Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
public class LabelResizing2 {
public static JPanel createSliderPanel(
int min, int max, int curr)
{
/*outer panel*/
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createRaisedBevelBorder());
final JLabel valueLabel = new JLabel();
valueLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
/*set up slider*/
final JSlider slider = new JSlider(JSlider.VERTICAL, min, max, curr);
slider.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.add(valueLabel);
panel.add(slider);
/*slider move event*/
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent event) {
valueLabel.setText(Integer.toString(slider.getValue()));
}
});
valueLabel.setText(Integer.toString(curr));
return panel;
}
public static void main(String[] args) {
JFrame gui = new JFrame("Label Resizing");
gui.setMinimumSize(new Dimension(500, 500));
gui.add(createSliderPanel(-100, 100, 0));
gui.setVisible(true);
}
}
The problem here is that the slider will move all over the place, as the label resizes from '99' to '100' and from '0' to '-1' etc.
Several questions:
Why is it bouncing around?
If I put in
valueLabel.setSize(100, 100);
it does absolutely nothing. Why?
valueLabel.setMinimumSize(100,100);
will stop it bouncing around, but doesn't actually resize the label. Why?
Best solution I've found is to go slider.setAlignmentX(Component.LEFT_ALIGNMENT);Is this a good solution? However, this won't work for CENTER_ALIGNMENT or RIGHT_ALIGNMENT, what's going on here?
If we modify it a bit and put the valueLabel inside a panel.
:
final JPanel labelPanel = new JPanel();
final JLabel valueLabel = new JLabel();
valueLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelPanel.add(valueLabel);
labelPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.add(labelPanel);
panel.add(slider);
The slider still moves around, even though the labelPanel isn't resizing (the valueLabel is resizing inside it). What's going on here? Is the layout manager also looking at nested components?
1 Why is it bouncing around?
That's the nature of this particular layout manager. As the component's preferredSize is changed, the layout manager is updating the layout.
2 If I put in
valueLabel.setSize(100, 100); it does absolutely nothing. Why?
valueLabel.setMinimumSize(100,100); will stop it bouncing around, but doesn't actually resize the label. Why?
Changing the size of a component under the control of a layout manager has no effect because the layout manager will resize the component when it re-lays out it's children.
preferred/minimum/maximumSize are "suggestions" that the layout managers may use to determine how best to perform their individual layouts, they are not obligated to use them in any way.
3 Best solution I've found is to go slider.setAlignmentX(Component.LEFT_ALIGNMENT);. Is this a good
solution?
I guess so, but "Be aware that these are hints only and might be ignored by certain layout managers"
4 If we modify it a bit and put the valueLabel inside a panel.
Now, instead of the components begin effected directly by the layout manager, each panel is effect it's parent layout manager, net result, some problem
UPDATED with example
In this case, you can cheat a little...
public class BadLayout03 {
public static void main(String[] args) {
new BadLayout03();
}
public BadLayout03() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestLayout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestLayout extends JPanel {
public TestLayout() {
setLayout(new GridBagLayout());
final JLabel valueLabel = new JLabel("000");
final JSlider slider = new JSlider();
slider.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
StringBuilder sb = new StringBuilder(Integer.toString(slider.getValue()));
while (sb.length() < 3) {
sb.insert(0, "0");
}
valueLabel.setText(sb.toString());
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.75;
gbc.anchor = GridBagConstraints.WEST;
valueLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(valueLabel, gbc);
gbc.gridx = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(slider, gbc);
}
}
}
I'm prgramming a simple input diagram in Swing. I use boxLayout to create a simple GUI of user input. Problem is that creating a horizontal strut between the JPanel of all the labels and the JPanel of the JTextFields causes the whole panel to shift downwards (weird) this is the whole panel:
private JPanel secondCard() {
//main panel. set the boxlayout
secondCard = new JPanel();
secondCard.setLayout(new BoxLayout(secondCard,BoxLayout.Y_AXIS));
// create vertical strut for looks
secondCard.add(Box.createVerticalStrut(20));
// create title. center it.
JLabel title = new JLabel("Configure main network parameters ");
title.setAlignmentX(CENTER_ALIGNMENT);
secondCard.add(title);
// create vertical strut for looks
secondCard.add(Box.createVerticalStrut(20));
// create panel for the description labels
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS));
labelPanel.setAlignmentX(LEFT_ALIGNMENT);
JLabel inPut =new JLabel("number of inputs");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
inPut =new JLabel("number of outputs");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
inPut =new JLabel("number of layers");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
JPanel textFieldPanel = new JPanel();
textFieldPanel.setLayout(new BoxLayout(textFieldPanel,BoxLayout.Y_AXIS));
textFieldPanel.setAlignmentX(LEFT_ALIGNMENT);
JTextField inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
textFieldPanel.setMaximumSize(new Dimension(50, labelPanel.getMaximumSize().height));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.X_AXIS));
inputPanel.setAlignmentX(CENTER_ALIGNMENT);
inputPanel.add(labelPanel);
//this is the problem strut!! it causes inputPanel to shift downwards
inputPanel.add(Box.createHorizontalStrut(20));
inputPanel.add(textFieldPanel);
secondCard.add(inputPanel);
return secondCard;
}
without the strut it looks like:
With strut it looks like (I know I suck at picture editing):
You are adding a Box strut to a BoxLayout.
As the javadoc states, createHorizontalStrut(int width):
Creates an invisible, fixed-width component. In a horizontal box, you
typically use this method to force a certain amount of space between
two components. In a vertical box, you might use this method to force
the box to be at least the specified width. The invisible component
has no height unless excess space is available, in which case it takes
its share of available space, just like any other component that has
no maximum height.
As such, it is filling the height between your title JLabel and the bottom of the JPanel.
You might want to consider using Box.createRigidArea(new Dimension(20, height)) instead, where height could be specified or set to the height of labelPanel.
Or, you could reconsider the layout for your JPanel - take a look at the visual guide.
For future reference, if you cannot make sense of your Swing layout, try putting adding a coloured LineBorder to the JComponents you're unsure of. In this case, the Box struts are not JComponents but Components, so you'd have to put them into a JPanel, but this would at least have shown you what space each component was taking up in your top-level JPanel.
use Cardlayout for wizard logics
put JLabel(Configure ...., JLabel.CENTER) to the BorderLayout.NORTH
put JPanel with JButtons to the BorderLayout.SOUTH
put JPanel with SpringLayout, GridLayout, or GridBagLayout to the BorderLayout.CENTER
Top-Level Container have got implemened BorderLayout by default, then there no reason to re_define BorderLayout
above mentioned steps are called NestedLayout
alternative are put all JComponents by using GridBagLayout, SpringLayout or todays MigLayout to the one JPanel, but why bothering
Example of a nested layout, one using BorderLayout, FlowLayout (JPanel's default), and GridBagLayout:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class LayoutFoo {
private static final String TITLE = "Configure Main Foobar Parameters";
private static final String[] LABEL_TEXTS = {
"Number of Spams", "Number of Frapzats", "Number of Zignuts"
};
private static final int TEXTFIELD_SIZE = 10;
private static final Insets WEST_INSETS = new Insets(5, 5, 5, 10);
private static final Insets EAST_INSETS = new Insets(5, 10, 5, 5);
private static final int EB_GAP = 5;
private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();
public JPanel getConfigFooPanel() {
JPanel textFieldPanel = new JPanel(new GridBagLayout());
for (int i = 0; i < LABEL_TEXTS.length; i++) {
addTextAndField(textFieldPanel, LABEL_TEXTS[i], i);
}
int blVertGap = 20;
JPanel borderLayoutPanel = new JPanel(new BorderLayout(0, blVertGap));
borderLayoutPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP,
EB_GAP, EB_GAP));
JLabel titleLabel = new JLabel(TITLE, JLabel.CENTER);
borderLayoutPanel.add(titleLabel, BorderLayout.PAGE_START);
borderLayoutPanel.add(textFieldPanel, BorderLayout.CENTER);
JPanel outerWrapperFlowPanel = new JPanel();
outerWrapperFlowPanel.add(borderLayoutPanel);
return outerWrapperFlowPanel;
}
public String getFieldText(String labelText) {
JTextField field = textFieldMap.get(labelText);
if (field == null) {
return ""; // ?? throw exception
} else {
return field.getText();
}
}
private void addTextAndField(JPanel panel, String text, int i) {
JLabel label = new JLabel(text, JLabel.LEFT);
JTextField textField = new JTextField(TEXTFIELD_SIZE);
textFieldMap.put(text, textField);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = i;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = WEST_INSETS;
panel.add(label, gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = EAST_INSETS;
panel.add(textField, gbc);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("LayoutFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LayoutFoo().getConfigFooPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
how can I correctly returns XxxSize from JComponent(s) added to the JLabel
1st. figure >> lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0)
2nd. figure >> added some PreferredSize to the JLabel
3rd. figure >> calculated PreferredSize from JComponent(s) added to the JLabel
4th. figure >> lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize
notice sice there is used Nimbus L&F, same output is there for all accesible L&F
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class NimbusBorderPainterDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame();
private JPanel fatherPanel = new JPanel(), titlePanel = new JPanel();
private JLabel buttonPanel = new JLabel();
//figure ---> 4th. switch JLabel with JPanel
//private JPanel buttonPanel = new JPanel();
private Queue<Icon> iconQueue = new LinkedList<Icon>();
public NimbusBorderPainterDemo() {
iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
JButton button0 = createButton();
JButton button1 = createButton();
JButton button2 = createButton();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
int gap = 5;
buttonPanel.setLayout(new GridLayout(0, 3, gap, 0));
buttonPanel.add(button0);
buttonPanel.add(button1);
buttonPanel.add(button2);
// figure 1st. ---> without PreferredSize
// figure 2nd. --->
//buttonPanel.setPreferredSize(new Dimension(160, 30));
// figure 3rd. --->
/*Dimension dim = button0.getPreferredSize();
int w = dim.width;
int h = dim.height;
w = (w + 5) * 3;
h += 4;
dim = new Dimension(w, h);
buttonPanel.setPreferredSize(dim);*/
titlePanel.setLayout(new BorderLayout());
titlePanel.add(new JLabel(nextIcon()), BorderLayout.WEST);
titlePanel.add(new JLabel("My Frame"), BorderLayout.CENTER);
titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
titlePanel.add(buttonPanel, BorderLayout.EAST);
fatherPanel.setLayout(new BorderLayout());
fatherPanel.add(titlePanel, BorderLayout.CENTER);
frame.setUndecorated(true);
frame.add(fatherPanel);
frame.setLocation(50, 50);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
}
private JButton createButton() {
JButton button = new JButton();
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setIcon(nextIcon());
//button.setRolloverIcon(nextIcon());
//button.setPressedIcon(nextIcon());
//button.setDisabledIcon(nextIcon());
nextIcon();
return button;
}
private Icon nextIcon() {
Icon icon = iconQueue.peek();
iconQueue.add(iconQueue.remove());
return icon;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception fail) {
}
UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED);
NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo();
}
});
}
}
The default preferred size calculation is to use the layout manager to determine the preferred size of a component. This means the layout manager iterates through all the child components to determine the preferred size of each. For a JPanel, which is meant to be used as a Container this calculation is used.
However, for other Swing components, the getPreferredSize() method is always overridden to provide a reasonable size for the given component.
In the case of a JLabel, the preferred size calculation takes into account the text and the icon used. Since you didn't provide either the preferred size is zero. Of course if you manually override this calculation by using the setPreferredSize() method then the component will have a preferred size.
So even though Swing allows you to add components to any component and use a layout manager to layout the child components, these child components are not used in the preferred size calculation.
This is not just a Nimbus issue.