Problem with JComboBox

D

Derek

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.
 
A

Arnaud Berger

Hi,

Try putting :

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

Regards,

Arnaud


Derek said:
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.
 
T

Thomas Weidenfeller

Derek said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top