0% found this document useful (0 votes)
38 views2 pages

Componentes GUI JComboBox

This document defines a Java class called ComboBox that illustrates the use of the JComboBox component. It creates a JComboBox with options for different colors, and when an option is selected it changes the background color of a JLabel to match the selected color. It defines the ComboBox class with methods to set up the GUI, add items and handlers to the JComboBox, and change the label color. It also defines a main class to instantiate and display the ComboBox frame.

Uploaded by

Héctor Nossa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

Componentes GUI JComboBox

This document defines a Java class called ComboBox that illustrates the use of the JComboBox component. It creates a JComboBox with options for different colors, and when an option is selected it changes the background color of a JLabel to match the selected color. It defines the ComboBox class with methods to set up the GUI, add items and handlers to the JComboBox, and change the label color. It also defines a main class to instantiate and display the ComboBox frame.

Uploaded by

Héctor Nossa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EJERCICIO ILUSTRATIVO DEL USO DEL COMPONENTE JComboBox

Definicin de la clase ComboBox

package interfaz10;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBox extends JFrame {


private JComboBox listaColores;
private JLabel etq1;
private String nombreColor[] = {"azul","rojo","verde"};
private Color colores[] = {Color.BLUE, Color.RED, Color.GREEN};
public ComboBox()
{
super( "Prueba ComboBox");
Container contenedor = getContentPane();
FlowLayout esquema = new FlowLayout();
contenedor.setLayout( esquema );
etq1 = new JLabel ( "Texto");
etq1.setHorizontalAlignment(SwingConstants.CENTER);
etq1.setOpaque(true);
etq1.setBackground(Color.BLUE);
etq1.setForeground(Color.WHITE);
etq1.setPreferredSize(new Dimension(200, 30));
contenedor.add( etq1 );
listaColores = new JComboBox(nombreColor);
listaColores.setMaximumRowCount(3);
contenedor.add(listaColores);
ManejadorComboBox manejador = new ManejadorComboBox();
listaColores.addItemListener( manejador );
setSize(400,300);
setVisible( true );
}

// Clase interna privada para manejar eventos tipo ItemEvent


private class ManejadorComboBox
implements ItemListener { // Inicio ManejadorComboBox
public void itemStateChanged ( ItemEvent evento) {
if (evento.getStateChange() == ItemEvent.SELECTED)
{
int idx = listaColores.getSelectedIndex();
etq1.setBackground(colores[idx]);
}
}
} // Fin ManejadorComboBox

Definicin de la clase main

package interfaz10;
import javax.swing.JFrame;
public class Interfaz10 {
public static void main(String[] args) {
ComboBox a = new ComboBox();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

You might also like