User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,688 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,708 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1640 | Replies: 3
Reply
Join Date: Jul 2005
Posts: 5
Reputation: MFal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MFal MFal is offline Offline
Newbie Poster

Recognizing when a button is pressed.

  #1  
Aug 1st, 2005
I really appreciate all the help i've been getting on my project lately from you guys, and hopefully this will be the last problem.

I've written an applet, and it uses ActionListener to detect when the button is pressed. The program is supposed to go through my methods when the button is pressed. However when i launch the applet and put values in the boxes, nothing happens when i click. Using an embedded class is too hard for me (i've been doing java for 5 weeks, and the only prior experience i had was html). Also i can't seem to make two seperate files, because only one can implement ActionListener.

here's what i have so far.

making the button -
enter = new JButton("Click here to calculate numeric palindrome.");
    enter.addActionListener(this);

running my methods when i click the button -
 public void actionPerformed(ActionEvent e)
  {
    Numtemp = displayN.getText();

    Base = displayB.getText();

	convert();
	FindPalindrome();
	reconvert();
	print();
  }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Solution Re: Recognizing when a button is pressed.

  #2  
Aug 1st, 2005
Hi everyone,

I have no idea the number of JButtons in your application but where's the source??

Try this

public void actionPerformed(ActionEvent e)
  {

JButton B = (JButton)e.getSource();

if(B == enter)
    Numtemp = displayN.getText();

    Base = displayB.getText();

	convert();
	FindPalindrome();
	reconvert();
	print();
}

  }

I hope this helps you

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote  
Join Date: Jul 2005
Posts: 5
Reputation: MFal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MFal MFal is offline Offline
Newbie Poster

Re: Recognizing when a button is pressed.

  #3  
Aug 1st, 2005
Thanks for responding. I took that code and adapted it to my code, and it compiles, but i still have the same problem. Here is all of the relevant code. Oh and there is only one button.
public void init()
  {
    displayN = new JTextField(35);
    displayN.setFont(new Font("Monospaced", Font.BOLD, 14));
    String message1 = "Enter Number Here";
    displayN.setText(message1);

    displayB = new JTextField(15);
    displayB.setFont(new Font("Monospaced", Font.BOLD, 14));
    String message2 = "Enter Base Here";
    displayB.setText(message2);

    enter = new JButton("Click here to calculate numeric palindrome.");
    enter.addActionListener(this);

    displayS = new JTextField(60);
    displayS.setFont(new Font("Monospaced", Font.BOLD, 14));

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.setBackground(Color.darkGray);
    c.add(displayN);
    c.add(displayB);
    c.add(enter);
    c.add(displayS);

  }

//This tells the program to run when the button is clicked. Help for this code came from
//freesoft_2000 at the www.daniweb.com/techtalkforums
  public void actionPerformed(ActionEvent e)
  {
    JButton B = (JButton)e.getSource();

    if(B == enter)
    {
    Numtemp = displayN.getText();

    Base = displayB.getText();

	convert();
	FindPalindrome();
	reconvert();
	print();
	}
  } 
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Recognizing when a button is pressed.

  #4  
Aug 1st, 2005
Debugging 101: Put some system.out.println()'s throughout the actionPerformed() method, and all methods it calls. check to make sure all variables have the proper values, and that it even enters certain methods.

By the way, what are you doing with these lines:

Numtemp = displayN.getText();

Base = displayB.getText();


If those are used in the coorisponding methods called, I would suggest passing them as parameters instead.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC