Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: How do I enter data into a GUI application?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I enter data into a GUI application?

    I am currently tasked to work with this GUI application:

    asdfa.jpg

    Pardon me if I am not all familiar with the technical terms about Java, I'm still new. My first task is to write a method to enter data a car; I want to have the registration number (which is to be printed in the registration box), age (selected from the combo box) and whether it has or has not been in an accident (determined by the checkbox) displayed into text in the textbox once I hit ENTER after I've entered the information for all three, so far the GUI wouldn't take effect if I were to type in a registration number, select an age or tick the checkbox because I've not yet written a method for it.


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    This is my code:


    /*
    Course: Programming Fundamentals COIT11222 2014 Term2
    Purpose: Assignment two -- Motorcity Car Insurance GUI application
    Programmer: Bernard Li
    File: CarInsuranceGUI.java
    Date: 14 August 2014
    */

    /*
    Student Name: Shao Bang Lin
    Student Number: s0263796
    Date: 21/09/2014
    File Name: CarInsuranceGUI.java
    Purpose of the class: This class creates a Graphical User Interface (GUI)
    that is to be used by staff for providing details and
    producing reports of cars in the system.


    */


    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    import javax.swing.JOptionPane;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Font;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;


    public class CarInsuranceGUI extends JFrame
    {
    ///////////////////////////////////////////////////////////
    // declare your data structures and class variables here //
    ///////////////////////////////////////////////////////////


    // GUI components
    JLabel registrationLabel; // label for registration field
    JTextField registrationField; // field to enter the cars's registration
    JLabel ageLabel; // label for selecting the car's age from drop-down combo box
    JComboBox<String> ageCombo; // drop-down combo box for selecting the car's age
    JCheckBox accidentCheckBox; // check box for selecting if car has been in an accident or not
    JButton enterButton; // button for entering car's detail
    JButton displayAllButton; // button to display all cars entered so far
    JButton sortButton; // button to sort the car records by name
    JButton searchButton; // button to search for a car using it's registration
    JButton exitButton; // button to exit the program
    JTextArea textArea; // text area for displaying the data
    JScrollPane scrollPane; // scoll pane for text area scrolling

    // class variables

    private final int NUMBER_OF_CARS = 10; // the maximum amount of cars to be examined will be 10
    private final int AGE_THRESHOLD_FOR_INSURANCE_PRICE = 5; // the insurance threshold of a cars based on age
    private final double INSURANCE_FEE_BELOW_THRESHOLD = 200.0; // the insurance fee for a car below or equal to the age of 5
    private final double INSURANCE_FEE_ABOVE_THRESHOLD = 350.0; // the insurance fee for a car exceeding the age of 5
    private final double ACCIDENT_FREE_DISCOUNT = 25.00; // the percentage of the insurance price that will be discounted for cars that haven't had an accident

    // Constructor
    public CarInsuranceGUI()
    {
    super("Motorcity Car Insurance"); // invoke JFrame constructor
    setLayout(new FlowLayout()); // set the layout to flow layout

    registrationLabel = new JLabel("Registration"); // create registration label
    add(registrationLabel); // add the label to the JFrame
    registrationField = new JTextField(15); // create registration field
    add(registrationField); // add the registration field to the JFrame

    ageLabel = new JLabel("Age"); // create age label
    add(ageLabel); // add the name label
    ageCombo = new JComboBox<String>(); // create the age combo box
    for (int i = 0; i <= 30; i++) // populate the age combo box with numbers 0-30
    {
    ageCombo.addItem(i + ""); // add number as a string
    }
    add(ageCombo); // add the age combo

    accidentCheckBox = new JCheckBox("Accident?"); // create accident checkbox
    add(accidentCheckBox); // add accident checkbox

    enterButton = new JButton("Enter"); // create enter button
    add(enterButton); // add enter button

    displayAllButton = new JButton("Display All"); // create display all button
    add(displayAllButton); // add display all button

    sortButton = new JButton("Sort"); // create sort button
    add(sortButton); // add sort button

    searchButton = new JButton("Search"); // create search button
    add(searchButton); // add search button

    exitButton = new JButton("Exit"); // create exit button
    add(exitButton); // add exit button

    textArea = new JTextArea(16, 52); // create text area
    // set text area to a monospaced font so the columns can be aligned using a format string
    textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
    textArea.setEditable(false); // set the text area to be read only
    scrollPane = new JScrollPane(textArea); // put the text area into the scroll pane
    add(scrollPane); // add the scroll pane


    // add the ActionListener objects to all of the buttons and the name field
    ButtonHandler buttonEvent = new ButtonHandler(); // create event handler object

    enterButton.addActionListener(buttonEvent);
    registrationField.addActionListener(buttonEvent);
    displayAllButton.addActionListener(buttonEvent);
    sortButton.addActionListener(buttonEvent);
    searchButton.addActionListener(buttonEvent);
    exitButton.addActionListener(buttonEvent);

    // when the user pushes the system close (X top right corner)
    addWindowListener( // override window closing method
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    exit(); // Attempt to exit application
    }
    }
    );

    ////////////////////////////////////////////////////////////////////////
    // contruct your data structures and set up your class variables here //
    ////////////////////////////////////////////////////////////////////////
    final int NUMBER_OF_CARS = 10; // the maximum number of cars to be examined
    final int AGE_THRESHOLD_FOR_PRICE = 5; // insurance age threshold for cars
    final double COST_BELOW_THRESHOLD = 200.00; // cars less than or equal to age threshold are cheaper to insure
    final double COST_OVER_THRESHOLD = 350.00; // older cars cost more to insure
    final double ACCIDENT_FREE_DISCOUNT = 25.00; // amount of discount (in percent) given to cars without accidents

    String carRegistration; // the variable for the registration number of the car
    int carAge; // the variable for the age of the car

    } // end constructor


    // method used to enter that data for a car
    private void enterData()
    {


    registrationField.requestFocus();
    }// enterData


    // method used to display the cars entered into the program
    private void displayAll()
    {

    }// displayAll


    // method used to sort the car arrays based on registration
    private void sort()
    {
    // TO BE COMPLETED BY STUDENTS

    }// sort


    // method used to search for a car by registration
    private void search()
    {
    // TO BE COMPLETED BY STUDENTS

    }// search


    // method used to exit the program
    // a confirmation dialog should be shown to the user before exiting
    private void exit()
    {
    // TO BE COMPLETED BY STUDENTS
    System.exit(0);

    }// exit


    public static void main(String [] args)
    { // main method to create an instance of the class
    CarInsuranceGUI carInsurance = new CarInsuranceGUI(); // create instance of class

    carInsurance.setDefaultCloseOperation(JFrame.DO_NO THING_ON_CLOSE); // let the code close the program
    carInsurance.setSize(440, 390); // dimensions of the JFrame
    carInsurance.setVisible(true); // make the application visible
    } // main


    // private class used as an action listener for the buttons
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() == enterButton || e.getSource() == registrationField) // enter button or return hit in registration field
    enterData();
    else if (e.getSource() == displayAllButton) // display all button hit
    displayAll();
    else if (e.getSource() == sortButton) // sort button hit
    sort();
    else if (e.getSource() == searchButton) // search button hit
    search();
    else if (e.getSource() == exitButton) // exit button hit
    exit();
    }
    }// end ButtonHandler

    }// end class

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    import javax.swing.JOptionPane;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Font;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
     
    public class CarInsuranceGUI extends JFrame
    {
    	///////////////////////////////////////////////////////////
    	// declare your data structures and class variables here //
    	///////////////////////////////////////////////////////////
     
     
    	// GUI components
    	JLabel registrationLabel;		// label for registration field
    	JTextField registrationField;	// field to enter the cars's registration
    	JLabel ageLabel;				// label for selecting the car's age from drop-down combo box
    	JComboBox<String> ageCombo;		// drop-down combo box for selecting the car's age
    	JCheckBox accidentCheckBox;		// check box for selecting if car has been in an accident or not
    	JButton enterButton;			// button for entering car's detail
    	JButton displayAllButton;		// button to display all cars entered so far
    	JButton sortButton;				// button to sort the car records by name
    	JButton searchButton;			// button to search for a car using it's registration
    	JButton exitButton;				// button to exit the program
    	JTextArea textArea;				// text area for displaying the data
    	JScrollPane scrollPane;			// scoll pane for text area scrolling
     
    	// class variables
     
    	private final int NUMBER_OF_CARS = 10; // the maximum amount of cars to be examined will be 10
    	private final int AGE_THRESHOLD_FOR_INSURANCE_PRICE = 5; // the insurance threshold of a cars based on age
    	private final double INSURANCE_FEE_BELOW_THRESHOLD = 200.0; // the insurance fee for a car below or equal to the age of 5
    	private final double INSURANCE_FEE_ABOVE_THRESHOLD = 350.0; // the insurance fee for a car exceeding the age of 5
    	private final double ACCIDENT_FREE_DISCOUNT = 25.00; // the percentage of the insurance price that will be discounted for cars that haven't had an accident
     
    	// Constructor
    	public CarInsuranceGUI()
    	{
    		super("Motorcity Car Insurance");	 			// invoke JFrame constructor
    		setLayout(new FlowLayout());					// set the layout to flow layout
     
    		registrationLabel = new JLabel("Registration");	// create registration label
    		add(registrationLabel);							// add the label to the JFrame
    		registrationField = new JTextField(15);			// create registration field
    		add(registrationField);							// add the registration field to the JFrame
     
    		ageLabel = new JLabel("Age");					// create age label
    		add(ageLabel);									// add the name label
    		ageCombo = new JComboBox<String>();				// create the age combo box
    		for (int i = 0; i <= 30; i++)					// populate the age combo box with numbers 0-30
    		{
    			ageCombo.addItem(i + "");					// add number as a string
    		}
    		add(ageCombo);									// add the age combo
     
    		accidentCheckBox = new JCheckBox("Accident?");	// create accident checkbox
    		add(accidentCheckBox);							// add accident checkbox
     
    		enterButton = new JButton("Enter");				// create enter button
    		add(enterButton);								// add enter button
     
    		displayAllButton = new JButton("Display All");	// create display all button
    		add(displayAllButton);							// add display all button
     
    		sortButton = new JButton("Sort");				// create sort button
    		add(sortButton);								// add sort button
     
    		searchButton = new JButton("Search");			// create search button
    		add(searchButton);								// add search button
     
    		exitButton = new JButton("Exit");				// create exit button
    		add(exitButton);								// add exit button
     
    		textArea = new JTextArea(16, 52);				// create text area
    		// set text area to a monospaced font so the columns can be aligned using a format string
    		textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
    		textArea.setEditable(false);					// set the text area to be read only
    		scrollPane = new JScrollPane(textArea);			// put the text area into the scroll pane
    		add(scrollPane);								// add the scroll pane
     
     
    		// add the ActionListener objects to all of the buttons and the name field
    		ButtonHandler buttonEvent = new ButtonHandler();	// create event handler object
     
    		enterButton.addActionListener(buttonEvent);
    		registrationField.addActionListener(buttonEvent);
    		displayAllButton.addActionListener(buttonEvent);
    		sortButton.addActionListener(buttonEvent);
    		searchButton.addActionListener(buttonEvent);
    		exitButton.addActionListener(buttonEvent);
     
    		// when the user pushes the system close (X top right corner)
    		addWindowListener( // override window closing method
    			new WindowAdapter()
    			{
    				public void windowClosing(WindowEvent e)
    				{
    					exit();	// Attempt to exit application
    				}
    			}
    		);
     
    		////////////////////////////////////////////////////////////////////////
    		// contruct your data structures and set up your class variables here //
    		////////////////////////////////////////////////////////////////////////
    	final int NUMBER_OF_CARS = 10;                  // the maximum number of cars to be examined
        final int AGE_THRESHOLD_FOR_PRICE = 5;			// insurance age threshold for cars
    	final double COST_BELOW_THRESHOLD = 200.00;		// cars less than or equal to age threshold are cheaper to insure
    	final double COST_OVER_THRESHOLD = 350.00;		// older cars cost more to insure
    	final double ACCIDENT_FREE_DISCOUNT = 25.00;	// amount of discount (in percent) given to cars without accidents
     
    	String carRegistration; // the variable for the registration number of the car
    	int carAge;             // the variable for the age of the car
     
    	} // end constructor
     
     
    	// method used to enter that data for a car
    	private void enterData()
    	{
     
     
    		registrationField.requestFocus();
    	}// enterData
     
     
    	// method used to display the cars entered into the program
    	private void displayAll()
    	{
     
    	}// displayAll
     
     
    	// method used to sort the car arrays based on registration
    	private void sort()
    	{
    		// TO BE COMPLETED BY STUDENTS
     
    	}// sort
     
     
    	// method used to search for a car by registration
    	private void search()
    	{
    		// TO BE COMPLETED BY STUDENTS
     
    	}// search
     
     
    	// method used to exit the program
    	// a confirmation dialog should be shown to the user before exiting
    	private void exit()
    	{
    		// TO BE COMPLETED BY STUDENTS
    		System.exit(0);
     
    	}// exit
     
     
    	public static void main(String [] args)
    	{ // main method to create an instance of the class
    		CarInsuranceGUI carInsurance = new CarInsuranceGUI();			// create instance of class
     
    		carInsurance.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	// let the code close the program
    		carInsurance.setSize(440, 390);										// dimensions of the JFrame
    		carInsurance.setVisible(true);										// make the application visible
    	} // main
     
     
    	// private class used as an action listener for the buttons
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if (e.getSource() == enterButton || e.getSource() == registrationField)	// enter button or return hit in registration field
    				enterData();
    			else if (e.getSource() == displayAllButton)						// display all button hit
    				displayAll();
    			else if (e.getSource() == sortButton)							// sort button hit
    				sort();
    			else if (e.getSource() == searchButton)							// search button hit
    				search();
    			else if (e.getSource() == exitButton)							// exit button hit
    				exit();
    		}
    	}// end ButtonHandler
     
    }// end class

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I enter data into my GUI application

     
    /*
    	Course: Programming Fundamentals COIT11222 2014 Term2
    	Purpose: Assignment two -- Motorcity Car Insurance GUI application
    	Programmer: Bernard Li
    	File: CarInsuranceGUI.java
    	Date: 14 August 2014
    */
     
    /*
    	Student Name: Shao Bang Lin
    	Student Number: s0263796
    	Date: 21/09/2014
    	File Name: CarInsuranceGUI.java
    	Purpose of the class: This class creates a Graphical User Interface (GUI)
    	                      that is to be used by staff for providing details and
    	                      producing reports of cars in the system.
     
     
    */
     
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JComboBox;
    import javax.swing.JCheckBox;
    import javax.swing.JOptionPane;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Font;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
     
    public class CarInsuranceGUI extends JFrame
    {
    	///////////////////////////////////////////////////////////
    	// declare your data structures and class variables here //
    	///////////////////////////////////////////////////////////
     
     
    	// GUI components
    	JLabel registrationLabel;		// label for registration field
    	JTextField registrationField;	// field to enter the cars's registration
    	JLabel ageLabel;				// label for selecting the car's age from drop-down combo box
    	JComboBox<String> ageCombo;		// drop-down combo box for selecting the car's age
    	JCheckBox accidentCheckBox;		// check box for selecting if car has been in an accident or not
    	JButton enterButton;			// button for entering car's detail
    	JButton displayAllButton;		// button to display all cars entered so far
    	JButton sortButton;				// button to sort the car records by name
    	JButton searchButton;			// button to search for a car using it's registration
    	JButton exitButton;				// button to exit the program
    	JTextArea textArea;				// text area for displaying the data
    	JScrollPane scrollPane;			// scoll pane for text area scrolling
     
    	// class variables
     
    	private final int NUMBER_OF_CARS = 10; // the maximum amount of cars to be examined will be 10
    	private final int AGE_THRESHOLD_FOR_INSURANCE_PRICE = 5; // the insurance threshold of a cars based on age
    	private final double INSURANCE_FEE_BELOW_THRESHOLD = 200.0; // the insurance fee for a car below or equal to the age of 5
    	private final double INSURANCE_FEE_ABOVE_THRESHOLD = 350.0; // the insurance fee for a car exceeding the age of 5
    	private final double ACCIDENT_FREE_DISCOUNT = 25.00; // the percentage of the insurance price that will be discounted for cars that haven't had an accident
     
    	// Constructor
    	public CarInsuranceGUI()
    	{
    		super("Motorcity Car Insurance");	 			// invoke JFrame constructor
    		setLayout(new FlowLayout());					// set the layout to flow layout
     
    		registrationLabel = new JLabel("Registration");	// create registration label
    		add(registrationLabel);							// add the label to the JFrame
    		registrationField = new JTextField(15);			// create registration field
    		add(registrationField);							// add the registration field to the JFrame
     
    		ageLabel = new JLabel("Age");					// create age label
    		add(ageLabel);									// add the name label
    		ageCombo = new JComboBox<String>();				// create the age combo box
    		for (int i = 0; i <= 30; i++)					// populate the age combo box with numbers 0-30
    		{
    			ageCombo.addItem(i + "");					// add number as a string
    		}
    		add(ageCombo);									// add the age combo
     
    		accidentCheckBox = new JCheckBox("Accident?");	// create accident checkbox
    		add(accidentCheckBox);							// add accident checkbox
     
    		enterButton = new JButton("Enter");				// create enter button
    		add(enterButton);								// add enter button
     
    		displayAllButton = new JButton("Display All");	// create display all button
    		add(displayAllButton);							// add display all button
     
    		sortButton = new JButton("Sort");				// create sort button
    		add(sortButton);								// add sort button
     
    		searchButton = new JButton("Search");			// create search button
    		add(searchButton);								// add search button
     
    		exitButton = new JButton("Exit");				// create exit button
    		add(exitButton);								// add exit button
     
    		textArea = new JTextArea(16, 52);				// create text area
    		// set text area to a monospaced font so the columns can be aligned using a format string
    		textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
    		textArea.setEditable(false);					// set the text area to be read only
    		scrollPane = new JScrollPane(textArea);			// put the text area into the scroll pane
    		add(scrollPane);								// add the scroll pane
     
     
    		// add the ActionListener objects to all of the buttons and the name field
    		ButtonHandler buttonEvent = new ButtonHandler();	// create event handler object
     
    		enterButton.addActionListener(buttonEvent);
    		registrationField.addActionListener(buttonEvent);
    		displayAllButton.addActionListener(buttonEvent);
    		sortButton.addActionListener(buttonEvent);
    		searchButton.addActionListener(buttonEvent);
    		exitButton.addActionListener(buttonEvent);
     
    		// when the user pushes the system close (X top right corner)
    		addWindowListener( // override window closing method
    			new WindowAdapter()
    			{
    				public void windowClosing(WindowEvent e)
    				{
    					exit();	// Attempt to exit application
    				}
    			}
    		);
     
    		////////////////////////////////////////////////////////////////////////
    		// contruct your data structures and set up your class variables here //
    		////////////////////////////////////////////////////////////////////////
    	final int NUMBER_OF_CARS = 10;                  // the maximum number of cars to be examined
        final int AGE_THRESHOLD_FOR_PRICE = 5;			// insurance age threshold for cars
    	final double COST_BELOW_THRESHOLD = 200.00;		// cars less than or equal to age threshold are cheaper to insure
    	final double COST_OVER_THRESHOLD = 350.00;		// older cars cost more to insure
    	final double ACCIDENT_FREE_DISCOUNT = 25.00;	// amount of discount (in percent) given to cars without accidents
     
    	String carRegistration; // the variable for the registration number of the car
    	int carAge;             // the variable for the age of the car
     
    	} // end constructor
     
     
    	// method used to enter that data for a car
    	private void enterData()
    	{
     
     
    		registrationField.requestFocus();
    	}// enterData
     
     
    	// method used to display the cars entered into the program
    	private void displayAll()
    	{
     
    	}// displayAll
     
     
    	// method used to sort the car arrays based on registration
    	private void sort()
    	{
    		// TO BE COMPLETED BY STUDENTS
     
    	}// sort
     
     
    	// method used to search for a car by registration
    	private void search()
    	{
    		// TO BE COMPLETED BY STUDENTS
     
    	}// search
     
     
    	// method used to exit the program
    	// a confirmation dialog should be shown to the user before exiting
    	private void exit()
    	{
    		// TO BE COMPLETED BY STUDENTS
    		System.exit(0);
     
    	}// exit
     
     
    	public static void main(String [] args)
    	{ // main method to create an instance of the class
    		CarInsuranceGUI carInsurance = new CarInsuranceGUI();			// create instance of class
     
    		carInsurance.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	// let the code close the program
    		carInsurance.setSize(440, 390);										// dimensions of the JFrame
    		carInsurance.setVisible(true);										// make the application visible
    	} // main
     
     
    	// private class used as an action listener for the buttons
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if (e.getSource() == enterButton || e.getSource() == registrationField)	// enter button or return hit in registration field
    				enterData();
    			else if (e.getSource() == displayAllButton)						// display all button hit
    				displayAll();
    			else if (e.getSource() == sortButton)							// sort button hit
    				sort();
    			else if (e.getSource() == searchButton)							// search button hit
    				search();
    			else if (e.getSource() == exitButton)							// exit button hit
    				exit();
    		}
    	}// end ButtonHandler
     
    }// end class

    I am supposed to use the code above to edit this:

    asdfa.jpg

    right now I want to know what I should write in order to make this GUI application display the data I've written when I hit the ENTER button, details would be nice but please keep it simple and basic, I am new.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    Threads merged and moved.

    Please do not start multiple threads on the same topic. You don't need a do-over, just improve the existing thread, which has now been done.

    What you need to do is complete the method:
        // method used to enter that data for a car
        private void enterData()
        {
     
     
            registrationField.requestFocus();
        }// enterData
    to do what you've described. I suspect you knew that much, so then your question becomes, "How do I complete the enterData() method to obtain and store the data entered into the GUI by the user?"

    In general:

    1. Collect the data from the JTextField registrationField and store it into a variable,
    2. Collect the data from the JComboBox ageCombo and store it into a variable,
    3. Collect the state of the JCheckBox accidentCheckBox and store it into a variable,

    etc. . . .

    and then display that data or create a new object using the data as needed to continue with the next task.

    If you do not know how to do the above (collect the data or state from each component), then refer to the API for each component to see which methods it already has to collect its contents or its state.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Joey231 (September 20th, 2014)

  8. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    Thank you so much

    --- Update ---

    A question about the registrationField, ageCombo and accidentCheckBox. How do I declare them, btw this code wasn't written by me but written for me, my task is to complete the enterData() method, displayAll() method etc.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    In general:

    1. Collect the data from the JTextField registrationField and store it into a variable,
    2. Collect the data from the JComboBox ageCombo and store it into a variable,
    3. Collect the state of the JCheckBox accidentCheckBox and store it into a variable,
    Hi Greg, can you further clarify how this is to be done? I do wish to do most of my work by myself but I am pretty unfamiliar with these tasks you've listed.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    my task is to complete the enterData() method, displayAll() method etc.
    Yep, I gathered that. Next time, lead with that explanation.
    How do I declare them
    The registrationField, ageCombo, and accidentCheckBox components already exist. Declaring/initializing them is already done. How do you address or use them? You type their name which refers to the object created for each and then apply methods to the name using the dot operator, '.', to obtain their data or state. You would see this explained if you referred to the API page for each.
    can you further clarify how this is to be done?
    For example, registrationField is a JTextField. Open the API for a JTextField and read that API, looking for how to use it and the methods it offers that will be helpful to you.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    Joey231 (September 20th, 2014)

  11. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    Ok, thank you so much

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    You're welcome. Come back when you need more help after you've done some reading and tried some things. Start simple: get the data from the registrationField and send it to the console using a print statement.

  13. The Following User Says Thank You to GregBrannon For This Useful Post:

    Joey231 (September 20th, 2014)

  14. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    By API are you referring to the Application Programming Interface? do you know how I can open this API page?

  15. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    For most every SE class, Google "java className doc" and a link to the API page should be the top 1-3 results. For the JTextField, Google "java jtextfield doc". If this is brand new information to you, you either haven't been paying attention, attending class, doing the required reading, or it's time to find another Java class.

  16. #13
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    I have had attendance issues, but I really don't recall any mentions of API in class. Anyway I have found a page I believe to be relevant: JTextField (Java Platform SE 7 )

    A list of methods for JTextField is on the list, I've read through it a few times but still aren't able to determine which is the one I should use.

  17. #14
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How do I enter data into a GUI application?

    Well, it is a TextField and what you want is the Text from the TextField. Perhaps the getText() method that is listed in the API will help you? Read the documentation that is listed here:
    JTextComponent (Java Platform SE 7 )

  18. The Following User Says Thank You to Cornix For This Useful Post:

    Joey231 (September 20th, 2014)

  19. #15
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    Ok, thank you

  20. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    You just need to catch up.

    The page you found is relevant. First, at the top of the API page notice that the lineage of JTextField is:
    java.lang.Object
        java.awt.Component
            java.awt.Container
                javax.swing.JComponent
                    javax.swing.text.JTextComponent
                        javax.swing.JTextField
    From that, you might deduce that JTextField not only has the methods described in the Methods section of the API page which are specific to JTextField, but that it also can use the methods of its ancestors - that's how Java and inheritance works. With that in mind, if you keep looking down the page, you'll notice that there are other methods listed as being inherited from its ancestors.

    Do you see any method names in those inherited methods that sound hopeful? The method you're looking for probably starts with 'get' and will be basic to ALL components that contain text.

  21. #17
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I enter data into a GUI application?

    private void enterData()
    	{
    		textArea.setText(String.format("%10s %10s %10s %10s","Registration","Age","Accident","Fee\n"));
     
    		registrationField.getText();
     
     
    		registrationField.requestFocus();
    	}// enterData

    I have added registrationField.getText(); and it compiles, but what am I meant to do to actually allow text to appear into the GUI?

    --- Update ---

    Oh wait I wasn't meant to type registrationField.getText() my apologies

    --- Update ---

    Well I was expecting to find something like getText() but it's not listed, I did try registrationField.getText(); and I was able to compile.

  22. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I enter data into a GUI application?

    but it's not listed
    Yes it is, as I described in Post #16.

    This is an intermediate Java class. Have you completed a basic Java class or any basic programming class? If not, you might be in over your head, and you should consider dropping this one, taking a basic programming class when you can, and come back to this one later.

Similar Threads

  1. Need Some GUI Help With My Application
    By amitection in forum AWT / Java Swing
    Replies: 3
    Last Post: May 8th, 2014, 02:02 AM
  2. [SOLVED] Application Gui Issue
    By kit367 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2014, 03:42 PM
  3. how can I get my program to enter data on a website?
    By leonne in forum Java Theory & Questions
    Replies: 4
    Last Post: April 21st, 2013, 04:10 PM
  4. insert data through gui application (JFrame)
    By muzammil786 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 08:30 AM
  5. Connecting GUI with application
    By kakb9091 in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2012, 08:19 PM