[英]highlight the cell in jtable
我有這個代碼fom stackoverflow 如何在jtable中突出顯示多個單元格 :
private static class CellHighlighterRenderer extends JLabel implements TableCellRenderer {
public CellHighlighterRenderer() {
setOpaque(true); // Or color won't be displayed!
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
String val = (String)value;
Color c;
if (val.matches(".*MIN.*")) // Add a method to configure the regexpr
c = Color.YELLOW; // Add a method to configure color
else
c = UIManager.getColor("Table.background");
setBackground(c);
setText(val);
return this;
}
}
但是當我用它來突出顯示一個單元格時,它會給出錯誤的操作,就像整個數據丟失一樣。 我剛接觸java swing。 請幫助在按鈕按下操作事件中突出顯示單元格。
更新:添加我的示例代碼:
package myPackage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
public class JTableCreatingDemo {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = { { "Row1-Column1"},
{ "Row2-Column1" } ,{ "Row3-Column1"},{ "Row4-Column1"},};
Object columnNames[] = { "Column One" };
final JTable table = new JTable(rowData, columnNames);
JButton button = new JButton("Highlight cell-1");
//Add action listener to button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
table.setDefaultRenderer(Object.class, new CellHighlighterRenderer());
}
});
JPanel pnl = new JPanel();
pnl.add(button);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(pnl,BorderLayout.SOUTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class CellHighlighterRenderer extends JLabel implements TableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public CellHighlighterRenderer() {
setOpaque(true); // Or color won't be displayed!
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
String val = (String)value;
Color c;
if (isSelected) // Add a method to configure the regexpr
c = Color.YELLOW; // Add a method to configure color
else
c = UIManager.getColor("Table.background");
setBackground(c);
setText(val);
return this;
}
}
我想要的是單擊按鈕我想要突出顯示單元格編號-1(Row1-Column1)。
我使用這個類來設置JTable的樣式
public class CellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if (isSelected)
cell.setBackground(Color.YELLOW);
else if (column == 0)
cell.setBackground(new Color(0xDDDDD));
else
cell.setBackground(new Color(0xFFFFFF));
return cell;
}
創建此類的實例並將其應用於您需要設置樣式的單元格。 您可以使用isSelected參數編輯單元格高亮顏色。
編輯
感謝您的更新示例,以下是更改單元格渲染器的切換按鈕的示例
首先使用默認表格單元格渲染器為單元格創建顏色樣式
public class CellHighlighterRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
cell.setBackground(Color.YELLOW);
return cell;
}
創建您的JFrame並添加JTable和按鈕
public class Main extends JFrame {
public Main() {
super("Table Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setLocationRelativeTo(null);
setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel();
model.setColumnCount(5);
model.setRowCount(5);
JTable table = new JTable();
table.setModel(model);
//Get an instance of the column and the style to apply and hold a default style instance
final TableColumn column = table.getColumnModel().getColumn(1);
final CellHighlighterRenderer cellRenderer = new CellHighlighterRenderer();
final TableCellRenderer defaultRenderer = column.getCellRenderer();
//Now in your button listener you can toggle between the styles
JButton button = new JButton("Click!");
button.addActionListener(new ActionListener() {
private boolean clicked = false;
@Override
public void actionPerformed(ActionEvent e) {
if (clicked) {
column.setCellRenderer(cellRenderer);
clicked = false;
} else {
column.setCellRenderer(defaultRenderer);
clicked = true;
}
repaint(); //edit
}
});
getContentPane().add(table, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.NORTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
希望這可以幫助
編輯我添加了一個重繪清理最后一個例子。 如果您只想定位特定單元格,請將表格單元格渲染器更改為僅渲染所需的單元格
@Override
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
//add condition for desired cell
if (row == 1 && column == 1)
cell.setBackground(Color.YELLOW);
return cell;
}
有多種方法可以實現,在您的,非常特殊的情況下,您想要突出顯示特定的行和列,您可以使用...
class CellHighlighterRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public CellHighlighterRenderer() {
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row == 0 && column == 0) {
setBackground(Color.YELLOW);
setOpaque(true);
} else {
setBackground(table.getBackground());
setOpaque(isSelected);
}
return this;
}
}
現在,如果要突出顯示整行,則需要替換
if (row == 0 && column == 0) {
同
if (row == 0) {
使用渲染器掌握的一個更難的概念是需要在每次迭代時完全重置組件的狀態。 基本上這意味着,不是假設已經為當前迭代正確設置了屬性,並確保您沒有使用的默認值...
有關更多詳細信息,請參閱使用自定義渲染器 ...
你可以嘗試這個:
首先,在表格代碼中:
HighlightCellRenderer renderer = new HighlightCellRenderer();
table.setDefaultRenderer(String.class, renderer);
table.setDefaultRenderer(Number.class, renderer);
table.setDefaultRenderer(Boolean.class, renderer);
table.setDefaultRenderer(Character.class, renderer);
然后:
公共類HighlightCellRenderer擴展DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if (row == table.getSelectedRow()) {
if (column == table.getSelectedColumn()) {
// special color
cell.setBackground(Color.GREEN);
} else {
// The selected row color in "com.jtattoo.plaf.aero.AeroLookAndFeel"
cell.setBackground(new Color(176, 196, 222));
}
} else {
// Other rows
cell.setBackground(Color.WHITE);
}
return cell;
}
}
您可以嘗試不同的方法,而不是使用單元格渲染器,您可以手動選擇這樣的JTable單元格
table.setCellSelectionEnabled(true); //Enable single cell selection
table.addRowSelectionInterval(1, 1); // select rows
table.setColumnSelectionInterval(1, 1); // select columns
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.