Reputation: 605
I have a list of categories and each category has an image, I need to display those images one after the other (4 at a row) with some spaces between them. I have problem displaying the last label, it seems that the setBounds method doesn't affect it. I created a JPanel and I add all the labels containing the images to the panel. this is my source code, I also added a link to the screenshot
Thanks!
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
panel.setBackground(Color.white);
java.util.Iterator<Entry<Integer, Y2category>> it = configFile.categories.entrySet().iterator();
int positionx = 50;
int positiony = 50;
int linecounter = 0;
while( it.hasNext() )
{
Map.Entry<Integer, Y2category> pairs = (Entry<Integer, Y2category>) it.next();
Y2category cat = (Y2category) pairs.getValue();
JLabel label = new JLabel( new ImageIcon( "img\\main\\black.png" ), JLabel.CENTER );
label.setBounds(positionx,positiony,115,179);
label.setFont(new Font("Arial", Font.PLAIN, 14));
panel.add(label);
positionx += 220;
linecounter++;
if ( linecounter == 4 )
{
linecounter = 0;
positiony += 200;
positionx = 50;
}
}
frame.add(panel);
//ImageIcon icon = new ImageIcon("img\\icon.jpg");
//frame.setIconImage(icon.getImage());
frame.setResizable( false );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,900);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true);
Upvotes: 0
Views: 65
Reputation: 653
Using a null layout is never recommendable. setBounds() method sets the location static and is unsuitable for any dynamic UI. Moreover when you would need to add a component in between at a later stage you would need to update most of the code i.e. modify setBounds() of effected components.
I would recommend using GridBagLayout which is very flexible and all you need to set is grid for components. I have written a small sample code to help you understand:
public JPanel getComponentPanel()
{
if(null == componentPanel)
{
componentPanel = new JPanel();
GridBagLayout gridBagLayout = new GridBagLayout();
componentPanel.setLayout(gridBagLayout);
// Create a single constraint to be reused
GridBagConstraints constraint = new GridBagConstraints();
// Insets is to provide spacing in the format (Top, Left, Bottom, Right)
constraint.insets = new Insets(10, 10, 10, 10);
// gridx for x-axis positioning and gridy for y-axis positioning
constraint.gridx = 0;
constraint.gridy = 0;
label1 = new JLabel("Label 1");
componentPanel.add(label1, constraint);
constraint.gridx = 1;
constraint.gridy = 0;
label2 = new JLabel("Label 2");
componentPanel.add(label2, constraint);
constraint.gridx = 2;
constraint.gridy = 0;
label3 = new JLabel("Label 3");
componentPanel.add(label3, constraint);
constraint.gridx = 3;
constraint.gridy = 0;
label4 = new JLabel("Label 4");
componentPanel.add(label4, constraint);
constraint.gridx = 0;
constraint.gridy = 1;
label5 = new JLabel("Label 5");
componentPanel.add(label5, constraint);
.
.
.
.
constraint.gridx = 3;
constraint.gridy = 3;
labelXYZ = new JLabel("Label 5");
componentPanel.add(labelXYZ, constraint);
}
return componentPanel;
}
Upvotes: 4