java - JTable:获取单元格内的对象位置

标签 java jtable tablecellrenderer

我不知道如何解决这个问题:

我有一个带有自定义单元格渲染器的 JTable。在单元格渲染器中,我放置了一个带有网格包布局的 JLabel。

如果我尝试获取 JLabel 在渲染器中的位置,我总是得到 0、0。我之前尝试重新验证 JLabel,但找不到正确的解决方案。

这是自定义渲染器的代码:

public CustomRenderer() {
    // set the renderer layout to null
    setLayout(new GridBagLayout());
    // create the layouts label
    layoutsLbl = new JLabel("Layout");
    // create a grid bag constraints instance
    GridBagConstraints gbc = new GridBagConstraints();
    // first column
    gbc.gridx = 0;
    // first row
    gbc.gridy = 0;
    // maximum horizontal weight
    gbc.weightx = 1;
    // anchor to the top left corner
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    // fill horizontally
    gbc.fill = GridBagConstraints.HORIZONTAL;
    // set the insets
    gbc.insets = new Insets(35, 0, 10, 0);
    // add the layouts label to the view
    add(layoutsLbl, gbc);
}

public String clickCheck() {
    // revalidate the layouts label
    layoutsLbl.revalidate();
    // get the layouts label location
    System.out.println("Label location: " + layoutsLbl.getLocation());
}

我知道在某些情况下我可能能够计算出 JLabel 的位置,因为它是固定的,但我想要一个通用的方法,因为我需要它用于渲染器,其中我有很多动态生成的 JLabel。

最终目标是找到每个 JLabel 的位置,以便能够识别哪个 JLabel 已通过附加到表的 MouseListener 单击。我能够找到单击的单元格、单元格中的鼠标位置,因此我需要单元格中的 JLabel 位置来根据鼠标位置对其进行测试。

最佳答案

The final goal is to find the location of each JLabel to be able to identify Blockquote which JLabel has been clicked with a MouseListener attached to the table.

这个问题很容易解决。只需使用这个简单的代码片段即可获取被点击的对象。

this.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {

            Component clicked = getComponentAt(e.getPoint());
            if(clicked instanceof JLabel){
                JLabel myLabel = (JLabel) clicked;
                //obviously myLabel is the clicked JLabel
                System.out.println("Clicked Label: " + myLabel.getLocation());
            }

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    });

使用 getLocation() 方法现在将为您提供 JLabel 的真实位置。

UPDATE: Here is the requested complete Example (Using JTable instead)

这是我的主要内容:

public static void main(String[] args) {
    JFrame frame =  new JFrame();
    frame.setContentPane(new CostumTable());
    frame.getContentPane().addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {

            Component clicked = frame.getContentPane().getComponentAt(e.getPoint());
            if(clicked instanceof JLabel){
                JLabel myLabel = (JLabel) clicked;
                //obviously myLabel is the clicked JLabel
                System.out.println("Text of clicked Label: " + myLabel.getText());
            }

        }
        @Override
        public void mousePressed(MouseEvent e) {

        }
        @Override
        public void mouseReleased(MouseEvent e) {

        }
        @Override
        public void mouseEntered(MouseEvent e) {

        }
        @Override
        public void mouseExited(MouseEvent e) {

        }

    });

    frame.setSize(200, 500);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

这将是 CustomRenderer(现在是 CostumTable):

    public CostumTable() {
    // set the renderer layout to GridBagLayout
    setLayout(new GridBagLayout());
    // create the layouts label
    layoutsLbl = new JLabel("Layout");
    // create a grid bag constraints instance
    GridBagConstraints gbc = new GridBagConstraints();
    // first column
    gbc.gridx = 0;
    // first row
    gbc.gridy = 0;
    // maximum horizontal weight
    gbc.weightx = 1;
    // anchor to the top left corner
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    // fill horizontally
    gbc.fill = GridBagConstraints.HORIZONTAL;
    // set the insets
    gbc.insets = new Insets(35, 0, 10, 0);
    // add the layouts label to the view
    add(layoutsLbl, gbc);


    //Just here to test the MousListener
    List<JLabel> testLabel = new ArrayList<>();
    for(int i = 0; i < 3; i++) {
        testLabel.add(new JLabel("TestLabel number " + i));
        gbc.gridx = 0;
        gbc.gridy = i + 1;
        add(testLabel.get(i), gbc);
    }
    //
}

public String clickCheck() {
    // revalidate the layouts label
    layoutsLbl.revalidate();
    // get the layouts label location
    System.out.println("Label location: " + layoutsLbl.getLocation());
    return null; //I dont know what you want to return
}

关于java - JTable:获取单元格内的对象位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297617/

相关文章:

java - Spring 带注释的 Controller bean 在应用程序上下文中重复

java - 在java中创建一个自定义表

java - JPanel 作为 TableCellEditor 消失

java - 如何访问 BigQuery 结果集的 cacheHit 属性?

Java 日历不一致的结果

java - Spark2.2.1 不兼容 Jackson 版本 2.8.8

java - TableModelListener 并不总是工作

java - JCheckBox 选中时返回 false

java - 减小 JTable 单元格中自定义渲染组件的大小

java - 默认表模型不显示新行