Solved

Java ActionListener Problems

Posted on 2003-11-23
12
623 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

770 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