我面临一些问题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) {
}
}
}
}
}
}
在此代码中,当您选择行时,在行移动后,所选行将是其他行,即行选择不会随着行移动而移动。
请帮助我。