Solved

Java ActionListener Problems

Posted on 2003-11-23
12
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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