Reputation: 597
I have a JPanel
, that lists a set of Jlabel
s. I would like to make the width of each label wide as the panel's size. So it will be wide but but text stays in the left. I am using BoxLayout
to list labels.
Here is the code:
public class JavaApplication78 {
JFrame frame;
JPanel panel, labelsPanel;
JLabel label;
ArrayList<String> names = new ArrayList<String>();
ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
Border border = BorderFactory.createLineBorder(Color.BLUE);
JScrollPane labelsScroll;
public JavaApplication78(){
frame = new JFrame();
panel = new JPanel(new BorderLayout());
names.add(".mp3");names.add(".html");names.add(".jpeg");names.add(".mp4");names.add(".pdf");
labelsPanel = new JPanel();
labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.PAGE_AXIS));
labelsScroll = new JScrollPane(labelsPanel);
for(String format : names){
label = new JLabel(format);
//icon
labelsArray.add(label);
labelsPanel.add(label);
label.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
}
panel.add(labelsScroll, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JavaApplication78();
}
}
Currently I could give a border around each JLabel
. The height of labels are ok, but width need to be as wide as the parent panel.
Any idea ?
Upvotes: 3
Views: 9951
Reputation: 32700
Use PreferredSize
to set the dimension of the parent JPanel
for your JLabel
s :
JLabel label = new JLabel() {
public Dimension getPreferredSize() {
return labelsPanel.getSize();
};
};
Unless you are obliged to use BoxLayout
, maybe it's better to use GridBagLayout
as suggested by @MadProgrammer in his comment.
EDIT :
You could also take a look at MigLayout. Adding components with it is very simple :
labelsPanel.setLayout(new MigLayout());
labelsPanel.add(label, "span") // span to take the whole row width.
Upvotes: 1
Reputation: 347314
You could use a GridBagLayout
...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
public class JavaApplication78 {
JFrame frame;
JPanel panel, labelsPanel;
JLabel label;
ArrayList<String> names = new ArrayList<String>();
ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
Border paddingBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border border = BorderFactory.createLineBorder(Color.BLUE);
JScrollPane labelsScroll;
public JavaApplication78() {
frame = new JFrame();
panel = new JPanel(new BorderLayout());
names.add(".mp3");
names.add(".html");
names.add(".jpeg");
names.add(".mp4");
names.add(".pdf");
labelsPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
labelsScroll = new JScrollPane(labelsPanel);
for (String format : names) {
label = new JLabel(format);
//icon
labelsArray.add(label);
labelsPanel.add(label, gbc);
label.setBorder(BorderFactory.createCompoundBorder(border, paddingBorder));
}
panel.add(labelsScroll, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
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) {
ex.printStackTrace();
}
new JavaApplication78();
}
});
}
}
Or a JList
, but in cases like this, I tend to like using the VerticalLayout
from the SwingLabs SwingX library
Upvotes: 5
Reputation: 324167
BoxLayout respects the maximum size of the component and for a JLabel the maximum size is equal to the preferred size.
You can override the getMaximumSize()
method:
label = new JLabel(format)
{
@Override
public Dimension getMaximumSize()
{
Dimension d = super.getMaximumSize();
d.width = Integer.MAX_VALUE;
return d;
}
};
label.setHorizontalAlignment(JLabel.CENTER);
This will then allow the label to expand horizontally to fill the width of the panel.
Or if you wanted you could use the Relative Layout. It functions much like the BoxLayout but has a few additional features. The basic code would be:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel labelsPanel = new JPanel( rl );
Upvotes: 2