Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable all the component?

Status
Not open for further replies.

wangdong

Programmer
Oct 28, 2004
202
0
0
CN
I have a JInternalFrame with a JTabbedPane on it. The JTabbedPane has six tabs and each tab has its own components like buttons, tables, textfields.

Now, I need to load this JInternalFrame in two modes. The first mode is editing mode, user can add record, delete record and apply change. The second mode is monitoring mode. user can only view it, all the buttons should be disabled, and all the textfields are uneditable.

Does anyone know how to disable all these components in a simple way? What I mean is not to change them one by one. I am trying to use something like:

Code:
Component cp[] = tbInternalFrame.getComponents();
for(int i=0;i<cp.length;i++){
  cp[i].setEnabled(false);
}
tbInternalFrame.setVisible(true);

but it doesn't do the job.
 
What i use to (de)activate components is the following:

public void setEnabled(Component component, boolean enable) {
component.setEnabled(enable);
// Check for instance of container.
if (component instanceof Container) {
// Continue setEnabled if there are more components.
Component components[] = ((Container) component).getComponents();
for (int i = 0; i < components.length; i++) {
if (components != null) {
setEnabled(components, enable); // <- Here is the recursive call.
}
}
}
}



 
How do know if the type of a component is a JButton or a JTextField? Because I only need to disable them.
 
Sorry. It doesn't solve my problem. JPanel is not a component. or Maybe I am wrong. but when I am trying to use JTabbedPane.getComponents(), it return 0 to me.

 
As far as I understand it, you'd need to call the getComponents on the panel which you associated with the particular tab, not the tab itself.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top