我使用的是http://www.jidesoft.com/的Sortable table。由于需要,我们只需要使用这一个。
我们需要突出显示,并获得行数时,用户选择复选框,这是呈现为JTable的一部分。
我的用户界面(我原来的用户界面是不同的,只是增加了可重复的问题)。
我期望checkbox作为第一列可见,但它不是。我写了下面的代码来实现这一点。
public void adjustTable() {
columnModel.getColumn(0).setCellEditor(new BooleanCheckBoxCellEditor());
columnModel.getColumn(0).setCellRenderer(new BooleanCheckBoxCellRenderer());
}
上面的代码是工作,如果我们注解掉(复选框是可见的)
setDefaultCellRenderer(new CustomRenderer());
从StudentSortableTable的构造函数。
复选框可见
但当我继续应用突出显示功能并取消注解时
setDefaultCellRenderer(new CustomRenderer());
渲染器代码从StudentSortableTable我可以看到复选框是不可见的,只有真/假是渲染。就像combobox一样。当我单击true时,它会突出显示行。
具有选定True的UI
无法弄清楚如何显示复选框,而不是如果组合框。
我的源代码:
public class JTableDemo extends JFrame {
private StudentSortableTable studentSortableTable;
private JTableDemo() {
setBounds(300, 200, 550, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
initializeComponent();
}
void initializeComponent() {
JPanel panel = new JPanel();
studentSortableTable = new StudentSortableTable(getDummyData());
studentSortableTable.adjustTable();
studentSortableTable.setAutoCreateRowSorter(true);
panel.add(new JScrollPane(studentSortableTable));
add(panel);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JTableDemo object = new JTableDemo();
object.setVisible(true);
}
private List<Student> getDummyData() {
List<Student> students = new ArrayList<>();
Student s1 = new Student(false, "Test User1");
Student s2 = new Student(false, "Test User2");
Student s3 = new Student(false, "Test User3");
Student s4 = new Student(false, "Test User4");
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
return students;
}
}
public class StudentModel extends DefaultTableModel {
private static final long serialVersionUID = 1L;
private boolean inlcludeSelect = true;
private final List<String> columns;
private final List<Student> studentData;
private static final List<String> HEAD_COLUMNS = Arrays.asList("SELECTED", "NAME");
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
default:
return super.getColumnClass(column);
}
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
public StudentModel(List<Student> studentData) {
columns = new ArrayList<>();
columns.addAll(HEAD_COLUMNS);
this.studentData = studentData;
setDataVector(this.studentData);
}
private void setDataVector(List<Student> StudentArray) {
Object[][] tableModel = new Object[StudentArray.size()][columns.size()];
for (int rowNumber = 0; rowNumber < StudentArray.size(); rowNumber++) {
int columNumber = 0;
Student stdudentDataObject = StudentArray.get(rowNumber);
if (stdudentDataObject != null) {
tableModel[rowNumber][columNumber] = (Boolean) stdudentDataObject.isIsSelected();
columNumber++;
tableModel[rowNumber][columNumber] = stdudentDataObject.getName();
}
}
setDataVector(tableModel, columns.toArray());
}
public Student getStudentData(int row) {
return this.studentData.get(row);
}
public int getSelectedColumn() {
return inlcludeSelect ? columns.indexOf("SELECTED") : -1;
}
}
public final class StudentSortableTable extends SortableTable {
private static final long serialVersionUID = 1L;
private StudentModel studentModel;
private final List<Student> studentData;
public StudentSortableTable(List<Student> studentData) {
super();
this.studentData = studentData;
updateModel(this.studentData);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setDefaultCellRenderer(new CustomRenderer());
}
public void updateModel(List<Student> studentData) {
studentModel = new StudentModel(studentData);
setModel(studentModel);
}
public void adjustTable() {
columnModel.getColumn(0).setCellEditor(new BooleanCheckBoxCellEditor());
columnModel.getColumn(0).setCellRenderer(new BooleanCheckBoxCellRenderer());
}
private class CustomRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = null;
c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (studentModel.getValueAt(row, 0) == Boolean.TRUE) {
c.setBackground(Color.orange);
} else {
c.setBackground(Color.WHITE);
}
return c;
}
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
return c;
}
}
}
public class Student {
String name;
boolean isSelected;
public boolean isIsSelected() {
return isSelected;
}
public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public Student(boolean selected, String name) {
this.isSelected = selected;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我也在想如何从所有的行中得到选中的计数。
编辑修改后
void initializeComponent() {
JPanel panel = new JPanel();
studentSortableTable = new SortableTable(new StudentModel(getDummyData()));
studentSortableTable.getColumnModel().getColumn(0).setCellEditor(new BooleanCheckBoxCellEditor());
studentSortableTable.getColumnModel().getColumn(0).setCellRenderer(new BooleanCheckBoxCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value.equals(Boolean.TRUE)) {
c.setBackground(Color.orange);
} else {
c.setBackground(Color.WHITE);
}
return c;
}
});
studentSortableTable.setAutoCreateRowSorter(true);
panel.add(new JScrollPane(studentSortableTable));
add(panel);
}
Only color changes for first column
编辑2:
public class StudentModel extends DefaultTableModel {
private static final long serialVersionUID = 1L;
private final List<String> columns;
private final List<Student> studentData;
private static final List<String> HEAD_COLUMNS = Arrays.asList("SELECTED", "NAME");
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
default:
return super.getColumnClass(column);
}
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
public StudentModel(List<Student> studentData) {
columns = new ArrayList<>();
columns.addAll(HEAD_COLUMNS);
this.studentData = studentData;
setDataVector(this.studentData);
}
private void setDataVector(List<Student> StudentArray) {
Object[][] tableModel = new Object[StudentArray.size()][columns.size()];
for (int rowNumber = 0; rowNumber < StudentArray.size(); rowNumber++) {
int columNumber = 0;
Student stdudentDataObject = StudentArray.get(rowNumber);
if (stdudentDataObject != null) {
tableModel[rowNumber][columNumber] = (Boolean) stdudentDataObject.isIsSelected();
columNumber++;
tableModel[rowNumber][columNumber] = stdudentDataObject.getName();
}
}
setDataVector(tableModel, columns.toArray());
}
public Student getStudentData(int row) {
return this.studentData.get(row);
}
}
public class TableDemo extends JFrame {
private JTable studentSortableTable;
private TableDemo() {
setBounds(300, 200, 550, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
initializeComponent();
}
void initializeComponent() {
JPanel panel = new JPanel();
studentSortableTable = new JTable(new StudentModel(getDummyData()));
studentSortableTable.getColumnModel().getColumn(0).setCellEditor(new BooleanCheckBoxCellEditor());
studentSortableTable.getColumnModel().getColumn(0).setCellRenderer(new BooleanCheckBoxCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value.equals(Boolean.TRUE)) {
c.setBackground(Color.orange);
} else {
c.setBackground(Color.WHITE);
}
return c;
}
});
studentSortableTable.setAutoCreateRowSorter(true);
panel.add(new JScrollPane(studentSortableTable));
add(panel);
}
public static void main(String[] args) {
TableDemo object = new TableDemo();
object.setVisible(true);
}
private List<Student> getDummyData() {
List<Student> students = new ArrayList<>();
Student s1 = new Student(false, "Test User1");
Student s2 = new Student(false, "Test User2");
Student s3 = new Student(false, "Test User3");
Student s4 = new Student(false, "Test User4");
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
return students;
}
}
1条答案
按热度按时间6yt4nkrj1#
将为您提供正确的渲染行为。颜色调整可以用正常的方式来完成我不会混淆
Student
类中“被选择”的概念这是一个gui的东西,可能不属于数据模型。我还没有看过他们的Javadoc,但是可以想象,通过API直接实现上述类型的样式,而不是覆盖方法