Results 1 to 7 of 7

Thread: GUI interaction

  1. #1
    Glyph is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default 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
    Java 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);
        }
    
    }
    Earnings Bucket
    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;
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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));
       }
    }

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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;

  4. #4
    Glyph is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by paul pasciak View Post
    It would be code that looks like this:

    Java Code:
        int sum = sum + data.variableInEarningsBucket;
    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
    Last edited by DarrylBurke; 10-19-2010 at 06:53 AM.

  6. #6
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    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

  7. #7
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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

  1. Java interaction
    By satimis in forum New To Java
    Replies: 4
    Last Post: 06-30-2010, 05:14 PM
  2. Interaction with File System
    By Italian_Programmer in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2010, 10:11 AM
  3. Basic Class Interaction Question
    By McChill in forum New To Java
    Replies: 1
    Last Post: 05-09-2009, 11:41 PM
  4. program interaction possible?
    By Pierced1 in forum Advanced Java
    Replies: 3
    Last Post: 02-19-2009, 12:54 AM
  5. Object Interaction
    By Jinxes in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 12:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •