与 JTable 中的单元格渲染器交互

发布于 2024-09-24 08:22:45 字数 33 浏览 11 评论 0原文

有没有办法让单元格渲染器响应鼠标事件,例如鼠标悬停?

Is there any way to get a cell renderer to respond to mouse events, such as mouseovers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

八巷 2024-10-01 08:22:45

从未尝试过,但我想您需要:

a)创建一个自定义渲染器以在两种状态下绘制单元格

b)您需要跟踪当前应在“鼠标悬停”状态下绘制哪个单元格

c)添加鼠标监听器跟踪鼠标进入/退出和鼠标移动。对于每个事件,您需要更新一个变量来跟踪鼠标所在的单元格。你
可以使用 JTable 的 columnAtPoint() 和 rowAtPoint() 方法

d) 当鼠标离开单元格时,您需要在单元格上调用 repaint() 。您可以使用 getCellRect() 方法来确定要重绘哪个单元格

e) 当鼠标进入某个单元格时,您需要重置“鼠标悬停”状态的单元格值,然后重新绘制该单元格。

Never tried it but I guess you would need to:

a) create a custom renderer to paint the cell in two states

b) you need to keep track of which cell should currently be painted in the "mouse over" state

c) add a mouse listener to track mouse entered/exited and mouseMoved. With each event you would need to update a variable that tracks which cell the mouse is positioned over. You
can use the columnAtPoint() and rowAtPoint() methods of JTable

d) when the mouse leaves a cell you need to invoke repaint() on the cell. You can use the getCellRect() method to determine which cell to repaint

e) when the mouse enters a cell you need to reset the cell value for the "mouse over" state and then repaint the cell.

佼人 2024-10-01 08:22:45

好的,所以我尝试实现 camickr 的方法,但在这个过程中我遇到了一些非常讨厌的问题。我已向 JTable 添加了一个 MouseMotionListener 来跟踪当前和上一个单元格,并添加了一些方法来指示渲染器返回哪个组件,然后重新绘制相应的单元格。然而,由于某种奇怪的原因,即使只有一个重绘请求,每个单元格也会重绘两次。基本上,我能够在鼠标悬停时突出显示单元格,但是一旦鼠标光标离开单元格,就无法从单元格中删除突出显示。经过最初的困惑后,我决定以不同的方式来做这件事。我添加了一个方法,当鼠标悬停在单元格上时调用编辑器,然后添加一些代码,一旦 JToggleButton(我的渲染和编辑组件)的状态发生更改,这些代码就会停止编辑。我当前的代码如下所示:

package guipkg;

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

public class Grid extends JTable implements MouseListener {

int currentCellColumn = -1;
int currentCellRow = -1;
int previousCellColumn = -1;
int previousCellRow = -1;

public void detectCellAtCursor (MouseEvent e) {
    Point hit = e.getPoint();
    int hitColumn = columnAtPoint(hit);
    int hitRow = rowAtPoint(hit);
    if (currentCellRow != hitRow || currentCellColumn != hitColumn) {
        this.editCellAt(hitRow, hitColumn);
        currentCellRow = hitRow;
        currentCellColumn = hitColumn;
    }

}


}







package guipkg;

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


public class TCEditor extends AbstractCellEditor implements TableCellEditor  {

    /**
     * A toggle button which will serve as a cell editing component
     */

    JToggleButton togglebutton = new JToggleButton();





    public Component getTableCellEditorComponent (JTable Table, Object value, boolean isSelected, int rindex, int cindex) {

        /**
         * We're adding an action listener here to stop editing as soon as the state of JToggleButton is switched.
         * This way data model is updated immediately. Otherwise updating will only occur after we've started to
         * edit another cell.
         */

        togglebutton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                stopCellEditing();
            }
        });


        if (value.toString().equals("true")) {
            togglebutton.setSelected(true);
        }
 else {
            togglebutton.setSelected(false);
 }
        togglebutton.setBorderPainted(false);
        return togglebutton;
    }

    public Object getCellEditorValue () {
        return togglebutton.isSelected();
    }


}

我希望这会对某人有所帮助

Ok, so I've tried implement camickr's approach, but I've encountered some very nasty problem in the process. I've added a MouseMotionListener to JTable to track the current and previous cell and added some methods that instruct a renderer about which component to return and then repaint the appropriate cell. However, for some odd reason each cell is repainted twice even though there was only one repaint request. Basically, I was able to highlight a cell on a mouse rollover, but it was impossible to remove highlighting from a cell once the mouse cursor leaved it. After initial confusion I've decided to do this in a different way. I've added a method which invokes the editor whe the mouse is over the cell and then added some code that stops editing as soon as the state of JToggleButton (my rendering and editing component) is changed. My current code looks like this :

package guipkg;

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

public class Grid extends JTable implements MouseListener {

int currentCellColumn = -1;
int currentCellRow = -1;
int previousCellColumn = -1;
int previousCellRow = -1;

public void detectCellAtCursor (MouseEvent e) {
    Point hit = e.getPoint();
    int hitColumn = columnAtPoint(hit);
    int hitRow = rowAtPoint(hit);
    if (currentCellRow != hitRow || currentCellColumn != hitColumn) {
        this.editCellAt(hitRow, hitColumn);
        currentCellRow = hitRow;
        currentCellColumn = hitColumn;
    }

}


}







package guipkg;

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


public class TCEditor extends AbstractCellEditor implements TableCellEditor  {

    /**
     * A toggle button which will serve as a cell editing component
     */

    JToggleButton togglebutton = new JToggleButton();





    public Component getTableCellEditorComponent (JTable Table, Object value, boolean isSelected, int rindex, int cindex) {

        /**
         * We're adding an action listener here to stop editing as soon as the state of JToggleButton is switched.
         * This way data model is updated immediately. Otherwise updating will only occur after we've started to
         * edit another cell.
         */

        togglebutton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                stopCellEditing();
            }
        });


        if (value.toString().equals("true")) {
            togglebutton.setSelected(true);
        }
 else {
            togglebutton.setSelected(false);
 }
        togglebutton.setBorderPainted(false);
        return togglebutton;
    }

    public Object getCellEditorValue () {
        return togglebutton.isSelected();
    }


}

I hope that will help someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文