我已经做了一些调查标志,但我仍然不明白我将如何将它们附加到项目/使用它们。我的目标是使用标记来标记JList中的所有选定条目,以便在执行搜索时,所有显示所选项目都保持选中状态,并且当清除搜索时,所有先前选择的项目都会被重新突出显示。此外,当搜索并选择一个术语时,它会在搜索被清除后保持高亮显示。我被告知旗帜是最好的方法,但我不知道我会怎么做。
答案 0 :(得分:0)
你的问题有点脱离背景,因为我不知道你如何刷新你的观点,例如。
但是我可能会使用JList
而不是JTable
,因为它可以编辑,并且可以为JCheckBox
但是,首先,我会从一个容器对象开始,它既可以包含所选状态,也可以包含我想要与之关联的值...
public class SearchTerm {
private boolean selected;
private String text;
public SearchTerm(boolean selected, String text) {
this.selected = selected;
this.text = text;
}
public boolean isSelected() {
return selected;
}
public String getText() {
return text;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
非常简单,现在因为状态是在对象的内部维护,你可以移动对象,它的状态(以及其他内容)将保持完好。
Nopw,我们需要一些方法对此进行建模,这样可以维护我们想要控制它的逻辑(因此只有复选框是可编辑的)
public class SearchTermTableModel extends AbstractTableModel {
private List<SearchTerm> searchTerms;
public SearchTermTableModel() {
searchTerms = new ArrayList<>(25);
}
public void add(SearchTerm searchTerm) {
searchTerms.add(searchTerm);
fireTableRowsInserted(searchTerms.size() - 1, searchTerms.size() - 1);
}
@Override
public int getRowCount() {
return searchTerms.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public String getColumnName(int column) {
return column == 0 ? "" : "Search Term";
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex == 0 ? Boolean.class : String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
SearchTerm term = searchTerms.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = term.isSelected();
break;
case 1:
value = term.getText();
break;
}
return value;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
if (aValue instanceof Boolean) {
SearchTerm term = searchTerms.get(rowIndex);
term.setSelected((Boolean)aValue);
super.setValueAt(aValue, rowIndex, columnIndex);
}
break;
}
}
}
好的,现在,我们只需要将它放在屏幕上供用户玩......
model = new SearchTermTableModel();
model.add(new SearchTerm(false, "Bananas"));
model.add(new SearchTerm(false, "Apples"));
model.add(new SearchTerm(false, "Streberries"));
model.add(new SearchTerm(false, "Poines with guns"));
model.add(new SearchTerm(false, "Anime"));
model.add(new SearchTerm(false, "Helicopters"));
model.add(new SearchTerm(false, "Sunny beaches which I'm not on"));
setLayout(new BorderLayout());
table = new JTable(model);
add(new JScrollPane(table));
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private SearchTermTableModel model;
public TestPane() {
model = new SearchTermTableModel();
model.add(new SearchTerm(false, "Bananas"));
model.add(new SearchTerm(false, "Apples"));
model.add(new SearchTerm(false, "Streberries"));
model.add(new SearchTerm(false, "Poines with guns"));
model.add(new SearchTerm(false, "Anime"));
model.add(new SearchTerm(false, "Helicopters"));
model.add(new SearchTerm(false, "Sunny beaches which I'm not on"));
setLayout(new BorderLayout());
table = new JTable(model);
add(new JScrollPane(table));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class SearchTerm {
private boolean selected;
private String text;
public SearchTerm(boolean selected, String text) {
this.selected = selected;
this.text = text;
}
public boolean isSelected() {
return selected;
}
public String getText() {
return text;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
public class SearchTermTableModel extends AbstractTableModel {
private List<SearchTerm> searchTerms;
public SearchTermTableModel() {
searchTerms = new ArrayList<>(25);
}
public void add(SearchTerm searchTerm) {
searchTerms.add(searchTerm);
fireTableRowsInserted(searchTerms.size() - 1, searchTerms.size() - 1);
}
@Override
public int getRowCount() {
return searchTerms.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public String getColumnName(int column) {
return column == 0 ? "" : "Search Term";
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex == 0 ? Boolean.class : String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
SearchTerm term = searchTerms.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = term.isSelected();
break;
case 1:
value = term.getText();
break;
}
return value;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
if (aValue instanceof Boolean) {
SearchTerm term = searchTerms.get(rowIndex);
term.setSelected((Boolean) aValue);
super.setValueAt(aValue, rowIndex, columnIndex);
}
break;
}
}
}
}
有关详细信息,请查看How to Use Tables