Solved

Java ActionListener Problems

Posted on 2003-11-23
12
620 Views
Last Modified: 2008-02-01
I have made a program which has two buttons one is for launching the analog clock and the other os for launching the digital clock.

For the buttons I dont use text but I use images, the problem is that when I i add the actionlisteners I will only identify the button by the text, so I fixed it by adding a alphabet so that it would be identified:

class helpPanel extends JPanel implements ActionListener
{
      //Constructor For Help Panel
      public helpPanel()
      {
            //Creating Buttons To Display Image
            JButton guideButton = new JButton("A", new ImageIcon("images/butt-guide.jpg"));
            JButton creditsButton = new JButton("B", new ImageIcon("images/butt-credits.jpg"));
            JButton contactButton = new JButton("C", new ImageIcon("images/butt-contact.jpg"));
            
            //Adding Components
            add(guideButton);
            add(creditsButton);
            add(contactButton);
            
            //Adding ActionListeners To Components
            guideButton.addActionListener(this);
            creditsButton.addActionListener(this);
            contactButton.addActionListener(this);
      }
      
      public void actionPerformed(ActionEvent event)
      {
            String finalEvent = event.getActionCommand();
            
            if(finalEvent.equals("A"))
            {
                  new Credits();
                  Toolkit.getDefaultToolkit().beep();
            }

            if(finalEvent.equals("B"))
            {
                  new Credits();
                  Toolkit.getDefaultToolkit().beep();
            }
            
            if(finalEvent.equals("C"))
            {
                  new Contact();
                  Toolkit.getDefaultToolkit().beep();
            }
      }
}

The thing is that the buttons now look weired because of that one alphabet there, can someone please tell me how to make it identify without showing that character.

As you can see I have a button which is called Guide can also please tell me how to make the actionlistener for that button, open either a .hlp file or a .html file.

One more thing is that when I open all the JFrames, by that I mean the Main Frame, the Credits Frame, and the Contact frame, if I were to close one of the frames all the frames close, can you also please help me fix that


Thanks a Zillion, I really appreciate your help
Isaac


PS: The whole program can be found at http://isp7.net/isaac/java/eco.zip
0
Comment
Question by:isaacmc
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9805651
If you change

>>String finalEvent = event.getActionCommand();

to

Object finalEvent = event.getSource();

and then do

if(finalEvent.equals(creditsButton))

and similar, then you don't need the alphabet

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9805672
That was part one. For part 3:

Set the JFrames as possible to stop them closing the whole application:

FrameA.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9805681
For part 2:

To show your help window, add a new JEditorPane to the frame:

JEditorPane helpPane = new JEditorPane("help.html");
getContentPane().add(helpPane, BorderLayout.CENTER);
0
 
LVL 92

Expert Comment

by:objects
ID: 9806914
Just use seperate action listeners

guideButton.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { guide(); }});
          creditsButton.addActionListener(this);
          contactButton.addActionListener(this);
...

private void guide()
{
     new Credits();
     Toolkit.getDefaultToolkit().beep();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9806924
>          creditsButton.addActionListener(this);
>          contactButton.addActionListener(this);

Woops, you'd do similiar to the other two listeners. ie. add a dedicated listener that performs appropriate action.

Let me know if you have any q's.
0
 
LVL 92

Expert Comment

by:objects
ID: 9806942
> if I were to close one of the frames all the frames close, can you also please help me fix that

First for each frame set the close action to simply hide the frame.
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
You can then reuse this frame, if it is opened again.

Then add a WindowListener to each frame that checks how many windows are open and exits the app when the last window is closed.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:isaacmc
ID: 9807688
thanks alot you guys.

CEHJ your response to my first question game me the following error:

C:\1\ECO.java:131: cannot resolve symbol
symbol  : variable guideButton
location: class helpPanel
            if(finalEvent.equals(guideButton))
                                                                     ^
C:\1\ECO.java:137: cannot resolve symbol
symbol  : variable creditsButton
location: class helpPanel
            if(finalEvent.equals(creditsButton))
                                                                     ^
C:\1\ECO.java:143: cannot resolve symbol
symbol  : variable contactButton
location: class helpPanel
            if(finalEvent.equals(contactButton))
                                                                      ^
3 errors


This is How I Modified The Code:

      public helpPanel()
      {
            //Creating Buttons To Display Image
            JButton guideButton = new JButton(new ImageIcon("images/butt-guide.jpg"));
            JButton creditsButton = new JButton(new ImageIcon("images/butt-credits.jpg"));
            JButton contactButton = new JButton(new ImageIcon("images/butt-contact.jpg"));
            
            //Adding Components
            add(guideButton);
            add(creditsButton);
            add(contactButton);
            
            //Adding ActionListeners To Components
            guideButton.addActionListener(this);
            creditsButton.addActionListener(this);
            contactButton.addActionListener(this);
      }
      
      public void actionPerformed(ActionEvent event)
      {
            Object finalEvent = event.getSource();
            
            if(finalEvent.equals(guideButton))
            {
                  new Credits();
                  Toolkit.getDefaultToolkit().beep();
            }

            if(finalEvent.equals(creditsButton))
            {
                  new Credits();
                  Toolkit.getDefaultToolkit().beep();
            }
            
            if(finalEvent.equals(contactButton))
            {
                  new Contact();
                  Toolkit.getDefaultToolkit().beep();
            }
      }
}

Please tell me how I can fix this
0
 
LVL 92

Expert Comment

by:objects
ID: 9807715
  guideButton.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e) { guide(); }});
   creditsButton.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e) { credits(); }});
   contactButton.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e) { contact(); }});
}

private void guide()
{
    new Credits();
    Toolkit.getDefaultToolkit().beep();
}

private void credits()
{
    new Credits();
    Toolkit.getDefaultToolkit().beep();
}

private void contact()
{
    new Contact();
    Toolkit.getDefaultToolkit().beep();
}
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 9807723
so you class would end up looking like:

     public helpPanel()
     {
          //Creating Buttons To Display Image
          JButton guideButton = new JButton(new ImageIcon("images/butt-guide.jpg"));
          JButton creditsButton = new JButton(new ImageIcon("images/butt-credits.jpg"));
          JButton contactButton = new JButton(new ImageIcon("images/butt-contact.jpg"));
         
          //Adding Components
          add(guideButton);
          add(creditsButton);
          add(contactButton);
         
          //Adding ActionListeners To Components
     guideButton.addActionListener(new ActionListener()
         { public void actionPerformed(ActionEvent e) { guide(); }});
      creditsButton.addActionListener(new ActionListener()
         { public void actionPerformed(ActionEvent e) { credits(); }});
      contactButton.addActionListener(new ActionListener()
         { public void actionPerformed(ActionEvent e) { contact(); }});    
}    

private void guide()
{
    new Credits();
    Toolkit.getDefaultToolkit().beep();
}

private void credits()
{
    new Credits();
    Toolkit.getDefaultToolkit().beep();
}

private void contact()
{
    new Contact();
    Toolkit.getDefaultToolkit().beep();
}

}
0
 

Author Comment

by:isaacmc
ID: 9807741
You guys are great, how I wish I could accept more than one comment

special thanks to CEHJ and Objects
0
 
LVL 92

Expert Comment

by:objects
ID: 9807751
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809923
>>You guys are great, how I wish I could accept more than one comment

You could have done - a points split is perfectly possible
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now