Solved

Java ActionListener Problems

Posted on 2003-11-23
12
625 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 36
Configure a Bean in an XML file 4 48
java example issue 5 42
java imports not found 4 47
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

735 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