Show icons in order in Java Swing

2

I'm doing a program that shows two buttons with two different colors (in this case Red and Blue) and according to the user presses one or the other it is shown in a label panel that is higher, so that if the user presses the Blue button and then the Red shows: Blue - > Red. The idea is to transform that text to an individual icon in each case, which is the color that was pressed. For example if you press the Blue and then the Red you will see a Blue icon and then a Red icon next to the previous one in order, just like with the labels. Any idea how I can do it? The idea is that these icons are unlimited, since the user must press them whenever he wants!

I leave part of the code and a screenshot:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    if(contador<10){
            todo = jLabel1.getText();
            jLabel1.setText(todo+"--->"+"Azul");
            jPanel1.add(jLabel1);
            contador++;
            }else{
            jLabel1.setText("<html>"+todo+"<p><html>"); //Hace un salto de linea si se supera el valor de 10.
            contador = 0;
            }
        }                                        

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

            if(contador<10){
            todo = jLabel1.getText();
            jLabel1.setText(todo+"--->"+"Rojo");
            jPanel1.add(jLabel1);
            contador++;
            }else{
            jLabel1.setText("<html>"+todo+"<p><html>"); //Hace un salto de linea si se supera el valor de 10.
            contador = 0;
            }
}

This is what he does for now:

As I said the idea is that these Labels are really the ordered colors.

Thanks in advance:)

P.D: If there is an easier way than doing it with better icons! The important thing is to show the colors in the form of an image.

    
asked by TaD 24.09.2018 в 03:40
source

1 answer

1
  

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:

    
answered by 26.09.2018 / 21:29
source