Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Seems simple enough, using setText to display GUI results
Pat Steele
Greenhorn
Posts: 22
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
HI,
I have the code here to populate a GUI.
I have the math and then print out and return a single number.
I have tried to use setText in the class CalculateButtonHandler.
Please see if you can solve the problem.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RectangleProgram extends JFrame { private JLabel lengthL, widthL, areaL, perimeterL, averageL; //* The private values are used only withing a certain class such as the size of window private JTextField lengthTF, widthTF, areaTF, perimeterTF, averageTF; private JTextField A,B,C,D,average; private JButton calculateB, exitB; private CalculateButtonHandler cbHandler; private ExitButtonHandler ebHandler; private static final int WIDTH = 600; private static final int HEIGHT = 350; public RectangleProgram() { //vertical labels lengthL = new JLabel("Test one:", SwingConstants.RIGHT); widthL = new JLabel ("Test two:",SwingConstants.RIGHT); areaL = new JLabel ("Test three:", SwingConstants.RIGHT); perimeterL = new JLabel ("Test four:", SwingConstants.RIGHT); averageL= new JLabel("Average:", SwingConstants.RIGHT); // lengthTF = new JTextField(10); widthTF = new JTextField(10); areaTF = new JTextField(10); perimeterTF = new JTextField(10); averageTF = new JTextField(10); A = new JTextField(10); B = new JTextField(10); C = new JTextField(10); D = new JTextField(10); calculateB = new JButton("Calculate"); cbHandler = new CalculateButtonHandler(); calculateB.addActionListener(ebHandler); exitB = new JButton ("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); setTitle ("Math Averages calc"); Container pane = getContentPane(); pane.setLayout(new GridLayout(6,3)); pane.add(lengthL); pane.add(lengthTF); pane.add(A); pane.add(widthL); pane.add(widthTF); pane.add(B); pane.add(areaL); pane.add(areaTF); pane.add(C); pane.add(perimeterL); pane.add(perimeterTF); pane.add(D); pane.add(averageL); pane.add(averageTF); pane.add(calculateB); pane.add(exitB); setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { double testone, testtwo, testthree, testfour, weightone, weighttwo, weightthree,weightfour; double sum1, sum2, sum3, sum4, average; testone = Double.parseDouble(lengthTF.getText()); weightone = Double.parseDouble(A.getText()); testtwo = Double.parseDouble(widthTF.getText()); weighttwo = Double.parseDouble(B.getText()); testthree = Double.parseDouble(areaTF.getText()); weightthree = Double.parseDouble(C.getText()); testfour = Double.parseDouble(perimeterTF.getText()); weightfour = Double.parseDouble(D.getText()); sum1 = testone * weightone; sum2 = testtwo * weighttwo; sum3 = testthree * weightthree; sum4 = testfour * weightfour; average = (sum1+sum2+sum3+sum4)/4; //Here is the piece that is said to display the "average" in the field averageTF. How would one remedy this? averageTF.setText(""+average); } } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { RectangleProgram rectObject = new RectangleProgram(); } }
Mohamed Sanaulla
Bartender
Posts: 3225
34
I like...
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In the below line:
calculateB.addActionListener(ebHandler);
you are setting the correct handler for the Calculate button. The calculation handler is
cbHandler
and not
ebHandler
.
A bit more clear names would help in avoiding such errors and oversights.
Mohamed Sanaulla |
My Blog
| Author of
Java 9 Cookbook
|
Java 11 Cookbook
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help with button handles
JLabel Question
Trying to create a program that provides average test scores
why doesn't this code work?
Null pointer exception in button event handler.
More...