在jtable中突出显示单元格 [英] highlight the cell in jtable

查看:173
本文介绍了在jtable中突出显示单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码fom stackoverflow 如何在jtable中突出显示多个单元格

I have this code fom stackoverflow how to highlight multiple cells in 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。请帮助使按钮按下动作事件突出显示一个单元格。

更新:添加我的示例代码:

But when i use it for highlighting a cell , it gives wrong action like the whole data gets lost . Iam new to java swing. Please help to make a cell gets highlighted on a button press action event.
UPDATE: adding my sample code:

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)。

What i want is on clicking the button i want to highlight just the cell number-1 (Row1-Column1).

推荐答案

我使用此类来设置JTables样式

I use this class to style JTables

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参数编辑单元格高亮颜色。

create an instance of this class and apply it to the cells that you need to style. You can use the isSelected parameter to edit the cell highlight color.

编辑

谢谢您更新的示例,以下是更改单元格渲染器的切换按钮示例

Thanks for your updated example, here is an example of a toggle button to change the cell renderer

首先使用默认表格单元格渲染器为单元格创建颜色样式

First create a color style for the cell using a default table cell renderer

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和按钮

Create your JFrame and add the JTable and button

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();
}

希望这会有所帮助

编辑
我添加了一个重绘来清理最后一个例子。如果您只想定位特定单元格,请将表格单元格渲染器更改为仅渲染所需的单元格

EDIT I added a repaint to clean up the last example. If you only want to target a specific cell change the table cell renderer to only render the cell you need like this

    @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;
}

这篇关于在jtable中突出显示单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆