我在JTable
面临一些问题。
我正在做的是:动态移动表行并动态更改行的颜色。我面临以下一些问题:
所以,我希望整行动态着色,行选择应随行移动。
不知怎的,我设法更新了整行的颜色,
但我仍然有选择行的问题。
以下是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class NewClass extends JFrame {
private JTable table;
private JScrollPane scrollPane;
private DefaultTableModel defaultTableModel;
public NewClass() {
setLocationByPlatform(true);
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 700));
setTitle("Table Issues");
setDefaultCloseOperation(EXIT_ON_CLOSE);
createTableModel();
createTable();
table.setModel(defaultTableModel);
scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
}
private void createTable() {
table = new JTable() {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) comp;
if (!isRowSelected(row)) {
int modelRow = convertRowIndexToModel(row);
int i = Integer.parseInt((String) getModel().getValueAt(modelRow, 2));
if (i == 0) {
jc.setBackground(Color.orange);
} else if (i == 1 || i == 2) {
jc.setBackground(Color.yellow);
} else if (i == 3) {
jc.setBackground(Color.green);
} else {
jc.setBackground(Color.white);
}
}
return comp;
}
};
}
private void createTableModel() {
Vector cols = new Vector();
cols.add("1");
cols.add("2");
cols.add("3");
cols.add("4");
cols.add("5");
Vector rows = new Vector();
for (int i = 0; i < 50; i++) {
Vector row = new Vector();
row.add((i + 1) + "");
row.add("B");
int r = (int) (Math.random() * 5);
row.add(r + "");
row.add("D");
row.add("E");
rows.add(row);
}
defaultTableModel = new DefaultTableModel(rows, cols) {
Class[] types = new Class[]{
String.class, String.class, String.class, String.class, String.class
};
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (Exception e) {
}
final Object lock = new Object();
final NewClass nc = new NewClass();
nc.setVisible(true);
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
synchronized (lock) {
int i = (int) (Math.random() * 5);
int row = (int) (Math.random() * 50);
int move = (int) (Math.random() * 50);
nc.defaultTableModel.setValueAt(i + "", row, 2);
nc.defaultTableModel.moveRow(row, row, move);
nc.defaultTableModel.setValueAt(i + "", row, 2);
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
synchronized (lock) {
int i = (int) (Math.random() * 5);
int row = (int) (Math.random() * 50);
int move = (int) (Math.random() * 50);
nc.defaultTableModel.setValueAt(i + "", row, 2);
nc.defaultTableModel.moveRow(row, row, move);
nc.defaultTableModel.setValueAt(i + "", row, 2);
}
}
}
}).start();
while (true) {
synchronized (lock) {
int i = (int) (Math.random() * 5);
int row = (int) (Math.random() * 50);
int move = (int) (Math.random() * 50);
nc.defaultTableModel.setValueAt(i + "", row, 2);
nc.defaultTableModel.moveRow(row, row, move);
nc.defaultTableModel.setValueAt(i + "", row, 2);
if (i % 2 == 1) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
}
}
在此代码中,当您选择行时,在行中移动后,所选行将是另一行,即行选择不随行移动而移动。
请帮我解决这个问题。
答案 0 :(得分:0)
我通过覆盖setValueAt
的{{1}}方法解决了对着色行的问题。重写方法是:
DefaultTableModel
问题可能是由于多个线程做了大量工作,而EDT可能没有时间来更新整行。通过@Override
public void setValueAt(Object aValue, int row, int column) {
Vector rowVector = (Vector)dataVector.elementAt(row);
rowVector.setElementAt(aValue, column);
// update whole row instead of single cell
fireTableRowsUpdated(row, row);
}
的{{1}}方法设置值,由于单元格更新事件的触发,该列正在着色。
但是我仍然有选择问题而不是在表格中移动行。
为行选择问题打开新线程。