I don't find the method that say what jpanel is showed at the moment, what I see on the screen at the moment. I try isShowing() , hasFocus() , isEnabled() but never that works. Thanks at all.
Asked
Active
Viewed 234 times
0
-
1`getSelectedIndex() ` , `getSelectedComponent()` related http://stackoverflow.com/questions/20145916/get-jtable-from-selected-tab-at-jtabbedpane and http://stackoverflow.com/questions/2937684/how-to-check-whether-the-tab-is-active-or-not-in-jtabbedpane – Madhawa Priyashantha Jun 15 '15 at 15:55
-
doesn't return the jpanel and myPane.getSelectedComponent().getComponents(); .getComponents() doesn't exist. How can I do? – Local Hero Jun 15 '15 at 16:24
-
i explain your comment problem at the end of my answer – Madhawa Priyashantha Jun 15 '15 at 17:01
1 Answers
3
you can use getSelectedComponent()
or getSelectedIndex()
to get currently active panel on jtabbed pane. consider following example this will set color of currently active panel to yello when click the button
public class JTabbedPaneDemo extends JFrame {
public JTabbedPaneDemo() {
JButton button = new JButton("color");
JPanel mainpanel = new JPanel();
JTabbedPane jtbExample = new JTabbedPane();
JPanel jplInnerPanel1 = new JPanel();
jtbExample.addTab("t1", jplInnerPanel1);
jtbExample.setSelectedIndex(0);
JPanel jplInnerPanel2 = new JPanel();
jtbExample.addTab("t2", jplInnerPanel2);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtbExample.getSelectedComponent().setBackground(Color.yellow);
}
});
setLayout(new GridLayout(1, 1));
mainpanel.setLayout(new BorderLayout());
mainpanel.add(jtbExample, BorderLayout.CENTER);
mainpanel.add(button,BorderLayout.NORTH);
this.setContentPane(mainpanel);
this.setVisible(true);
}
public static void main(String[] args) {
new JTabbedPaneDemo();
}
}
and for your comment
myPane.getSelectedComponent()
will return active component .and getSelectedComponent()
return component
object .so there is no method getComponents()
in component
class.if you are going to get all components in jpnel then you have to cast returned component to a jpanel
before call getComponents()
example
Component[] c=((JPanel)jtbExample.getSelectedComponent()).getComponents();
for your comment 2
if you want to verify that active one is jpanel1 then cast to jpanel
and check like follow.make sure you have declared jPanel12 as a field
variable.
if((JPanel)jTabbedPane2.getSelectedComponent()==jPanel12){
System.out.println("jPanel12 is active");
}

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60
-
Fantastic reply but I have this issue: I have this if condition: " if(jPanel12==jTabbedPane2.getSelectedComponent()) ". I must verify if I stay in the jPanel12 but this condition don't work. What's the trouble in my costruct? – Local Hero Jun 16 '15 at 06:54