Note that there are some explanatory texts on larger screens.

plurals
  1. POMouseListener on button rendered on a JTree Row in Java
    text
    copied!<p>I'm blocked with a probelm about Java and the use of JTree:</p> <p>I want to create a JTree with, node by node, some JButtons components (or Images, I don't mind), like in the following picture. It will be 3 or 4 buttons in the same row. I succeed to do that. </p> <p><img src="https://i.stack.imgur.com/J9wcT.jpg" alt="enter image description here"></p> <p>But where I'm blocked is when I want to add a mouselistener on each of this button to manage their tooltip or an action on them. In fact the JTree component is most of the time used to manage the action on the full node, but not on its inside components.</p> <p>I did a short code, in comparaison at the real big code I have to work in, to quickly test what I say:</p> <pre><code>import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.tree.*; import java.awt.event.*; import java.awt.*; import java.io.IOException; import java.net.URL; public class TreeWithPopup extends JPanel { DefaultMutableTreeNode root, node1, node2, node3; public TreeWithPopup() { MyJTree tree; root = new DefaultMutableTreeNode("root", true); node1 = new DefaultMutableTreeNode("node 1", true); node2 = new DefaultMutableTreeNode("node 2", true); node3 = new DefaultMutableTreeNode("node 3", true); root.add(node1); node1.add(node2); root.add(node3); setLayout(new BorderLayout()); tree = new MyJTree(root); tree.setCellRenderer(new PCellRenderer()); add(new JScrollPane((JTree) tree), "Center"); } public Dimension getPreferredSize() { return new Dimension(300, 300); } public static void main(String s[]) { JFrame frame = new JFrame("Tree with button"); TreeWithPopup panel = new TreeWithPopup(); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.setForeground(Color.black); frame.setBackground(Color.lightGray); frame.getContentPane().add(panel, "Center"); frame.setSize(panel.getPreferredSize()); frame.setVisible(true); frame.addWindowListener(new WindowCloser()); } } class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent e) { Window win = e.getWindow(); win.setVisible(false); System.exit(0); } } class MyJTree extends JTree implements ActionListener { MyJTree(DefaultMutableTreeNode dmtn) { super(dmtn); addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent arg0) { System.out.println("JTree.MouseListener"); } public void mouseEntered(MouseEvent arg0) { System.out.println("JTree.MouseListener"); } public void mouseExited(MouseEvent arg0) { System.out.println("JTree.MouseListener"); } public void mousePressed(MouseEvent arg0) { System.out.println("JTree.MouseListener"); } public void mouseReleased(MouseEvent arg0) { System.out.println("JTree.MouseListener"); } }); } public void actionPerformed(ActionEvent ae) { System.out.println("MyJTree.ActionPerformed"); } } class PCellRenderer extends DefaultTreeCellRenderer { public Component getTreeCellRendererComponent(JTree ptree, Object pvalue, boolean psel, boolean pexpanded, boolean pleaf, int prow, boolean phasFocus) { Box myPanel = new Box(BoxLayout.X_AXIS); JButton myButton = new JButton("test"); Image imgToUse = null; Image imgRollOver = null; try { URL urlIcon = new URL("file:///C:/1.jpg"); // &lt;===== change their the path to icons imgToUse = ImageIO.read(urlIcon); urlIcon = new URL("file:///C:/2.jpg"); // &lt;===== change their the path to icons imgRollOver = ImageIO.read(urlIcon); } catch (IOException e) { e.printStackTrace(); } myButton.setRolloverIcon(new ImageIcon(imgRollOver)); myButton.setIcon(new ImageIcon(imgToUse)); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.out.println(" detected on "); } }); myButton.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent arg0) { System.out.println("myButton.MouseListener"); } public void mouseEntered(MouseEvent arg0) { System.out.println("myButton.MouseListener"); } public void mouseExited(MouseEvent arg0) { System.out.println("myButton.MouseListener"); } public void mousePressed(MouseEvent arg0) { System.out.println("myButton.MouseListener"); } public void mouseReleased(MouseEvent arg0) { System.out.println("myButton.MouseListener"); } }); myPanel.add(myButton); return myPanel; } } </code></pre> <p>You just have to change the icon path or put the two following icons at "c:/"</p> <p><img src="https://i.stack.imgur.com/3z91L.jpg" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/EzzDS.jpg" alt="enter image description here"></p> <p>I also searched to use the x/y position of the row event but I was unable to find the position of my button after rendering. </p> <p>If anyone can have an idea how to do that, he could be very helpful.</p> <p>Thanks, at least for read this question ;-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload