Results 1 to 7 of 7
Thread: GUI interaction
- 10-17-2010, 11:16 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
GUI interaction
I am a total Java newb looking for some guidance.
Background
I am making an App for a course I am taking. It has a gui that allows some input of payment and minutes spent for a tutoring session. Then it will also display a report of sessions, etc.
Anyway -- I made a class (GUI) and built the GUI.
And I made a class (EarningsBucket) with a 2d array to hold the minutes and payment amounts. I made a method to record a session.
Then I have a Main class with my Main method, that I use to create an earnings bucket object, and then create and display the GUI.
Question
So I was trying to update the actionlistenter for the "Enter Session" button, that will take the values from the input fields for minutes and payment, and run the method to enter the session into the EarningsBucket object.
But the actionlistener (as I have it now anyway based on an example GUI from my book) is an internal class in the GUI JPanel Class where I build the GUI, and I cant seem to access the earningsbucket object from in there. (Even after making the variable that holds it in the Main Class public)
Anyway -- any input would be helpful. I may be approaching the problem all wrong -- in which case, please point me in the right direction.
PLEASE DO NOT RESPOND WITH SPECIFIC CODE. Though examples/skeleton code to convey a point or approach is fine.
In case anyone wants to see -- here is what I have so far
Main
Earnings BucketJava Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutor; import javax.swing.JFrame; /** * * @author glyph */ public class Main { public EarningsBucket data; /** * @param args the command line arguments */ public static void main(String[] args) { EarningsBucket data = new EarningsBucket(); GUI frame = new GUI(data); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Tutoring Earnings"); frame.setVisible(true); } }
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutor; /** * * @author glyph */ public class EarningsBucket { private int sessions; private double[][] array; public EarningsBucket() { array = new double[2][100]; sessions = 0; } public void recordSession(double minutes, double payment) { array[0][sessions] = minutes; array[1][sessions] = payment; sessions++; } // TODO add more methods, for report, etc }
GUI
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutor; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author glyph */ public class GUI extends JFrame { private javax.swing.JButton enterButton; private javax.swing.JButton runReportButton; private javax.swing.JButton quitButton; private javax.swing.JLabel minutesLabel; private javax.swing.JLabel paymentLabel; private javax.swing.JTextField minutesTextField; private javax.swing.JTextField paymentTextField; private java.awt.TextArea textArea; private ActionListener enterListener; private ActionListener quitListener; private ActionListener runReportListener; private static final int FRAME_WIDTH = 500; private static final int FRAME_HEIGHT = 500; private static final int TEXT_AREA_FRAME_WIDTH = 500; private static final int TEXT_AREA_FRAME_HEIGHT = 400; public GUI(Object data) { class enterListener implements ActionListener { public void actionPerformed(ActionEvent event) { double minutes = Double.parseDouble(minutesTextField.getText()); double payment = Double.parseDouble(paymentTextField.getText()); //TODO call the recordSession method on the EarningsBucket Object } } class quitListener implements ActionListener { public void actionPerformed(ActionEvent event) { //TODO enter code } } class runReportListener implements ActionListener { public void actionPerformed(ActionEvent event) { //TODO enter code } } enterListener = new enterListener(); quitListener = new enterListener(); runReportListener = new enterListener(); createWindow(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } public void createWindow() { JPanel textInputPanel = createTextInputPanel(); JPanel textAreaPanel = createTextAreaPanel(); JPanel buttonsPanel = createButtonsPanel(); add(textInputPanel, BorderLayout.NORTH); add(textAreaPanel, BorderLayout.CENTER); add(buttonsPanel, BorderLayout.SOUTH); // JPanel panel = new JPanel(); // panel.add(textInputPanel,BorderLayout.NORTH); // panel.add(textAreaPanel,BorderLayout.CENTER); // panel.add(buttonsPanel,BorderLayout.SOUTH); } public JPanel createTextAreaPanel() { textArea = new TextArea(); // textArea.setSize(TEXT_AREA_FRAME_WIDTH, TEXT_AREA_FRAME_HEIGHT); JPanel panel = new JPanel(); panel.add(textArea); panel.setSize(TEXT_AREA_FRAME_WIDTH, TEXT_AREA_FRAME_HEIGHT); return panel; } public JPanel createButtonsPanel() { runReportButton = new JButton(); runReportButton.setText("Run Report"); runReportButton.addActionListener(runReportListener); quitButton = new JButton(); quitButton.setText("Quit"); quitButton.addActionListener(quitListener); JPanel panel = new JPanel(); panel.add(runReportButton); panel.add(quitButton); return panel; } public JPanel createTextInputPanel() { minutesLabel = new JLabel(); minutesLabel.setText("Time in Minutes"); paymentLabel = new JLabel(); paymentLabel.setText("Payment"); minutesTextField = new JTextField(); paymentTextField = new JTextField(); enterButton = new JButton(); enterButton.setText("Enter"); enterButton.addActionListener(enterListener); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); panel.add(minutesLabel); panel.add(minutesTextField); panel.add(paymentLabel); panel.add(paymentTextField); panel.add(enterButton); // createTextInputPanel.setBorder(new TitledBorder(new EtchedBorder(), "Enter Info")); return panel; } }
-
My guess is that with your program you would call the model's methods in the actionPerformed methods.
For instance, say we had the most basic of models that does nothing but add or subtract two numbers and returns the results:
Java Code:class MyModel { public int addNumbers(int a, int b) { return a + b; } public int subtractNumbers(int a, int b) { return a - b; } }
Your GUI could have a MyModel field that it would initialize by the object passed in its constructor:
Java Code:class MyGui extends JPanel { private MyModel model; //... public MyGui(MyModel mymodel) { this.model = mymodel;
And then assuming that this simple app had two JTextFields, field1 and field2 for entering data, and one JTextField, resultField for displaying the results, the actionPerformed methods could call the model's methods, like so:
Java Code:private void addActionPerformed() { int value1 = Integer.parseInt(field1.getText()); int value2 = Integer.parseInt(field2.getText()); // [color="red"]call the model's method here[/color] int result = model.addNumbers(value1, value2); resultField.setText(String.valueOf(result)); }
All put together...
Java Code:import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MyMain { private static void createAndShowUI() { MyModel model = new MyModel(); MyGui gui = new MyGui(model); JFrame frame = new JFrame("Simple GUI"); frame.getContentPane().add(gui); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class MyModel { public int addNumbers(int a, int b) { return a + b; } public int subtractNumbers(int a, int b) { return a - b; } } class MyGui extends JPanel { private static final long serialVersionUID = 1L; private MyModel model; private JTextField field1 = new JTextField(4); private JTextField field2 = new JTextField(4); private JTextField resultField = new JTextField(4); public MyGui(MyModel mymodel) { this.model = mymodel; JButton addBtn = new JButton("Add"); JButton subtractBtn = new JButton("Subtract"); addBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addActionPerformed(); } }); subtractBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { subtractActionPerformed(); } }); JPanel btnPanel = new JPanel(new GridLayout(0, 1)); btnPanel.add(addBtn); btnPanel.add(subtractBtn); add(field1); add(btnPanel); add(field2); add(new JLabel("=")); add(resultField); resultField.setEditable(false); } private void addActionPerformed() { int value1 = Integer.parseInt(field1.getText()); int value2 = Integer.parseInt(field2.getText()); int result = model.addNumbers(value1, value2); resultField.setText(String.valueOf(result)); } private void subtractActionPerformed() { int value1 = Integer.parseInt(field1.getText()); int value2 = Integer.parseInt(field2.getText()); int result = model.subtractNumbers(value1, value2); resultField.setText(String.valueOf(result)); } }
- 10-18-2010, 03:18 PM #3
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
Accessing data by-way-of actionListener
But the actionlistener (as I have it now anyway based on an example GUI from my book)
is an internal class in the GUI JPanel Class where I build the GUI, and I cant seem
to access the earningsbucket object from in there. (Even after making the variable
that holds it in the Main Class public)
This is all true.
I see you assign the object that contains your
values the name "data", then hand it off to the
GUI.
All of this is done properly in your code:
Java Code:public static void main(String[] args) { EarningsBucket data = new EarningsBucket(); GUI frame = new GUI(data); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Tutoring Earnings"); frame.setVisible(true); }
But I never see this reference ( "data" ) used in the
actionListener to access any values in the
EarningsBucket object.
It would be code that looks like this:
Java Code:int sum = sum + data.variableInEarningsBucket;
- 10-19-2010, 01:38 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Thanks. I got this working! I just declared an instance variable, and set it with the variable passed in the constructor call.
At this point though I am curious though. While what I have works, I am wondering if it is considered better form to do it differently. (or really, if there is anything I did, that would be considered a sloppy way to do it)
Earnings Bucket
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutor; import javax.swing.JOptionPane; /** * * @author bbeetle */ public class EarningsBucket { private int sessions; private double[][] array; public EarningsBucket() { array = new double[2][100]; sessions = 0; } public void recordSession(double minutes, double payment) { array[0][sessions] = minutes; array[1][sessions] = payment; // JOptionPane.showMessageDialog(null, array[0][sessions]); // JOptionPane.showMessageDialog(null, array[1][sessions]); sessions++; } public String listData() { String rawData = ""; for (int i = 0; i < sessions; i++) { rawData = rawData + array[0][i] + " "; rawData = rawData + array[1][i] + "\n"; } // JOptionPane.showMessageDialog(null, rawData); return rawData; } }
GUI
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tutor; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author bbeetle */ public class GUI extends JFrame { private javax.swing.JButton enterButton; private javax.swing.JButton runReportButton; private javax.swing.JButton quitButton; private javax.swing.JLabel minutesLabel; private javax.swing.JLabel paymentLabel; private javax.swing.JTextField minutesTextField; private javax.swing.JTextField paymentTextField; private java.awt.TextArea textArea; private ActionListener enterListener; private ActionListener quitListener; private ActionListener runReportListener; private static final int FRAME_WIDTH = 500; private static final int FRAME_HEIGHT = 500; private static final int TEXT_AREA_FRAME_WIDTH = 500; private static final int TEXT_AREA_FRAME_HEIGHT = 400; private EarningsBucket data; public GUI(EarningsBucket mydata) { data = mydata; class enterListener implements ActionListener { public void actionPerformed(ActionEvent event) { double minutes = Double.parseDouble(minutesTextField.getText()); double payment = Double.parseDouble(paymentTextField.getText()); data.recordSession(minutes, payment); minutesTextField.setText(""); paymentTextField.setText(""); } } class quitListener implements ActionListener { public void actionPerformed(ActionEvent event) { //TODO enter code } } class runReportListener implements ActionListener { public void actionPerformed(ActionEvent event) { String rawData = data.listData(); textArea.setText(rawData); } } enterListener = new enterListener(); quitListener = new enterListener(); runReportListener = new enterListener(); createWindow(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } public void createWindow() { JPanel textInputPanel = createTextInputPanel(); JPanel textAreaPanel = createTextAreaPanel(); JPanel buttonsPanel = createButtonsPanel(); add(textInputPanel, BorderLayout.NORTH); add(textAreaPanel, BorderLayout.CENTER); add(buttonsPanel, BorderLayout.SOUTH); // JPanel panel = new JPanel(); // panel.add(textInputPanel,BorderLayout.NORTH); // panel.add(textAreaPanel,BorderLayout.CENTER); // panel.add(buttonsPanel,BorderLayout.SOUTH); } public JPanel createTextAreaPanel() { textArea = new TextArea(); // textArea.setSize(TEXT_AREA_FRAME_WIDTH, TEXT_AREA_FRAME_HEIGHT); JPanel panel = new JPanel(); panel.add(textArea); panel.setSize(TEXT_AREA_FRAME_WIDTH, TEXT_AREA_FRAME_HEIGHT); return panel; } public JPanel createButtonsPanel() { runReportButton = new JButton(); runReportButton.setText("Run Report"); runReportButton.addActionListener(runReportListener); quitButton = new JButton(); quitButton.setText("Quit"); quitButton.addActionListener(quitListener); JPanel panel = new JPanel(); panel.add(runReportButton); panel.add(quitButton); return panel; } public JPanel createTextInputPanel() { minutesLabel = new JLabel(); minutesLabel.setText("Time in Minutes"); paymentLabel = new JLabel(); paymentLabel.setText("Payment"); minutesTextField = new JTextField(); paymentTextField = new JTextField(); enterButton = new JButton(); enterButton.setText("Enter"); enterButton.addActionListener(enterListener); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); panel.add(minutesLabel); panel.add(minutesTextField); panel.add(paymentLabel); panel.add(paymentTextField); panel.add(enterButton); // createTextInputPanel.setBorder(new TitledBorder(new EtchedBorder(), "Enter Info")); return panel; } }Last edited by Glyph; 10-19-2010 at 01:41 AM.
- 10-19-2010, 06:45 AM #5
- 10-19-2010, 03:17 PM #6
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
That's just so wrong. How can you ever declare a variable and assign to it the value of an expression that contains the same variable?
db
Today 01:38 AM
You are right. That aspect of this code is a mistake.
In the context it was presented, however, I was
making a different point. I think the OP was able
to create a fix from what I contributed.Last edited by paul pasciak; 10-19-2010 at 03:21 PM. Reason: changed "write" to right
- 10-19-2010, 07:43 PM #7
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
Style comment
At this point though I am curious though. While what I have works, I am wondering if it is considered better form to do it differently. (or really, if there is anything I did, that would be considered a sloppy way to do it)
When I first looked at your GUI, I had to study it a bit
because this type of implementation seems unconventional.
I find that it has several good qualities.
It is concise; logical, easy to read, and easy to understand.
I also am be curious to know any technical problems anyone
might find in this implementation style.
Similar Threads
-
Java interaction
By satimis in forum New To JavaReplies: 4Last Post: 06-30-2010, 05:14 PM -
Interaction with File System
By Italian_Programmer in forum AWT / SwingReplies: 2Last Post: 03-08-2010, 10:11 AM -
Basic Class Interaction Question
By McChill in forum New To JavaReplies: 1Last Post: 05-09-2009, 11:41 PM -
program interaction possible?
By Pierced1 in forum Advanced JavaReplies: 3Last Post: 02-19-2009, 12:54 AM -
Object Interaction
By Jinxes in forum New To JavaReplies: 1Last Post: 12-03-2007, 12:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks