How to call button click event in another page's load event?

Having problem with program?

  • I'm trying to create a program that has a text field where i insert the degrees in Fahrenheit. I press enter and it converts it into Celsius. The text works but i also need a button so i can click the button and it will convert what is in the text field as well. I created the button but it does not do anything and it is not on the same panel. Here is the program import javax.swing.JFrame; import java.awt.Button; public class Fahrenheit { //--------------------------------------… // Creates and displays the temperature converter GUI. //--------------------------------------… public static void main (String[] args) { JFrame frame = new JFrame ("Fahrenheit"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); FahrenheitPanel panel = new FahrenheitPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); JFrame frame1 = new JFrame ("Push Counter"); frame1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame1.getContentPane().add(new Button()); frame1.pack(); frame1.setVisible(true); } } Here is the class i created to call import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FahrenheitPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel,label; private JTextField fahrenheit; private JButton push; //--------------------------------------… // Constructor: Sets up the main GUI components. //--------------------------------------… public FahrenheitPanel() { inputLabel = new JLabel ("Enter Fahrenheit temperature:"); outputLabel = new JLabel ("Temperature in Celsius: "); resultLabel = new JLabel ("---"); fahrenheit = new JTextField (5); fahrenheit.addActionListener (new TempListener()); add (inputLabel); add (fahrenheit); add (outputLabel); add (resultLabel); setPreferredSize (new Dimension(300, 75)); setBackground (Color.yellow); } public void Button() { push = new JButton ("Click Me"); push.addActionListener (new ButtonListener()); label = new JLabel ("Celsius:" + resultLabel); add (push); add (label); } //**************************************… // Represents an action listener for the temperature input field. //**************************************… private class TempListener implements ActionListener { //--------------------------------------… // Performs the conversion when the enter key is pressed in // the text field. //--------------------------------------… 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)); } } private class ButtonListener implements ActionListener { //--------------------------------------… // Performs the conversion when the enter key is pressed in // the text field. //--------------------------------------… 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)); } } }

  • Answer:

    Now button is also working check the lines with //////////////////////// import javax.swing.JFrame; import java.awt.Button; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Fahrenheit { //------------------------------------… // Creates and displays the temperature converter GUI. //------------------------------------… public static void main (String[] args) { JFrame frame = new JFrame ("Fahrenheit"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); FahrenheitPanel panel = new FahrenheitPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); JFrame frame1 = new JFrame ("Push Counter"); frame1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame1.getContentPane().add(new Button()); frame1.pack(); frame1.setVisible(true); } } class FahrenheitPanel extends JPanel { private JLabel inputLabel, outputLabel, resultLabel,label; private JTextField fahrenheit; private JButton push; //------------------------------------… // Constructor: Sets up the main GUI components. //------------------------------------… public FahrenheitPanel() { inputLabel = new JLabel ("Enter Fahrenheit temperature:"); outputLabel = new JLabel ("Temperature in Celsius: "); resultLabel = new JLabel ("---"); fahrenheit = new JTextField (5); fahrenheit.addActionListener (new TempListener()); add (inputLabel); add (fahrenheit); add (outputLabel); add (resultLabel); setPreferredSize (new Dimension(300, 75)); setBackground (Color.yellow); ////////////////} ////////////////public void Button() ////////////////{ push = new JButton ("Click Me"); push.addActionListener (new ButtonListener()); label = new JLabel ("Celsius:" + resultLabel); add (push); add (label); }

Lebron at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.