JAVA How to get Tab from JTabbedPane by mousePosition

2.4k views Asked by At

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!". enter image description here

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.

2

There are 2 answers

5
camickr On BEST ANSWER
Component c = tab.getComponentAt(new Point(e.getX(), e.getY()));

You don't want to get the component where you click. You want to get the component from the selected tab.

Code should be:

//int index = tab.getSelectedTab(); // oops, this was a typo
int index = tab.getSelectedIndex();
Component c = tab.getComponentAt( index );
0
aterai On

how to get the component just from the mouse-clicking on the title.

I guess you are looking for JTabbedPane#indexAtLocation(int, int)?

Returns the tab index corresponding to the tab whose bounds intersect the specified location. Returns -1 if no tab intersects the location.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SSCE2 extends JFrame {

  private JTabbedPane tab;
  public SSCE2() {
    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! :-)
          int index = tab.indexAtLocation(e.getX(), e.getY());
          if (index >= 0) {
            Component c = tab.getComponentAt(index);
            if (c instanceof JLabel) {
              JLabel innerComponent = (JLabel) c;
              System.out.println("Found:" + innerComponent.getText());
            }
          }
        }
      }
    });
  }

  public static void main(final String[] args) throws Exception {
    JFrame start = new SSCE2();
    start.setDefaultCloseOperation(EXIT_ON_CLOSE);
    start.setVisible(true);
    start.setSize(new Dimension(450, 300));

  }
}