Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with JComboBox

Reply
Thread Tools

Problem with JComboBox

 
 
Derek
Guest
Posts: n/a
 
      05-09-2005
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.brighte r());
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(getPanelFormular io());

presentacion.repaint();


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

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.



 
Reply With Quote
 
 
 
 
Arnaud Berger
Guest
Posts: n/a
 
      05-09-2005
Hi,

Try putting :

((JComponent)presentacion.getContentPane()).revali date();
before
presentacion.repaint();

Regards,

Arnaud


"Derek" <(E-Mail Removed)> a écrit dans le message news:
d5n5h5$(E-Mail Removed)...
> 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.brighte r());
> 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(getPanelFormular io());
>
> presentacion.repaint();
>
>
> }
> else if (event.getSource() == getBotonAtras())
> {
> presentacion.remove(getPanelFormulario());
> System.out.println("Pulsado boton Atras");
> presentacion.getContentPane().add(getPanelPresenta cion());
>
> 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.
>
>
>



 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      05-09-2005
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/...g/java/gui/faq
 
Reply With Quote
 
epicwinter@hotmail.com
Guest
Posts: n/a
 
      05-09-2005

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/tutor...out/using.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JComboBox problem with new java 6u10 on ubuntu timinganalyzer Java 4 10-27-2008 02:42 PM
Problem/Question Using JComboBox as a tablecelleditor dieselmachine@gmail.com Java 0 01-28-2007 07:38 AM
JComboBox problem - only one element Genevieve Java 13 07-06-2005 02:31 PM
JComboBox problem Allan Mullan Java 0 04-18-2004 12:42 PM
JComboBox problem, pse help! Tobi Krausl Java 6 12-01-2003 08:10 AM



Advertisments