Following the example in CustomCanvas.java
I've managed to paint a JButton inside a custom Swing canvas for an mxCell with this code
public void drawVertex(mxCellState state, String label)
{
Object value = ((mxCell) state.getCell()).getValue();
Pattern p = (Pattern)value;
System.out.println(p.length);
MyPanel comp = new MyPanel();
rendererPane.paintComponent(g, comp, graphComponent,
(int) (state.getX() translate.getX()),
(int) (state.getY() translate.getY()),
(int) state.getWidth(), (int) state.getHeight(), true);
g.setColor(Color.GREEN);
for (int i = 0; i < p.length; i ) {
JButton b = new JButton("");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("click,click");
}
});
rendererPane.paintComponent(g, b, graphComponent,
(int) (state.getX() translate.getX() i*state.getWidth()/p.length),
(int) (state.getY() translate.getY()),
(int) state.getWidth()/p.length, (int) state.getHeight(), true);
}
}
the button gets painted but it isn't working and if I click it nothing happens
CodePudding user response:
If you want the component to be interactive, you have to add it to container (which itself is added to another container, eventually reaching a JFrame). What you have now just paints it into a cell.
Theoretically you could bend-over-backwards to intercept clicks to the cell and forward them to the JButton, but that's probably not a sustainable approach.