Just came to this problem. Brings SSCE with me! :-)
I would like to get the tab content (component) by double-clicking at its "Title".
I give you this SSCE and in this scenario, I would like to receive the JLabel by double-clicking at the tab-menu-title. (Green Tab) and I would like to receive the JLabel "I am label 2!".
there is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCE extends JFrame {
private JTabbedPane tab;
public SSCE() {
tab = new JTabbedPane();
this.add(tab);
JLabel p = new JLabel("I am label 1!");
tab.addTab("Red tab",p );
JLabel p2 = new JLabel("I am label 2!");
tab.addTab("Green tab",p2 );
JLabel p3 = new JLabel("I am label 3!");
tab.addTab("Blue tab",p3 );
JLabel p4 = new JLabel("I am label 4!");
tab.addTab("Cyan tab", p4 );
tab.addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
if ( e.getClickCount() > 1) {
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));
//TODO Find the right label and print it! :-)
JLabel innerComponent = (JLabel) c;
System.out.println("Found:" + innerComponent.getText());
}
}
});
}
public static void main(final String[] args) throws Exception {
SSCE start = new SSCE();
start.setDefaultCloseOperation(EXIT_ON_CLOSE);
start.setVisible(true);
start.setSize(new Dimension(450,300));
}
}
Is it possible to do it somehow? I was trying a lot of stuff. But no luck :-( Thank you so much for your help!
What I am trying to do, is to implement functionality for JTabbedPane.. So when you double-click at the "Title" it will open a dialog with the content of the Tab you double-clicked at.
I know how to create dialog and so on.. But I dont know how to get the component just from the mouse-clicking on the title.
You don't want to get the component where you click. You want to get the component from the selected tab.
Code should be: