4 Replies - 173 Views - Last Post: 06 October 2011 - 09:10 PM Rate Topic: -----

Topic Sponsor:

#1 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

How to access a class inside a GUI?

Posted 06 October 2011 - 07:42 PM

I made a very simple GUI. It asks for a start time and an end time. When you press calculate it will find the difference between those times. The input of the time and the calculations all happens in the Time class. I'm having trouble access the time class inside the GUI. I don't know how to.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import hsa.Console;



class timedatafield extends JFrame
{


    private JTextField _firstTime = new JTextField (3);
    private JTextField _secondTime = new JTextField (3);
    private JTextArea _timecalculation = new JTextArea ();

    public timedatafield ()
    {
    
    

        JButton differenceBtn = new JButton ("Difference");
        differenceBtn.addActionListener (new DifferenceBtnListener ());

        JPanel content = new JPanel ();
        content.setLayout (new BorderLayout ());
        JPanel north = new JPanel ();
        north.setLayout (new FlowLayout ());

        // 3... Add the components to the input area.
        north.add (new JLabel ("Starting Time")); // Create, add label
        north.add (_firstTime);            // Add input field
        north.add (new JLabel ("Ending Time")); // Create, add label
        north.add (_secondTime);          // Add output field
        north.add (differenceBtn);             // Add button

        content.add (north, "North");
        content.add (_timecalculation, "South");

        // 4... Set this window's attributes.
        setContentPane (content);
        pack ();
        setTitle ("Difference Calcualtion");
        setSize (400, 400);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo (null);           // Center window.
    }


    class DifferenceBtnListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {

            String lowTimeStr = A1.read (c);
            String highTimeStr = t2.read (c);
            A.difference (t2, A1);
            String outputStr = "The differnece between those two times is: ";
            _timecalculation.setText (outputStr);
            

            pack ();
        }
    }


    public static void main (String[] args)
    {
        

       
        timedatafield window = new timedatafield ();
        window.setVisible (true);
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: How to access a class inside a GUI?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6384
  • View blog
  • Posts: 25,923
  • Joined: 06-March 08

Re: How to access a class inside a GUI?

Posted 06 October 2011 - 07:53 PM

Where is your Time class ?
Where do you instantiate a Time object out of it ?
Was This Post Helpful? 0
  • +
  • -

#3 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

Re: How to access a class inside a GUI?

Posted 06 October 2011 - 08:23 PM

ok this is my time class

// The Time Class Version 1.0
import hsa.Console;
import java.awt.event.*;

class Time
{
    // data fields(will explain protected)
    protected int minutes, seconds, difference, totalseconds1, totalseconds2, total, secondsmod, tempsecond, tempminutes;
    protected int finaltime;
    protected int num = 0;
    protected boolean okay = false;

    // default constructor
    public Time ()
    {
        minutes = 0;
        seconds = 0;

    }


    // copy constructor
    public Time (Time t)
    {
        minutes = t.minutes;
        seconds = t.seconds;
    }


    // alternate constructor
    public Time (int min, int sec)
    {
        minutes = min;
        seconds = sec;

    }


    public void advance ()
    {
        seconds++;

        while (seconds > 60)
        {
            if (seconds >= 59)
            {
                seconds = 0;
                minutes++;
            }

        }
    }


    public void difference (Time t2, Time A1)
    {

        if (t2.minutes > A1.minutes || t2.minutes == A1.minutes && t2.seconds > A1.seconds)
        {
            totalseconds1 = (A1.minutes * 60) + A1.seconds;
            totalseconds2 = (t2.minutes * 60) + t2.seconds;
            total = totalseconds2 - totalseconds1;

            finaltime = (int) total / 60;

            minutes = finaltime;
            secondsmod = total % 60;
            seconds = secondsmod;
        }
        else
        {
            totalseconds1 = (A1.minutes * 60) + A1.seconds;
            totalseconds2 = (t2.minutes * 60) + t2.seconds;
            total = totalseconds1 - totalseconds2;

            finaltime = (int) total / 60;

            minutes = finaltime;
            secondsmod = total % 60;
            seconds = secondsmod;
        }

    }


    public void simplify (Time t3)
    {
        while (seconds >= 60)
        {
            if (t3.seconds <= 59)
                seconds = t3.seconds;
            else
            {
                t3.minutes++;
                seconds = t3.seconds - 60;
            }
        }
    }


    // read from keyboard
    public void read (Console c)
    {


        while (!okay)       // Loop will continue until successful conversion to int is made
        {

            try
            {
                c.print ("Minutes: ");
                String temp1 = c.readLine ();    // Reads in data as a string
                c.print ("Seconds: ");
                String temp2 = c.readLine ();
                minutes = Integer.parseInt (temp1); // Attempts to convert temp to an integer
                seconds = Integer.parseInt (temp2);
                okay = true;                        // If it gets this far, conversion worked
            }
            catch (NumberFormatException e)  // Catches failed attempt at conversion
            {
                c.println ("Please a time with proper minutes and seconds. Try again."); // Displays error message
            }
        }


    }


    // override Object toString for printing
    public String toString ()
    {
        if (seconds <= 9)

            return (minutes + ": 0" + seconds);
        else
            return (minutes + ": " + seconds);



    }
}



and this is my time class runner

// The "TimeTest" class.
import hsa.Console;

public class TimeTest
{
    static Console c;

    public static void main (String[] args)
    {
        c = new Console ();
        boolean keeptiming = true;
        char options; 
        while (keeptiming == true)
        {
            // declare times 0:00 and 1:20
            Time A = new Time ();
            Time t2 = new Time ();
            Time t3 = new Time ();
            Time t4 = new Time ();
            Time diff = new Time ();
            Time A1 = new Time ();



            c.println ("Enter a time");
            A.read (c);
            c.print ("The time is: ");
            c.println ("\t" + A + "");
            c.getChar ();
            c.print ("The time advanced by one second is: ");
            A.advance ();
            c.println (A + "");
            c.getChar ();
            c.println ("Enter two times");
            A1.read (c);
            t2.read (c);
            A.difference (t2, A1);
            c.print ("The differnece between those two times is: ");
            c.println (A + "");
            c.getChar ();
            c.println ("Enter a time with an odd second: ");
            t3.read (c);
            t3.simplify (t3);
            c.print (t3 + "");

            c.println (A + "");
            c.println (t3 + "");
        }
    } // main method
} // TimeTest class



so I have to create GUI and inside the action listener I have to calculate the time difference but I need to call the Time class. I don't know how to exactly code it.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6384
  • View blog
  • Posts: 25,923
  • Joined: 06-March 08

Re: How to access a class inside a GUI?

Posted 06 October 2011 - 09:03 PM

And where do you call/instantiate it from the code you posted ?
If we have to beg for every piece of code we are not out of the woods :)
Was This Post Helpful? 0
  • +
  • -

#5 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

Re: How to access a class inside a GUI?

Posted 06 October 2011 - 09:10 PM

I call it here

   class DifferenceBtnListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            Time A = new Time ();
            Time t2 = new Time ();
            Time t3 = new Time ();
            Time t4 = new Time ();
            Time diff = new Time ();
            Time A1 = new Time ();

            String lowTimeStr = A1.read (c);
            String highTimeStr = t2.read (c);
            A.difference (t2, A1);
            String outputStr = "The differnece between those two times is: ";
            _timecalculation.setText (outputStr);


            pack ();
        }
    }


Time is my class and in String lowTimeStr = =A1.read©
i'm trying to get a time from the user and all that happens in the class.
so i'm calling the class in this method
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1