简体   繁体   中英

Find components in Java's GUI hierarchy

With this code I am able to find what tab is selected but I need to do stuff with what is inside the tab. How do I work with the hierarchy?

EditPane.addChangeListener(new ChangeListener() {
// This method is called whenever the selected tab changes
public void stateChanged(ChangeEvent evt) {
    JTabbedPane pane = (JTabbedPane)evt.getSource();

    // Gets current tab
    int sel = pane.getSelectedIndex();
}
});

The component that is inside the tab is a JScrollPane .

You don't need the index of the pane, you need the component selected underneath. use getSelectedComponent() - eg

JTabbedPane pane = (JTabbedPane)evt.getSource();
JComponent myComponent = pane.getSelectedComponent();

To clarify your original goal, you want to manipulate the client object living in the JScrollPane. You're missing some objects. in your JScrollPane you need to invoke getViewport().getViewportView() from the ScrollPane. (Source: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html )

@ Dasdasd

I already checked it out but it only returns ViewPorts and ScrollBars

yes that correct, (probalby there you put JPanel) then you have to repeats your steps again, until as you will not find JPanel into ViewPort , that's possible get JComponents another way(s), but this is very good lesson for Hierarchy of JComponents

Component[] components = xxx.getComponents();
  for (int i = 0, l = components.length; i < l; i++) {
     if (components[i] instanceof JScrollPane) {
         JScrollPane scr = (JScrollPane) components[i];
            Component[] components1 = scr.getComponents();n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM