Question

My problem is that the JSpinner (named spinnerCantidadPuntas in the code) is sometimes not showing at all, or just buggy. when i run the project (F6) its almost guaranteed to not show properly, when i debug it (CTRL+F5) it displays properly most of the time. i cannot the figure out why. heres the details on what im using:

Product Version: NetBeans IDE 7.2.1 (Build 201210100934) Java: 1.7.0_11; Java HotSpot(TM) Client VM 23.6-b04 System: Windows 7 version 6.1 running on x86; Cp1252; es_VE (nb)

I made a project, im listing the content of all classes here

    package estrella;
    public class Main {
        public static void main(String[] args) {
            Interfaz f=new Interfaz();
        }
    }



    package estrella;
    import java.awt.Color;
    import java.awt.Container;
    import java.util.Hashtable;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JSlider;
    import javax.swing.JSpinner;
    import javax.swing.SpinnerNumberModel;
    public class Interfaz extends JFrame {

        Container c;

        AreaDeDibujos lienzo;

        JLabel lInfo,lCantidadPuntas,lTamanioEstrella,lLargoPuntas;
        JSpinner spinnerCantidadPuntas;
        JSlider sliderTamanioEstrella,sliderLargoPuntas;

        public Interfaz(){

            setTitle("Ejemplo <<Dibujo de primitivas y poligonos>> Version 1.0 (05/02/2013)");
            setSize(1024,720);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);

            c=getContentPane();
            c.setBackground(new Color(240,250,245));
            c.setLayout(null);

            /*lienzo=new AreaDeDibujos();
            lienzo.setBounds(25,25,800,600);

            c.add(lienzo);*/

            lInfo=new JLabel("Por favor, seleccione los parametros de la estrella a dibujar:");
            lInfo.setBounds(10,10,350,20);
            c.add(lInfo);

            lCantidadPuntas=new JLabel("Numero de puntas de la estrella: ");
            lCantidadPuntas.setBounds(10,35,350,20);
            c.add(lCantidadPuntas);
            spinnerCantidadPuntas=new JSpinner(new SpinnerNumberModel(7, //valor inicial
                                                              Estrella.MINIMO_PUNTAS, //valor minimo
                                                              Estrella.MAXIMO_PUNTAS, //valor maximo
                                                              1 // incremento/decremento (paso)
                                                             )
                                       );
            spinnerCantidadPuntas.setBounds(10,55,120,20);
            c.add(spinnerCantidadPuntas);

            lTamanioEstrella=new JLabel("Tamaño de la estrella (pixeles): ");
            lTamanioEstrella.setBounds(10,80,350,20);
            c.add(lTamanioEstrella);
            sliderTamanioEstrella=new JSlider( 
                                     JSlider.HORIZONTAL,
                                     AreaDeDibujos.TAMANIO_ESTRELLA_MINIMO,
                                     AreaDeDibujos.TAMANIO_ESTRELLA_MAXIMO,
                                     (int)(AreaDeDibujos.TAMANIO_ESTRELLA_MAXIMO/2)
                                     );
            Hashtable etiquetasDelSlider1 = new Hashtable();
            int i,cantidadDeRayas,rango,paso,minimo,maximo,valorDeLaRaya;
            minimo=AreaDeDibujos.TAMANIO_ESTRELLA_MINIMO;
            maximo=AreaDeDibujos.TAMANIO_ESTRELLA_MAXIMO;
            rango=maximo-minimo;
            cantidadDeRayas=10;
            paso=rango/cantidadDeRayas;
            for(i=0;i<=cantidadDeRayas;i++){
                valorDeLaRaya=minimo+paso*i;
                etiquetasDelSlider1.put(new Integer (valorDeLaRaya), new JLabel(""+valorDeLaRaya));
    }
            sliderTamanioEstrella.setLabelTable( etiquetasDelSlider1 );
            sliderTamanioEstrella.setPaintLabels(true);
            sliderTamanioEstrella.setMajorTickSpacing(10);
            sliderTamanioEstrella.setPaintTicks(true);
            sliderTamanioEstrella.setBounds(10,100,350,60);
            c.add(sliderTamanioEstrella);
        }

    }

    package estrella;
    public class Estrella {
        public static final int MINIMO_PUNTAS=3,MAXIMO_PUNTAS=10;
    }

    package estrella;
    public class GestorDeEventos {   
    }

    package estrella;
    import javax.swing.JComponent;
    public class AreaDeDibujos extends JComponent {
        public static final int TAMANIO_ESTRELLA_MINIMO=50,TAMANIO_ESTRELLA_MAXIMO=400;
    }

i didnt bother add a Jar file because i know ppl will not want to download nor run it thinking its a virus

Was it helpful?

Solution

"My problem is that the JSpinner", actually, it's the lack of a proper layout manager...

Using a null layout manager means that any time you invalidate the container (by adding something to it), the container isn't updating itself in a way that the repaint manager will identify.

Move setVisible(true); to the end of the constructor.

If you were using a layout manager, you would be able to revalidate/repaint instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top