Show icons in order in Java Swing
To show icons, label, components in that layout, a JPanel with FlowLayout
layout is ideal, which will place them side by side.
By default they will be placed starting from the center. To change that we will use this instruction or class field FlowLayout.LEFT
, which in this case will start from the left.
JPanel superior = new JPanel(new FlowLayout(FlowLayout.LEFT));
As the user presses, a rectangle or icon of color will be drawn according to button pressed . The idea is that these icons are unlimited, since the user must press them whenever he wants!
Well, inside the actionPerformed()
method of each button, which is where the event will be generated, we will create a JLabel dynamically each time you press each of the buttons by setting a background color.
//botón azul
btnBlue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JLabel lbl = new JLabel();
lbl.setPreferredSize(new Dimension(50,15));
lbl.setOpaque(true);
lbl.setBackground(Color.BLUE);
superior.add(lbl);
superior.revalidate();
System.out.println("btn azul");
}
});
Creating the JLabel
JLabel lbl = new JLabel();
We give or set a preferred size to our label
lbl.setPreferredSize(new Dimension(50,15));
We give the component an opacity or we can not give it a background color.
lbl.setOpaque(true);
We set the background color. In this case blue
lbl.setBackground(Color.BLUE);
We added to our panel.
superior.add(lbl);
And we can draw dynamically with this instruction.
superior.revalidate();
Complete example ..
public class LabelConFondoColor extends JFrame {
JPanel superior;
public LabelConFondoColor() {
miGUI();
}
private void miGUI() {
superior = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel inferior = new JPanel();
JButton btnBlue = new JButton("Azul");
btnBlue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JLabel lbl = new JLabel();
lbl.setPreferredSize(new Dimension(50,15));
lbl.setOpaque(true);
lbl.setBackground(Color.BLUE);
superior.add(lbl);
superior.revalidate();
System.out.println("btn azul");
}
});
JButton btnRed = new JButton("Rojo");
btnRed.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JLabel lbl = new JLabel();
lbl.setPreferredSize(new Dimension(50,15));
lbl.setOpaque(true);
lbl.setBackground(Color.RED);
superior.add(lbl);
superior.revalidate();
System.out.println("btn azul");
}
});
inferior.add(btnBlue);
inferior.add(btnRed);
this.setBounds(300, 300, 570, 650);
this.add("Center", superior);
this.add("South", inferior);
}
public static void main(String[] args) {
LabelConFondoColor app = new LabelConFondoColor();
app.setDefaultCloseOperation(EXIT_ON_CLOSE);
app.setVisible(true);
}
}
Result ..
Update.
How to put Image to a JLabel and fit the image to it.
. We have our JLabel ..
JLabel lbl = new JLabel();
. Apart from the preferredSize, we will give you a specific size
lbl.setSize(50, 15);
. Now we create that image
ImageIcon imagen = new ImageIcon("C:\Users\miusuario\Desktop\coca-cola.png");
. Now that image is adjusted to the scale of JLabel
and in turn we create a Icon
.
Icon icono = new ImageIcon(imagen.getImage().getScaledInstance(lbl.getWidth(), lbl.getHeight(), Image.SCALE_DEFAULT));
. And finally we set the Icon in the JLabel.
lbl.setIcon(icono);
I leave you the content of a actionPerformed()
public void actionPerformed(ActionEvent e) {
JLabel lbl = new JLabel();
lbl.setPreferredSize(new Dimension(50,15));
lbl.setSize(50, 15);
lbl.setOpaque(true);
ImageIcon imagen = new ImageIcon("C:\Users\dlolh\Desktop\coca-cola.png");
Icon icono = new ImageIcon(imagen.getImage().getScaledInstance(lbl.getWidth(), lbl.getHeight(), Image.SCALE_DEFAULT));
lbl.setIcon(icono);
superior.add(lbl);
superior.revalidate();
}
And the result: