JAVA - 如何在点击后更改 JTable 行的颜色?
JAVA - How to change JTable row color after clicking on it?
我是 Java 初学者。
我创建了一个带有 JTable 的应用程序,其中填充了一个数据库。
在我的数据库中,我有一些 'news'。在我的 JTable 中,我显示 'news' 的标题,当用户单击一行时,它会显示一个包含正确新闻内容的弹出窗口。
但是我想在用户单击它时为 'read' 的单元格着色。
我使用自己的 TableModel。
我希望我清楚...
如果我需要放一些代码,请告诉我什么...
找到了如何在 mouseClick 上获取 table 单元格的示例:http://codeprogress.com/java/showSamples.php?index=52&key=JTableValueofSelectedCell
您可以使用它来获取选定的行和列。
然后您需要创建一个自定义 TableCellRenderer,可能作为一个内部 class 以便它可以使用选定的行和列数据并将单元格的背景设置为您突出显示的颜色
public class JTableTest extends JFrame {
private JTable table;
private int col;
private int rowz;
/**
* Create the frame.
*/
public JTableTest() {
initComponents();
}
private void initComponents() {
/** any other components */
table = new JTable();//create the table
table.setDefaultRenderer(Object.class, new CustomModel());
table.addMouseListener(new CustomListener());
}
public class CustomListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent arg0) {
super.mouseClicked(arg0);
//get the clicked cell's row and column
rowz = table.getSelectedRow();
col = table.getSelectedColumn();
// Repaints JTable
table.repaint();
}
}
public class CustomModel extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Color c = Color.WHITE;//define the color you want
if (isSelected && row == rowz & column == col)
c = Color.GREEN;
label.setBackground(c);
return label;
}
}
}
我是 Java 初学者。 我创建了一个带有 JTable 的应用程序,其中填充了一个数据库。 在我的数据库中,我有一些 'news'。在我的 JTable 中,我显示 'news' 的标题,当用户单击一行时,它会显示一个包含正确新闻内容的弹出窗口。 但是我想在用户单击它时为 'read' 的单元格着色。
我使用自己的 TableModel。
我希望我清楚...
如果我需要放一些代码,请告诉我什么...
找到了如何在 mouseClick 上获取 table 单元格的示例:http://codeprogress.com/java/showSamples.php?index=52&key=JTableValueofSelectedCell
您可以使用它来获取选定的行和列。
然后您需要创建一个自定义 TableCellRenderer,可能作为一个内部 class 以便它可以使用选定的行和列数据并将单元格的背景设置为您突出显示的颜色
public class JTableTest extends JFrame {
private JTable table;
private int col;
private int rowz;
/**
* Create the frame.
*/
public JTableTest() {
initComponents();
}
private void initComponents() {
/** any other components */
table = new JTable();//create the table
table.setDefaultRenderer(Object.class, new CustomModel());
table.addMouseListener(new CustomListener());
}
public class CustomListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent arg0) {
super.mouseClicked(arg0);
//get the clicked cell's row and column
rowz = table.getSelectedRow();
col = table.getSelectedColumn();
// Repaints JTable
table.repaint();
}
}
public class CustomModel extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Color c = Color.WHITE;//define the color you want
if (isSelected && row == rowz & column == col)
c = Color.GREEN;
label.setBackground(c);
return label;
}
}
}