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