Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

Java ActionListener Problems

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
isaacmc
Asked:
isaacmc
  • 6
  • 4
  • 2
1 Solution
 
CEHJCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
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!

 
objectsCommented:
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
 
objectsCommented:
>          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
 
objectsCommented:
> 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
 
isaacmcAuthor Commented:
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
 
objectsCommented:
  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
 
objectsCommented:
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
 
isaacmcAuthor Commented:
You guys are great, how I wish I could accept more than one comment

special thanks to CEHJ and Objects
0
 
objectsCommented:
0
 
CEHJCommented:
>>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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now