codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Buttonlistener for both textfield and a button  gosi at 19:19 on Monday, October 31, 2005
 

In this program I have to write a number in a text field and then choose either if I want to press an button or press enter to get the results. No matter what I try I can't connect the button "push" to the text field. I both post my scripth panel and the program and hopefully someone can help me :)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FahrenheitModifyPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
private JButton push;

public FahrenheitModifyPanel()
{
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);


fahrenheit.addActionListener (new TempListener());

push = new JButton ("push");
add(push);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);

setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}


private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{

int fahrenheitTemp, celsiusTemp;

String text = fahrenheit.getText();

fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;

resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}


------------------------------------

import javax.swing.JFrame;

public class FahrenheitModify {




// -----------------------------------------------------------------
// Creates and displays the temperature converter GUI.
// -----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

FahrenheitModifyPanel panel = new FahrenheitModifyPanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}


  Re: Buttonlistener for both textfield and a button  crwood at 00:11 on Tuesday, November 01, 2005
 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FTest extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
private JButton push;

public FTest()
{
// initialize components
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
Dimension d = resultLabel.getPreferredSize();
d.width = 35;
resultLabel.setPreferredSize(d);
fahrenheit = new JTextField (5);
push = new JButton ("push");

// add listeners
TempListener l = new TempListener();
fahrenheit.addActionListener (l);
push.addActionListener(l);

// layout components
add(push);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);

setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}

private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text = fahrenheit.getText();
int fahrenheitTemp = Integer.parseInt (text);
int celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}

public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FTest panel = new FTest();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005