Problem with JComboBox

Discussion in 'Java' started by Derek, May 9, 2005.

  1. Derek

    Derek Guest

    Hello:

    I have the following problem to use JComboBox.

    I have an aplication that have two panels. When I run the aplication this
    show a first panel (Presentacion). It has a button and when I click this
    button it remove the current panel and show the second panel (Formulario).
    This second panel has two JComboBox elements. And, here is the problem,
    these JComboBox show like an "white space". Don´t show correctly.

    But, if I show first the panel (Formulario) that has the JComboBox elements,
    these show correctly.

    The code is the following. Compile and run it to see the problem:

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    /* FrameDemo.java requires no other files. */
    public class Presentacion extends JFrame implements ActionListener{

    JPanel panelPresentacion = null;
    JPanel panelFormulario = null;
    JPanel panelProgreso = null;

    JButton botoniniciar = null;
    JButton botonatras = null;
    JButton botonadelante = null;

    JComboBox comboNombre = null;
    JComboBox comboNombre2 = null;

    static Presentacion presentacion = new Presentacion();

    public Presentacion() {
    super("Interfaz Tarjeta");
    getContentPane().setLayout(null);
    getContentPane().setBounds(0,0,400,400);
    getContentPane().setBackground(Color.blue);
    getContentPane().add(getPanelPresentacion());
    //getContentPane().add(getPanelFormulario());

    pack();
    setResizable(false);
    setLocation(200, 200);
    setSize(400,400);
    setVisible(true);
    }



    public JPanel getPanelPresentacion() {

    if (panelPresentacion==null)
    {
    panelPresentacion= new JPanel();
    panelPresentacion.setBounds(0,2, 600,600);
    panelPresentacion.setBackground(Color.gray.brighter());
    panelPresentacion.setLayout(null);


    JLabel l = new JLabel("Bienvenido...");
    l.setBounds(10,40,100,25);
    panelPresentacion.add(l);
    panelPresentacion.add(getBotonIniciar());

    }
    getRootPane().setDefaultButton(getBotonIniciar());
    return panelPresentacion;
    }



    public JPanel getPanelFormulario() {

    if (panelFormulario == null)
    {
    panelFormulario= new JPanel();
    panelFormulario.setBounds(0,2, 400,400);
    panelFormulario.setBackground(Color.gray.brighter());
    panelFormulario.setLayout(null);

    panelFormulario.add(getCombo());
    panelFormulario.add(getCombo2());

    panelFormulario.add(getBotonAtras());
    }
    return panelFormulario;
    }



    public JComboBox getCombo() {
    if (comboNombre == null)
    {
    comboNombre = new JComboBox();
    comboNombre.setBounds(10,40,100,20);
    comboNombre.setBackground(Color.WHITE);
    comboNombre.addItem("objeto1");
    comboNombre.addItem("objeto2");
    comboNombre.addActionListener(this);
    }
    return comboNombre;
    }


    public JComboBox getCombo2() {
    if (comboNombre2 == null)
    {
    comboNombre2 = new JComboBox();
    comboNombre2.setBounds(10,100,100,20);
    comboNombre2.setBackground(Color.WHITE);
    comboNombre2.addItem("elemento1");
    comboNombre2.addItem("elemento2");
    comboNombre2.addActionListener(this);
    }
    return comboNombre2;
    }


    public JButton getBotonIniciar() {

    if (botoniniciar==null)
    {
    botoniniciar = new JButton("Iniciar");
    botoniniciar.setBounds(100,100,100,25);
    botoniniciar.addActionListener(this);
    }
    return botoniniciar;
    }


    public JButton getBotonAtras() {

    if (botonatras==null)
    {
    botonatras = new JButton("Atrás");
    botonatras.setBounds(50,200,100,25);
    botonatras.addActionListener(this);
    }
    return botonatras;
    }



    public void actionPerformed(ActionEvent event){
    if (event.getSource() == getBotonIniciar())
    {

    presentacion.remove(getPanelPresentacion());
    System.out.println("Pulsado boton Iniciar");
    presentacion.getContentPane().add(getPanelFormulario());

    presentacion.repaint();


    }
    else if (event.getSource() == getBotonAtras())
    {
    presentacion.remove(getPanelFormulario());
    System.out.println("Pulsado boton Atras");
    presentacion.getContentPane().add(getPanelPresentacion());

    presentacion.repaint();
    }
    else if (event.getSource() == getCombo())
    {
    System.out.println(getCombo().getSelectedItem());
    }
    else if (event.getSource() == getCombo())
    {
    System.out.println(getCombo().getSelectedItem());
    }
    else if (event.getSource() == getCombo2())
    {
    System.out.println(getCombo2().getSelectedItem());
    }
    }




    public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Presentacion presentacion = new Presentacion();

    presentacion.setTitle("Interfaz Smart Card");
    presentacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    presentacion.setVisible(true);

    // Cursor en espera //
    //presentacion.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    // Cursor en espera //

    }
    }


    What is the problem?.

    Thanks.
     
    Derek, May 9, 2005
    #1
    1. Advertising

  2. Hi,

    Try putting :

    ((JComponent)presentacion.getContentPane()).revalidate();
    before
    presentacion.repaint();

    Regards,

    Arnaud


    "Derek" <> a écrit dans le message news:
    d5n5h5$...
    > Hello:
    >
    > I have the following problem to use JComboBox.
    >
    > I have an aplication that have two panels. When I run the aplication this
    > show a first panel (Presentacion). It has a button and when I click this
    > button it remove the current panel and show the second panel (Formulario).
    > This second panel has two JComboBox elements. And, here is the problem,
    > these JComboBox show like an "white space". Don´t show correctly.
    >
    > But, if I show first the panel (Formulario) that has the JComboBox

    elements,
    > these show correctly.
    >
    > The code is the following. Compile and run it to see the problem:
    >
    > import java.awt.Color;
    > import java.awt.event.ActionEvent;
    > import java.awt.event.ActionListener;
    >
    > import javax.swing.JButton;
    > import javax.swing.JComboBox;
    > import javax.swing.JFrame;
    > import javax.swing.JLabel;
    > import javax.swing.JPanel;
    >
    > /* FrameDemo.java requires no other files. */
    > public class Presentacion extends JFrame implements ActionListener{
    >
    > JPanel panelPresentacion = null;
    > JPanel panelFormulario = null;
    > JPanel panelProgreso = null;
    >
    > JButton botoniniciar = null;
    > JButton botonatras = null;
    > JButton botonadelante = null;
    >
    > JComboBox comboNombre = null;
    > JComboBox comboNombre2 = null;
    >
    > static Presentacion presentacion = new Presentacion();
    >
    > public Presentacion() {
    > super("Interfaz Tarjeta");
    > getContentPane().setLayout(null);
    > getContentPane().setBounds(0,0,400,400);
    > getContentPane().setBackground(Color.blue);
    > getContentPane().add(getPanelPresentacion());
    > file://getContentPane().add(getPanelFormulario());
    >
    > pack();
    > setResizable(false);
    > setLocation(200, 200);
    > setSize(400,400);
    > setVisible(true);
    > }
    >
    >
    >
    > public JPanel getPanelPresentacion() {
    >
    > if (panelPresentacion==null)
    > {
    > panelPresentacion= new JPanel();
    > panelPresentacion.setBounds(0,2, 600,600);
    > panelPresentacion.setBackground(Color.gray.brighter());
    > panelPresentacion.setLayout(null);
    >
    >
    > JLabel l = new JLabel("Bienvenido...");
    > l.setBounds(10,40,100,25);
    > panelPresentacion.add(l);
    > panelPresentacion.add(getBotonIniciar());
    >
    > }
    > getRootPane().setDefaultButton(getBotonIniciar());
    > return panelPresentacion;
    > }
    >
    >
    >
    > public JPanel getPanelFormulario() {
    >
    > if (panelFormulario == null)
    > {
    > panelFormulario= new JPanel();
    > panelFormulario.setBounds(0,2, 400,400);
    > panelFormulario.setBackground(Color.gray.brighter());
    > panelFormulario.setLayout(null);
    >
    > panelFormulario.add(getCombo());
    > panelFormulario.add(getCombo2());
    >
    > panelFormulario.add(getBotonAtras());
    > }
    > return panelFormulario;
    > }
    >
    >
    >
    > public JComboBox getCombo() {
    > if (comboNombre == null)
    > {
    > comboNombre = new JComboBox();
    > comboNombre.setBounds(10,40,100,20);
    > comboNombre.setBackground(Color.WHITE);
    > comboNombre.addItem("objeto1");
    > comboNombre.addItem("objeto2");
    > comboNombre.addActionListener(this);
    > }
    > return comboNombre;
    > }
    >
    >
    > public JComboBox getCombo2() {
    > if (comboNombre2 == null)
    > {
    > comboNombre2 = new JComboBox();
    > comboNombre2.setBounds(10,100,100,20);
    > comboNombre2.setBackground(Color.WHITE);
    > comboNombre2.addItem("elemento1");
    > comboNombre2.addItem("elemento2");
    > comboNombre2.addActionListener(this);
    > }
    > return comboNombre2;
    > }
    >
    >
    > public JButton getBotonIniciar() {
    >
    > if (botoniniciar==null)
    > {
    > botoniniciar = new JButton("Iniciar");
    > botoniniciar.setBounds(100,100,100,25);
    > botoniniciar.addActionListener(this);
    > }
    > return botoniniciar;
    > }
    >
    >
    > public JButton getBotonAtras() {
    >
    > if (botonatras==null)
    > {
    > botonatras = new JButton("Atrás");
    > botonatras.setBounds(50,200,100,25);
    > botonatras.addActionListener(this);
    > }
    > return botonatras;
    > }
    >
    >
    >
    > public void actionPerformed(ActionEvent event){
    > if (event.getSource() == getBotonIniciar())
    > {
    >
    > presentacion.remove(getPanelPresentacion());
    > System.out.println("Pulsado boton Iniciar");
    > presentacion.getContentPane().add(getPanelFormulario());
    >
    > presentacion.repaint();
    >
    >
    > }
    > else if (event.getSource() == getBotonAtras())
    > {
    > presentacion.remove(getPanelFormulario());
    > System.out.println("Pulsado boton Atras");
    > presentacion.getContentPane().add(getPanelPresentacion());
    >
    > presentacion.repaint();
    > }
    > else if (event.getSource() == getCombo())
    > {
    > System.out.println(getCombo().getSelectedItem());
    > }
    > else if (event.getSource() == getCombo())
    > {
    > System.out.println(getCombo().getSelectedItem());
    > }
    > else if (event.getSource() == getCombo2())
    > {
    > System.out.println(getCombo2().getSelectedItem());
    > }
    > }
    >
    >
    >
    >
    > public static void main(String[] args) {
    > JFrame.setDefaultLookAndFeelDecorated(true);
    >
    > file://Presentacion presentacion = new Presentacion();
    >
    > presentacion.setTitle("Interfaz Smart Card");
    > presentacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > presentacion.setVisible(true);
    >
    > // Cursor en espera //
    >

    file://presentacion.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)
    );
    > // Cursor en espera //
    >
    > }
    > }
    >
    >
    > What is the problem?.
    >
    > Thanks.
    >
    >
    >
     
    Arnaud Berger, May 9, 2005
    #2
    1. Advertising

  3. Derek wrote:
    > What is the problem?.


    Many things, A quick rundown of what I saw at first glance. There might
    be more:

    - No usage of a layout manager

    - pack() without a layout manager in use

    - Superfluous object caching (all this if(<some gui component> ==
    null)), this comes together with no disposing of no longer used GUI
    components

    - Usage of one shared event handler only, instead of separate ones
    (resulting in the need to discriminate via the event source with an
    if/else orgies)

    - At least one superfluous setVisible(true) call, which goes hand in
    hand with showing the window in the constructor, which is usually a no-no.

    - No revalidation when the layout is changed, alternative, no use of a
    CardLayout

    - Creation of the GUI outside of the EDT

    - Messing with the default PLAF colors (that one is arguable)

    Whatever tutorial or book you are using, may I suggest that you get a
    better one? Even Sun's own UI tutorial (which isn't too great) would be
    better.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, May 9, 2005
    #3
  4. Derek

    Guest

    Thomas Weidenfeller wrote:
    > Derek wrote:
    > > What is the problem?.

    >
    > Many things, A quick rundown of what I saw at first glance. There

    might
    > be more:
    >
    > - No usage of a layout manager


    I agree with this. You need to employ an appropriate layout in your
    panel.
    http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
     
    , May 9, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobi Krausl

    JComboBox problem, pse help!

    Tobi Krausl, Nov 27, 2003, in forum: Java
    Replies:
    6
    Views:
    4,366
    Tobi Krausl
    Dec 1, 2003
  2. Allan Mullan

    JComboBox problem

    Allan Mullan, Apr 18, 2004, in forum: Java
    Replies:
    0
    Views:
    535
    Allan Mullan
    Apr 18, 2004
  3. Genevieve
    Replies:
    13
    Views:
    3,762
    Genevieve
    Jul 6, 2005
  4. Replies:
    0
    Views:
    614
  5. timinganalyzer
    Replies:
    4
    Views:
    1,013
    timinganalyzer
    Oct 27, 2008
Loading...

Share This Page