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

JTable: get object location inside a cell

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

我有一个带有自定义单元格渲染器的 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 的位置,以便能够识别已使用附加到 table 的 MouseListener 单击了哪个 JLabel。我能够找到单击的单元格、单元格中的鼠标位置,因此我需要单元格中的 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
}