Foro sobre Java SE > Dejar a null el primer elemento de un jCombobox
no entiendo lo de que se quede en blanco.... Te refieres a que cuando pinches en una opción se borre todos los items?? Explica un poco mejor y podremos ayudarte ;)
Disculpen por explicarme tan mal. No podia pararme a explicarme mejor.
Ahora me esplicare con mas detalles.
Tengo un jComboBox editable y quiero que cuando se haga click sobre el se borre el texto del indice 0 dejandolo en Blanco. Al igual que pasa con un campo de Texto de html que lleva un texto gris "Escriba aqui" y cuando clikeas sobre el campo del formulario se borra el "Escriba aqui" . Solo que yo lo quiero en un jCombobox .
Mira, intenta poner este código en el listener del combobox:
if(this.jComboBox1.getSelectedIndex()==0){
this.jComboBox1.setSelectedItem("");
}
donde jComboBox1 será el nombre de tu JComboBox. Simplemente lo que hace, es que cuando se elige la primera opción, el texto del JCombo se queda vacío para poder escribir, a ver si eso te vale. ;)
No me funcionan los eventos pero e encontrado uno que si funciona. Aqui copio y pego lo que he encontrado ---->
/* INICIO */
El problema está en que el componente que toma el foco en el caso de que cambies algo es el editor interno. Tienes que obtener una referencia a ese editor y asignarle a este el evento:
Código Java:
Ver original
box.getEditor().getEditorComponent().addKeyListener(
new KeyAdapter(){
@Override
public void keyTyped (KeyEvent e){
System.out.println("Presiono" + e.getKeyChar());
}} );
/* FIN */
Este evento si me funciona en mi jComboBox editable. Necesitaría saber como añadir el evento "Ganar Foco" , espero que podáis darme una solución modificando el código que he conseguido por la web. Porque llevo horas buscando y esto es lo que mas se aproxima a lo que necesito. Un saludo.
cboPrueba = new javax.swing.JComboBox();
//AutoCompleteDecorator.decorate(this.cboPrueba);
cboPrueba.setEditable(true);
cboPrueba.getEditor().getEditorComponent().addKeyListener(
new KeyAdapter(){
@Override
public void keyTyped (KeyEvent e){
System.out.println("Presiono: " + e.getKeyChar());
System.out.println(cboPrueba.getSelectedIndex());
System.out.println(cboPrueba.getSelectedItem());
if(cboPrueba.getSelectedIndex()==0){
cboPrueba.setSelectedItem("");
}
}} );
Buenos días.
Tengo creado con netbeans un jcombobox editable . En primer elemento ( indice 0 ) pone
"Tipo de Hardware" . Quiero que cuando se pinche en el "con el ratón" se quede en blanco.
Mi codigo
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package proyecto.incidencias;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import org.jdesktop.swingx.autocomplete.ObjectToStringConverter;
/**
*
* @author dpardobe
*/
public class Inventario extends javax.swing.JInternalFrame {
/**
* Creates new form NewJInternalFrame
*/
public Inventario() {
initComponents();
ListarCombo();
AutoCompleteDecorator.decorate(cboHardwareTipo);
}
private void ListarCombo(){
cboHardwareTipo.addItem("PC");
cboHardwareTipo.addItem("Pantalla");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTabbedPane3 = new javax.swing.JTabbedPane();
jPanel7 = new javax.swing.JPanel();
jTabbedPane4 = new javax.swing.JTabbedPane();
jPanel8 = new javax.swing.JPanel();
jTabbedPane5 = new javax.swing.JTabbedPane();
jPanel10 = new javax.swing.JPanel();
jPanel11 = new javax.swing.JPanel();
cboHardwareTipo = new javax.swing.JComboBox();
btHardwareTipoAñadir = new javax.swing.JButton();
btHardwareTipoBorrar = new javax.swing.JButton();
jPanel12 = new javax.swing.JPanel();
jPanel13 = new javax.swing.JPanel();
jPanel9 = new javax.swing.JPanel();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jPanel5 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jTabbedPane2 = new javax.swing.JTabbedPane();
jPanel14 = new javax.swing.JPanel();
jPanel15 = new javax.swing.JPanel();
jPanel16 = new javax.swing.JPanel();
jPanel17 = new javax.swing.JPanel();
jPanel18 = new javax.swing.JPanel();
jTabbedPane7 = new javax.swing.JTabbedPane();
jPanel23 = new javax.swing.JPanel();
jPanel24 = new javax.swing.JPanel();
jPanel25 = new javax.swing.JPanel();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("Inventario");
setToolTipText("");
setAutoscrolls(true);
setMaximumSize(new java.awt.Dimension(1200, 1000));
setPreferredSize(new java.awt.Dimension(1100, 900));
javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
jPanel10.setLayout(jPanel10Layout);
jPanel10Layout.setHorizontalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel10Layout.setVerticalGroup(
jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane5.addTab("VER", jPanel10);
cboHardwareTipo.setEditable(true);
cboHardwareTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tipo Hardware" }));
cboHardwareTipo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
cboHardwareTipoMouseClicked(evt);
}
});
cboHardwareTipo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cboHardwareTipoActionPerformed(evt);
}
});
btHardwareTipoAñadir.setText("+");
btHardwareTipoBorrar.setText("-");
javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
jPanel11.setLayout(jPanel11Layout);
jPanel11Layout.setHorizontalGroup(
jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel11Layout.createSequentialGroup()
.addContainerGap()
.addComponent(cboHardwareTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btHardwareTipoAñadir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btHardwareTipoBorrar)
.addContainerGap(772, Short.MAX_VALUE))
);
jPanel11Layout.setVerticalGroup(
jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel11Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cboHardwareTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btHardwareTipoAñadir)
.addComponent(btHardwareTipoBorrar))
.addContainerGap(810, Short.MAX_VALUE))
);
jTabbedPane5.addTab("AÑADIR", jPanel11);
javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
jPanel12.setLayout(jPanel12Layout);
jPanel12Layout.setHorizontalGroup(
jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel12Layout.setVerticalGroup(
jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane5.addTab("MODIFICAR", jPanel12);
javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
jPanel13.setLayout(jPanel13Layout);
jPanel13Layout.setHorizontalGroup(
jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel13Layout.setVerticalGroup(
jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane5.addTab("ELIMINAR", jPanel13);
javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
jPanel8.setLayout(jPanel8Layout);
jPanel8Layout.setHorizontalGroup(
jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane5)
);
jPanel8Layout.setVerticalGroup(
jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane5)
);
jTabbedPane4.addTab("HARDWARE", jPanel8);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane1.addTab("VER", jPanel1);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane1.addTab("VINCULAR", jPanel2);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane1.addTab("AÑADIR", jPanel3);
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane1.addTab("MODIFICAR", jPanel4);
javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1069, Short.MAX_VALUE)
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 844, Short.MAX_VALUE)
);
jTabbedPane1.addTab("ELIMINAR", jPanel5);
javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
jPanel9.setLayout(jPanel9Layout);
jPanel9Layout.setHorizontalGroup(
jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
jPanel9Layout.setVerticalGroup(
jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1)
);
jTabbedPane4.addTab("SOFTWARE", jPanel9);
javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
jPanel7.setLayout(jPanel7Layout);
jPanel7Layout.setHorizontalGroup(
jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane4)
);
jPanel7Layout.setVerticalGroup(
jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane4)
);
jTabbedPane3.addTab("MATERIAL", jPanel7);
javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
jPanel14.setLayout(jPanel14Layout);
jPanel14Layout.setHorizontalGroup(
jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel14Layout.setVerticalGroup(
jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane2.addTab("VER", jPanel14);
javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
jPanel15.setLayout(jPanel15Layout);
jPanel15Layout.setHorizontalGroup(
jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel15Layout.setVerticalGroup(
jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane2.addTab("AÑADIR", jPanel15);
javax.swing.GroupLayout jPanel16Layout = new javax.swing.GroupLayout(jPanel16);
jPanel16.setLayout(jPanel16Layout);
jPanel16Layout.setHorizontalGroup(
jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel16Layout.setVerticalGroup(
jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane2.addTab("MODIFICAR", jPanel16);
javax.swing.GroupLayout jPanel17Layout = new javax.swing.GroupLayout(jPanel17);
jPanel17.setLayout(jPanel17Layout);
jPanel17Layout.setHorizontalGroup(
jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel17Layout.setVerticalGroup(
jPanel17Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane2.addTab("ELIMINAR", jPanel17);
javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
jPanel6.setLayout(jPanel6Layout);
jPanel6Layout.setHorizontalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane2)
);
jPanel6Layout.setVerticalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane2)
);
jTabbedPane3.addTab("PRESUPUESTOS", jPanel6);
javax.swing.GroupLayout jPanel23Layout = new javax.swing.GroupLayout(jPanel23);
jPanel23.setLayout(jPanel23Layout);
jPanel23Layout.setHorizontalGroup(
jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel23Layout.setVerticalGroup(
jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane7.addTab("AÑADIR", jPanel23);
javax.swing.GroupLayout jPanel24Layout = new javax.swing.GroupLayout(jPanel24);
jPanel24.setLayout(jPanel24Layout);
jPanel24Layout.setHorizontalGroup(
jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel24Layout.setVerticalGroup(
jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane7.addTab("MODIFICAR", jPanel24);
javax.swing.GroupLayout jPanel25Layout = new javax.swing.GroupLayout(jPanel25);
jPanel25.setLayout(jPanel25Layout);
jPanel25Layout.setHorizontalGroup(
jPanel25Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1074, Short.MAX_VALUE)
);
jPanel25Layout.setVerticalGroup(
jPanel25Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 872, Short.MAX_VALUE)
);
jTabbedPane7.addTab("ELIMINAR", jPanel25);
javax.swing.GroupLayout jPanel18Layout = new javax.swing.GroupLayout(jPanel18);
jPanel18.setLayout(jPanel18Layout);
jPanel18Layout.setHorizontalGroup(
jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane7)
);
jPanel18Layout.setVerticalGroup(
jPanel18Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane7)
);
jTabbedPane3.addTab("COMPRAS", jPanel18);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane3)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane3)
);
pack();
}// </editor-fold>
private void cboHardwareTipoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println((String)cboHardwareTipo.getSelectedItem());
if((String)cboHardwareTipo.getSelectedItem()==("Tipo Hardware")){
}
}
private void cboHardwareTipoMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.out.println((String)cboHardwareTipo.getSelectedItem());
if((String)cboHardwareTipo.getSelectedItem()==("Tipo Hardware")){
}
}
// Variables declaration - do not modify
private javax.swing.JButton btHardwareTipoAñadir;
private javax.swing.JButton btHardwareTipoBorrar;
private javax.swing.JComboBox cboHardwareTipo;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel10;
private javax.swing.JPanel jPanel11;
private javax.swing.JPanel jPanel12;
private javax.swing.JPanel jPanel13;
private javax.swing.JPanel jPanel14;
private javax.swing.JPanel jPanel15;
private javax.swing.JPanel jPanel16;
private javax.swing.JPanel jPanel17;
private javax.swing.JPanel jPanel18;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel23;
private javax.swing.JPanel jPanel24;
private javax.swing.JPanel jPanel25;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel jPanel8;
private javax.swing.JPanel jPanel9;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTabbedPane jTabbedPane2;
private javax.swing.JTabbedPane jTabbedPane3;
private javax.swing.JTabbedPane jTabbedPane4;
private javax.swing.JTabbedPane jTabbedPane5;
private javax.swing.JTabbedPane jTabbedPane7;
// End of variables declaration
}