如何清除 jComboBox 中的空格
How to clean whitespaces in jComboBox
我正在尝试制作一个包含书名的 jComboBox,当我点击 "loan a book" 按钮时,该书不再出现。
我能够完成所有这些工作,但是当我 "lend a book" 时,它所在的位置有一个空白 space。
这是我试过的代码:
private void cargarLibros()
{
String[] libros = new String[this.librosDisponibles()]; //librosDisponibles() returns the amount of books available
for(int i=0; i<this.librosDisponibles(); i++)
{
if(!(this.getBiblioteca().getLibros().get(i).prestado()))
{
libros[i] = this.getBiblioteca().getLibros().get(i).getTitulo(); //get the titles
}
}
jComboBox3.removeAll();
DefaultComboBoxModel modelo = new DefaultComboBoxModel(libros);
this.jComboBox3.setModel(modelo);
}
也试过这个:
private void cargarLibros()
{
String[] libros = new String[this.librosDisponibles()];
for(int i=0; i<this.librosDisponibles(); i++)
{
if(!(this.getBiblioteca().getLibros().get(i).prestado()))
{
libros[i] = this.getBiblioteca().getLibros().get(i).getTitulo();
}
}
DefaultComboBoxModel modelo = (DefaultComboBoxModel)jComboBox3.getModel();
modelo.removeAllElements();
for(String libro : libros)
{
modelo.addElement(libro);
}
jComboBox3.setModel(modelo);
}
我得到了他们两个的结果:
Picking a book
Borrowed book
and when i hit a button that "loan a book", that book no longer appear.
您需要从组合框的模型中删除所选项目。
因此组合框的 ActionListener 中的代码类似于:
JComboBox comboBox = (JComboBox)e.getSource();
DefaultComboBoxModel model = (DefaultComboBoxModel)comboBox.getModel();
Object item = comboBox.getSelectedItem();
model.removeElement( item );
我正在尝试制作一个包含书名的 jComboBox,当我点击 "loan a book" 按钮时,该书不再出现。 我能够完成所有这些工作,但是当我 "lend a book" 时,它所在的位置有一个空白 space。 这是我试过的代码:
private void cargarLibros()
{
String[] libros = new String[this.librosDisponibles()]; //librosDisponibles() returns the amount of books available
for(int i=0; i<this.librosDisponibles(); i++)
{
if(!(this.getBiblioteca().getLibros().get(i).prestado()))
{
libros[i] = this.getBiblioteca().getLibros().get(i).getTitulo(); //get the titles
}
}
jComboBox3.removeAll();
DefaultComboBoxModel modelo = new DefaultComboBoxModel(libros);
this.jComboBox3.setModel(modelo);
}
也试过这个:
private void cargarLibros()
{
String[] libros = new String[this.librosDisponibles()];
for(int i=0; i<this.librosDisponibles(); i++)
{
if(!(this.getBiblioteca().getLibros().get(i).prestado()))
{
libros[i] = this.getBiblioteca().getLibros().get(i).getTitulo();
}
}
DefaultComboBoxModel modelo = (DefaultComboBoxModel)jComboBox3.getModel();
modelo.removeAllElements();
for(String libro : libros)
{
modelo.addElement(libro);
}
jComboBox3.setModel(modelo);
}
我得到了他们两个的结果:
Picking a book
Borrowed book
and when i hit a button that "loan a book", that book no longer appear.
您需要从组合框的模型中删除所选项目。
因此组合框的 ActionListener 中的代码类似于:
JComboBox comboBox = (JComboBox)e.getSource();
DefaultComboBoxModel model = (DefaultComboBoxModel)comboBox.getModel();
Object item = comboBox.getSelectedItem();
model.removeElement( item );