如何创建一个填充有对象列的组合框? (Java)

时间:2019-06-04 15:31:20

标签: java combobox

我有一个名为“ Book”的对象,该对象具有在Microsoft SQL Server上创建的标题,作者,bookID等列。一切都与Netbeans连接并且运行良好。我也有过滤器/搜索选项,该选项也很好用,但我想有一个组合框,其中装有书本列,例如书名,作者等,我想在其中选择一列并仅在其中搜索。

我已经有一个名为class的{​​{1}}返回书(下面的代码),但是我想要该表中的书列,而不是BookComboBoxModel上的Book.toString()方法< / p>

comboBox

2 个答案:

答案 0 :(得分:0)

您可以创建对象(客户)的array,然后将其传递给combobox,如下所示:combo = new JComboBox(customers);

看看这个例子:

import java.awt.BorderLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame {
  JLabel label;
  JComboBox combo;
  public static void main(String args[]) {
    new Main();
  }

  public Main() {
    label = new JLabel("Select a Customer");
    add(label, BorderLayout.NORTH);

    Customer customers[] = new Customer[6];
    customers[0] = new Customer("A", 1);
    customers[1] = new Customer("B", 6);
    customers[2] = new Customer("C", 2);
    customers[3] = new Customer("D", 3);
    customers[4] = new Customer("E", 4);
    customers[5] = new Customer("F", 5);

    combo = new JComboBox(customers);
    combo.addItemListener(e -> {
      Customer c = (Customer) e.getItem();
      label.setText("You selected customer id: " + c.getId());
    });
    JPanel panel = new JPanel();
    panel.add(combo);
    add(panel, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 200);
    setVisible(true);
  }
}

class Customer {
  private String name;
  private int id;

  public Customer(String name, int id) {
    this.name = name;
    this.id = id;
  }

  public String toString() {
    return getName();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

答案 1 :(得分:0)

基于注释,很明显,您想要的不是Book对象的模型,而是包含Book类的属性的模型。

一种好的方法是创建一个小的自定义对象以用作JComboBox项:

public class BookAttribute {
    private final String name;
    private final int columnNumber;

    public BookAttribute(String name,
                         int columnNumber) {

        this.name = Objects.requireNonNull(name, "Name cannot be null");
        this.columnNumber = columnNumber;
    }

    public String getName() {
        return name;
    }

    public int getColumnNumber() {
        return columnNumber;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof BookAttribute) {
            return this.columnNumber == ((BookAttribute) obj).columnNumber;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Integer.hashCode(columnNumber);
    }

    @Override
    public String toString() {
        return name;
    }
}

使用该课程,您不需要自定义模型。您只需将实例直接传递到标准JComboBox构造函数即可:

TableModel model = table.getModel();

int count = model.getColumnCount();
Vector<BookAttribute> fields = new Vector<>(count);
for (int col = 0; col < count; col++) {
    fields.add(new BookAttribute(model.getColumnName(col), col));
}

JComboBox<BookAttribute> fieldList = new JComboBox<>(fields);

要使用它,您可以在表格的RowSorter中添加一个过滤器:

table.setAutoCreateRowSorter(true);

ActionListener filterUpdater = e -> {
    String searchText = searchField.getText();

    BookAttribute bookAttribute =
        (BookAttribute) fieldList.getSelectedItem();
    int columnNumber = bookAttribute.getColumnNumber();

    TableRowSorter<? extends TableModel> sorter =
        (TableRowSorter<? extends TableModel>) table.getRowSorter();

    sorter.setRowFilter(new RowFilter<TableModel, Integer>() {
        @Override
        public boolean include(
            Entry<? extends TableModel, ? extends Integer> entry) {

            return entry.getStringValue(columnNumber).contains(searchText);
        }
    });
};

searchField.addActionListener(filterUpdater);
fieldList.addActionListener(filterUpdater);

(您可以为此使用反射,但是您不应该使用反射。反射是不可靠且容易出错的,因为编译器无法检查其正确性。通常是Java运行时。)