0

I'm trying to add various JButtons to a JPanel using "for" but doesn't work. No compilation or other errors. Buttons just won't appear.

A bit of context, I create an ArryList in another class ("GestorFrigo") that gets data from DataBase, this works fine, the array has all the data and there's no problem getting back the data from the array.

This is my code: Thanks in advance.

    import gestor.GestorFrigo;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollBar;

@SuppressWarnings("serial")
public class VentanaInterior extends JFrame {

    private JPanel contentPane;
    private JButton btnPerfiles;
    private JButton btnAadir;
    private JButton btnRecetas;
    private JScrollBar scrollBar;

    private GestorFrigo frigo;

    private ArrayList<JButton> botones;
    private ArrayList<Object> boton;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VentanaInterior frame = new VentanaInterior();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public VentanaInterior() {

        //Componentes       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 556, 363);
        setLocationRelativeTo(null);
        setTitle("Tu Frigorífico Inteligente");
        setIconImage(new ImageIcon(getClass().getResource("img/logo.png")).getImage());

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnPerfiles = new JButton("Perfiles");
        btnPerfiles.setBounds(0, 302, 96, 23);
        contentPane.add(btnPerfiles);

        btnAadir = new JButton("A\u00F1adir alimento");
        btnAadir.setBounds(369, 302, 148, 23);
        contentPane.add(btnAadir);

        btnRecetas = new JButton("Recetas");
        btnRecetas.setBounds(96, 302, 103, 23);
        contentPane.add(btnRecetas);

        scrollBar = new JScrollBar();
        scrollBar.setBounds(523, 0, 17, 325);
        contentPane.add(scrollBar);

        JButton btnQueso = new JButton();
        btnQueso.setBounds(24, 35, 62, 61);
        contentPane.add(btnQueso);

        frigo = new GestorFrigo(); //creamos el gestor
        String imagen = frigo.getArray().get(1).getImagen(); //cogemos la imagen asociada al alimento

        btnQueso.setIcon(new ImageIcon("src/img/"+imagen));

        for(int i=0;i<frigo.getArray().size();i++){

            JButton boton = new JButton();
            String imagen2 = frigo.getArray().get(i).getImagen();
            boton.setIcon(new ImageIcon("src/img/"+imagen2));
            contentPane.add(boton);

        }


    }
}
sheilapbi
  • 325
  • 1
  • 15
  • _Don't_ block the EDT in a loop; _do_ see [*Concurrency in Swing*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [*How to Use Swing Timers*](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). – trashgod May 14 '14 at 23:47
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. – Andrew Thompson May 14 '14 at 23:57
  • .. 2) `new ImageIcon("src/img/"+imagen)` By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson May 14 '14 at 23:58

1 Answers1

2

Don't use a null layout.

By default your buttons have a default size of (0, 0) so there is nothing to paint.

User a layout manager, probably a GridLayout, and the layout manager will determine the size and location of each button for you.

Read the section from the Swing tutorial o Using Layout Managers for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you so much, I used an AbsoluteLayout and .setBounds() to set the size of the buttons to fit my images. – sheilapbi May 20 '14 at 16:25
  • @user3638766, Nooooo!!! Don't use AbsoluteLayout!!! Use a layout manager that will set the size/location of all the components for you. – camickr May 20 '14 at 20:31