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

时间:2018-01-17 09:26:46

标签: 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。< / p>

最终目标是找到每个JLabel的位置,以便能够识别使用附加到表的MouseListener单击了哪个JLabel。我能够找到单击的单元格,单元格中的鼠标位置,因此我需要单元格中的JLabel位置来测试鼠标位置。

1 个答案:

答案 0 :(得分:1)

  

最终目标是找到每个JLabel能够识别的位置   Blockquote用附加到表格的MouseListener单击了哪个JLabel。

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

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所拥有的真实位置。

  

更新:这是请求的完整示例(改为使用JTable

这是我的主要:

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
}