Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Disabling an inherited JButton

Thread view: 
Gyruss - 06 Jan 2005 09:55 GMT
Dear all,

I have 2 child classes (ChildPanel1 & ChildPanel2) that extend ParentPanel.
ParentPanel has a JButton component coupled with an accessor method
toggleButton() to enable/disable the button.

I currently enable/disable the button by calling toggleButton() twice, once
on the ChildPanel1 instance and then again on the ChildPanel2 instance. I'd
like to be able to organise things so that I could enable/disable the button
on all the instances of all the classes that extend ParentPanel with one
call.

How can I do this?

Cheers!

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

public class ButtonTest {
   public class ParentPanel extends JPanel {
       protected JPanel buttonPanel;

       protected boolean enabled;

       protected JButton button1;

       public void toggleButton() {
           button1.setEnabled(enabled);
           if (enabled)
               enabled = false;
           else
               enabled = true;
       }

       public ParentPanel(String title) {
           super(new BorderLayout());
           enabled = false;
           button1 = new JButton("Button 1");
           buttonPanel = new JPanel();
           buttonPanel.add(button1);
           setLayout(new BorderLayout());
           add(buttonPanel, BorderLayout.SOUTH);
       }
   }

   public class ChildPanel1 extends ParentPanel {
       public ChildPanel1() {
           super("Panel 1");
           JTextArea ta = new JTextArea();
           JScrollPane scroll = new JScrollPane();
           scroll.getViewport().add(ta);
           add(scroll, BorderLayout.CENTER);
       }
   }

   public class ChildPanel2 extends ParentPanel {
       protected JList myList;

       public ChildPanel2() {
           super("Panel 2");
           myList = new JList(new String[] { "item 1", "item 2" });
           JScrollPane ps = new JScrollPane();
           ps.getViewport().add(myList);
           JButton button2 = new JButton("Button 2");
           buttonPanel.add(button2);
           add(ps, BorderLayout.CENTER);
       }
   }

   public static void main(String argv[]) {
       JFrame frame = new JFrame("Button Test");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(370, 330);
       JPanel mainPanel = new JPanel(new BorderLayout());
       ButtonTest myTest = new ButtonTest();
       final ChildPanel1 panel1 = myTest.new ChildPanel1();
       final ChildPanel2 panel2 = myTest.new ChildPanel2();
       JTabbedPane jtp = new JTabbedPane();
       jtp.addTab("Tab 1", panel1);
       jtp.addTab("Tab 2", panel2);
       JButton disableButton = new JButton(new AbstractAction(
               "Toggle button 1") {
           public void actionPerformed(ActionEvent e) {
               panel1.toggleButton();
               panel2.toggleButton();
           }
       });
       mainPanel.add(disableButton, BorderLayout.SOUTH);
       mainPanel.add(jtp, BorderLayout.CENTER);
       frame.setContentPane(mainPanel);
       frame.pack();
       frame.show();
   }
}
Andrey Kuznetsov - 07 Jan 2005 20:57 GMT
> I have 2 child classes (ChildPanel1 & ChildPanel2) that extend
> ParentPanel.
> ParentPanel has a JButton component coupled with an accessor method
> toggleButton() to enable/disable the button.

see http://jgui.imagero.com/examples/SelectableAction/

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.