简体   繁体   English

找到JTable单元并进行绘制

[英]Find the JTable cell and paint it

I have these data: 我有这些数据:

  1. Row Number 行号
  2. Column Number 列号
  3. Cell Value 单元格值

My questions are: 我的问题是:

  1. How can I find the cell by using those data? 如何使用这些数据找到单元格?
  2. How can I change the background of JTable cell on mouse press event and back to normal background on mouse release Event? 如何在鼠标按下事件时将JTable单元格的背景更改为鼠标释放事件时的正常背景?
  3. Can i Highlight the JTable without user interaction, means clicking on some other JTable cell i want to highlight another JTable cell by using given information, is it possible? 我可以在没有用户交互的情况下突出显示JTable吗,这意味着我可以通过使用给定的信息单击要突出显示另一个JTable单元格的其他JTable单元格,这可能吗?

Assuming you mean to find the rectangle of the cell for hit detection: 假设您的意思是找到用于点击检测的单元格的矩形:

 Rectangle cell = table.getCellRect(row, column, false);

For background changing, in your mouseListener code, set a marker which cell was hit, repaint on pressed/released and implement a custom renderer which checks for the marker. 为了更改背景,请在mouseListener代码中设置一个标记,单击哪个单元格,对按下/释放的单元进行重新绘制,并实现用于检查该标记的自定义渲染器。 Some pseudo-code 一些伪代码

 void mousePressed(MouseEvent ev) {
     // get the row/column from mouse location
     int column = table.columnAtPoint(ev.getPoint());
     int row = table.rowAtPoint(ev.getPoint());
     // set some kind of marker, f.i. as client property
     table.putClientProperty("hitColumn", column);
     table.putClientProperty("hitRow", row);
     // get the rectangle for repainting 
     Rectangle cell = table.getCellRect(column, row, false);
     table.repaint(cell);
 }

 void mouseReleased(MouseEvent ev) {
     // similar to reset the marker
     ....
     table.repaint(cell);
 }

 // custom renderer extends DefaultTableCellRenderer

 JComponent getTableCellRendererComponent(..., row, column ...) {
     Integer hitColumn = table.getClientProperty("hitColumn");
     Integer hitRow = ....
     if (hitColumn != null && column == hitColumn && hitRow ....) {
        setBackground(hitColor);
     } else {
         // force super to handle the background 
         setBackground(null);
     }
     return super.getTableCellRendererComponent(....);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM