This is my first question here. I searched this board to find an answer if it is possible to make legend items clickable. I am sorry to repeat this question in case it is already answered because I did not find an answer.
I already know that a ChartPanel can add a MouseListener object, but I have two problems:
1) How to mark the selected legend item. For instance, how to set a border?
2) The item's label is not withing the Rectangle of the block. It only works by clicking next to the label on left side.
Code: Select all
final ChartPanel legendPanel = new ChartPanel(legendChart);
legendPanel.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
List<Block> blockList =legendPanel.getChart().getLegend(0).getItemContainer().getBlocks();
int blockCount = blockList.size();
for(int i = 0; i < blockCount; i++){
Block block = blockList.get(i);
if(block.getBounds().contains(p)){
System.out.println("Mouse clicked on block " + i); // now it's time to highlight the clicked item and to select the correct settings tab
break;
}
}
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e) {}
});