1,105,578 Community Members

Using jtextfields, and parseint

Member Avatar
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

I have been searching high and low to find something remotely close to what I am trying to accomplish and have failed, miserably. I have read Oracle and tutorials, but have a hard time comprehending everything related to java. It is all so confusing. I would just be grateful for some advice in the correct direction because I want to cry and give up.

I am trying to create a program (with a GUI) that has several jtextfields. Across from them are a permanent string of shopping list items for reference. I am trying to get what the user inputs into the jtextfields added together and have the result displayed in the last jtextfield box "totalPrice." I have fixed it so that the user cannot alter the result.

Note: I must keep the random yes, maybe, no, caution, warning, exit buttons, jmenubar, and the jlabels in the array. I am building on an assignment from last week is why I mention this.

For whatever reason that is I have absolutely no idea. All the extra stuff seems to be irrelevant.

I apologize about the long back story. Here the code I have so far.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PracticeCode2 extends JFrame {  
	int result;
    //Initializing JPanels  
    private JPanel mainPanel, subPanel1, subPanel2;  
    //Setting up GUI  
    public PracticeCode2(){  
    	Toolkit.getDefaultToolkit();	//gets the toolkit ready for screen size and position
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	//chooses what happens when the user closes the JFrame 
        //Constructing Main JPanel with GridLayout of 1 row and 2 column  
        mainPanel = new JPanel();  
        mainPanel.setLayout(new GridLayout(1,2));  
        //Constructing JPanel 1 and 2 with GridLayout of 1 row and 1 column  
        subPanel1 = new JPanel();  
        subPanel1.setBorder(BorderFactory.createTitledBorder("Tech Shopping List"));  
        subPanel1.setLayout(new GridLayout(0, 1, 0, 0));
        subPanel2 = new JPanel();  
        subPanel2.setLayout(new GridLayout(0, 1, 0, 0));  
        //Adding JPanel 1 and 2 to main JPanel  
        //Setting up the container ready for the components to be added.  
        Container pane = getContentPane();  
        //Adding the main JPanel to the container  
        /**Array of Shopping Items listed as JLabels */
        JLabel[] labs = new JLabel[ 6 ];
        labs[0] = new JLabel( "Toshiba Laptop" );
        labs[1] = new JLabel("Kenneth Cole Laptop Case");
        labs[2] = new JLabel("Blank DVD-Rs");
        labs[3] = new JLabel("Microsoft Student Office 2010");
        labs[4] = new JLabel("AVG Antivirus");
        labs[5] = new JLabel("TOTAL");
        /**JTextFields to enter price of items */
        JTextField lapPrice  = new JTextField("Enter cost");
        JTextField lapCasePrice  = new JTextField("Enter cost");
        JTextField blankPrice = new JTextField("Enter cost");
        JTextField microPrice = new JTextField("Enter cost");
        JTextField avgPrice = new JTextField("Enter cost");
        JTextField totalPrice = new JTextField("");
        totalPrice.setEditable(false);	//Total field is not editable so the user can only view results and not change them
        int result = Integer.valueOf(lapCost.getText()) + Integer.valueOf(caseCost.getText()) + Integer.valueOf(blankCost.getText()) + Integer.valueOf(microCost.getText()) + Integer.valueOf(avgCost.getText());
        JButton reset = new JButton("Reset");
        JButton ok = new JButton("Ok");
        /**Set all the Components Visible. 
         * If it is set to "false", the components in the container will not be visible. 
    //Main Method  
    public static void main (String[] args) {  
        PracticeCode2 jp = new PracticeCode2();  
        /**Application Size */
		Dimension frameSize = Toolkit.getDefaultToolkit().getScreenSize();	//uses the screen size to determine JFrame visibility measurements
		int locationH = frameSize.height/2;
		jp.setBounds(((frameSize.width - locationH)/2), (frameSize.height - locationH)/2, locationH, locationH);	//enables the JFrame to be exactly half the height of the user screen, yet still positioned in the center
		/**JMenuBar */
		JMenuBar menub = new JMenuBar();	//a new menu bar is called up on to be placed
		jp.setJMenuBar(menub);	//sets the menu bar within the JFrame
		/** Item List*/
		JMenu file = new JMenu("File");	//calls a name to appear on the menu bar item
		menub.add(file);	//calls the item to appear on the menu bar
		JMenu edit = new JMenu("Edit");
		JMenuItem undo = new JMenuItem("Undo");	//inserts a drop down menu containing "Undo" and "Exit"
		edit.addSeparator();	//adds a separator between the "Undo" and "Exit"
		JMenuItem exit = new JMenuItem("Exit");
		JMenu window = new JMenu("Window");
		JMenu help = new JMenu("Help");

		// Lay components out with 5 columns
		jp.setLayout(new GridLayout(0, 1));
		JPanel mainPanel = new JPanel();
		mainPanel.add(new JButton("Yes"));	//adds a new button labeled "Ok"
		jp.add(mainPanel);	//adds the "Ok" button to the JPanel to be visually seen by the user
		mainPanel.add(new JButton("Maybe"));
		mainPanel.add(new JButton("No"));
		mainPanel.add(new JButton("Caution"));
		mainPanel.add(new JButton("Warning"));
		mainPanel.add(new JButton("Exit"));
    class ButtonListener implements ActionListener {

I was also trying to see if I could have a box somewhere that displayed the summary of the purchase.

All items amounted to $....

Cost of each item
Item1 $
Item2 $

Items bought were

Wishful thinking. :-/

Member Avatar
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]

use JFormattedTextField with Number formatter, and add DocumentListener

Member Avatar
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

use JFormattedTextField with Number formatter, and add DocumentListener

HA! Funny thing about that is in my current class I have not learned about that yet. That probably explains why I have been going out of my mind. I will take a look at those links and go back to my code. Thank you for your tips.

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: