JTable:单击按钮时更改单元格背景 [英] JTable: changing cell background when a button is clicked

查看:135
本文介绍了JTable:单击按钮时更改单元格背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在jtable中更改单元格背景是通过创建一个新的cellrenderer类来完成的。我做到了。我已经阅读了有关DefaultTableRenderer颜色记忆的问题,但我无法弄清楚如何为我的特定目的解决它。

I know changing cell backgrounds in jtable is done by creating a new cellrenderer class. I've done that. I've read about DefaultTableRenderer "color memory" issue, but I can't figure out how to work around it for my particular purpose.

我的目标很简单:单击按钮时,更改jtable中所有选定单元格的背景颜色。

My goal is simple enough: When a button is clicked, change the background color of all selected cells in the jtable.

我已为事件设置了足够的方法调用,但我不能让渲染器以我想要的方式工作。

I have the adequate method calls set up for the event, but I can't get the renderer to work the way I want it to.

我将所有选定的单元存储在TableCells的arraylist中(包含行,列和单元格的文本字符串数据的类)。这是我在CustomCellRenderer中的getTableCellRendererComponent的代码。

I have all selected cells stored in an arraylist of TableCells (a class containing row, column, and the cell's text string data). Here is my code for getTableCellRendererComponent inside of my CustomCellRenderer.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
     for(TableCell c: selectedCells)
     {
         if(c.row ==row && c.col == column)
         {
             this.setBackground(Color.black);
         }
         else
         {
             this.setBackground(Color.BLUE);
         }
     }
     return this;
  }

此代码将所有表格单元格背景的背景设置为蓝色。显然,我需要一些不同的逻辑来解决这个颜色记忆问题。对此的任何想法都会很棒。

This code sets the background of all table cells' backgrounds to blue. Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.

谢谢。

推荐答案

给你的渲染器类一个布尔变量怎么样,比如btnClicked初始化为false但在按钮的ActionListener中设置为true,这个监听器也指示表重绘自己。然后在渲染器本身中,您可以使用selected属性来查看是否选择了单元格。也许是这样的:

How about giving your renderer class a boolean variable, say btnClicked that is initialized to false but set to true in the button's ActionListener, a listener which also instructs the table to repaint itself. Then in the renderer itself, you could use the selected property to see if a cell is selected or not. Perhaps something like:

   private boolean btnClicked = false;

   public void setBtnClicked(boolean btnClicked) {
      this.btnClicked = btnClicked;
   }

   public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
      if (btnClicked) {
         if (isSelected) {
            setBackground(Color.black);
         } else {
            setBackground(Color.blue);
         }
      } else {
         // if button not clicked
         setBackground(Color.lightGray);
      }
      return this;
   }

同样关于:

显然我需要一些不同的逻辑来解决这个颜色记忆问题。关于这一点的任何想法都会很棒。

Obviously I need some different logic to work around this color memory issue. Any ideas on this would be great.

你说的这个彩色内存问题是什么?

What is this "color memory issue that you speak of?

编辑1

这是我的意思的可编辑示例。我仍然不确定你的颜色记忆问题是什么意思,对不起。

Edit 1
Here is a compilable example of what I meant. I'm still not sure what you mean by the color memory issue though, sorry.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

@SuppressWarnings("serial")
public class DisplaySelectedTableCells extends JPanel {
   public static final Integer[][] DATA = {
      {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
      {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
      {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
   };
   public static final String[] COLS = {"A", "B", "C"};
   private static final int PREF_WIDTH = 400;
   private static final int PREF_HEIGHT = 300;
   private DefaultTableModel model = new DefaultTableModel(DATA, COLS) {
      @Override
      public Class<?> getColumnClass(int columnIndex) {
         return Integer.class;
      }
   };
   private JTable table = new JTable(model);
   private JToggleButton toggleBtn = new JToggleButton("Show Selected Rows");
   private MyCellRenderer myCellRenderer = new MyCellRenderer();

   public DisplaySelectedTableCells() {
      table.setDefaultRenderer(Integer.class, myCellRenderer);
      JPanel btnPanel = new JPanel();
      btnPanel.add(toggleBtn);

      setLayout(new BorderLayout());
      add(new JScrollPane(table), BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);

      toggleBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            myCellRenderer.setShowSelected(toggleBtn.isSelected());
            table.repaint();
         }
      });
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_WIDTH, PREF_HEIGHT);
   }

   private static class MyCellRenderer extends DefaultTableCellRenderer {
      private static final Color SELECTED_COLOR = Color.pink;
      private static final Color UNSELECTED_COLOR = Color.lightGray;
      private static final Color BASE_COLOR = null;
      private boolean showSelected = false;

      public void setShowSelected(boolean showSelected) {
         this.showSelected = showSelected;
      }

      @Override
      public Component getTableCellRendererComponent(JTable table,
               Object value, boolean isSelected, boolean hasFocus, int row,
               int column) {
         Component superComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                  row, column);

         if (showSelected) {
            if (isSelected) {
               superComponent.setBackground(SELECTED_COLOR);
            } else {
               superComponent.setBackground(UNSELECTED_COLOR);
            }
         } else {
            superComponent.setBackground(BASE_COLOR);
         }

         return superComponent;
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("DisplaySelectedTableCells");
      frame.getContentPane().add(new DisplaySelectedTableCells());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

这篇关于JTable:单击按钮时更改单元格背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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