Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default How do you create a save button?

    I am new to Java, and I created a wage calculator for my computer class. I have been searching the forums for a way to make a save button to save the text fields in an orderly way. In the future, I would like the person typing in their wage to be able to put their name on the file and for them to be saved by date. If anyone has a simple way to do this please let me know.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    import java.awt.event.*;
    
    public class RectangleProgram extends JFrame
    {
    	private static final int WIDTH = 2000;
    	private static final int HEIGHT = 400;
    	
    	private JLabel hoursL, wageL, payL, yearL, overtimeL, overtimehoursL, overallpayL, blankL, blank2L;
    	private JTextField hoursTF, wageTF, payTF, yearTF, overtimeTF, overtimehoursTF, overallpayTF;
    	private JButton calculateB, exitB, resetB;
    	
    	//Button handlers:
    	private CalculateButtonHandler cbHandler;
    	private ExitButtonHandler ebHandler;
    	private ResetButtonHandler rbHandler;
    	
    	public RectangleProgram()
    	{
    		hoursL = new JLabel("Enter the hours you worked (without overtime) : ", SwingConstants.RIGHT);
    		wageL = new JLabel("Enter your wage per hour : ", SwingConstants.RIGHT);
    		payL = new JLabel("Your pay is : ", SwingConstants.RIGHT);
    		yearL = new JLabel ("Your Gross Annual is : ", SwingConstants.RIGHT);
    		overtimeL = new JLabel ("Your overtime pay is : ", SwingConstants.RIGHT);
    		overtimehoursL = new JLabel ("How much overtime have you worked in hours? : ", SwingConstants.RIGHT);
    		overallpayL = new JLabel ("Your overall pay is : ", SwingConstants.RIGHT);
    		blankL = new JLabel ("Once you Enter all your data press this button ----->", SwingConstants.RIGHT);
    		blank2L = new JLabel ("");
    		
    		hoursTF = new JTextField(5);
    		wageTF = new JTextField(5);
    		payTF = new JTextField(5);
    		yearTF = new JTextField(5);
    		overtimeTF = new JTextField(5); 
    		overtimehoursTF = new JTextField(5);
    		overallpayTF = new JTextField(5);
    		
    		//SPecify handlers for each button and add (register) ActionListeners to each button.
    		calculateB = new JButton("Calculate");
    		cbHandler = new CalculateButtonHandler();
    		calculateB.addActionListener(cbHandler);
    		exitB = new JButton("Exit");
    		ebHandler = new ExitButtonHandler();
    		exitB.addActionListener(ebHandler);
    		resetB = new JButton("Reset");
    		rbHandler = new ResetButtonHandler();
    		resetB.addActionListener(rbHandler);
    
    
    		
    		
    		
    		setTitle("Your Pay");
    		Container pane = getContentPane();
    		pane.setLayout(new GridLayout(9, 2));
    		
    		//Add things to the pane in the order you want them to appear (left to right, top to bottom)
    		pane.add(hoursL);
    		pane.add(hoursTF);
    		pane.add(overtimehoursL);
    		pane.add(overtimehoursTF);
    		pane.add(wageL);
    		pane.add(wageTF);
    		pane.add(resetB);
    		pane.add(calculateB);
    		pane.add(payL);
    		pane.add(payTF);
    		pane.add(overtimeL);
    		pane.add(overtimeTF);
    		pane.add(overallpayL);
    		pane.add(overallpayTF);
    		pane.add(yearL);
    		pane.add(yearTF);
    		pane.add(blank2L);
    		pane.add(exitB);
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	
    	private class CalculateButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double hours, wage, pay, annual, overtime, extrapay, overtimewage, overall;
    			
    			hours = Double.parseDouble(hoursTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields.
    			wage = Double.parseDouble(wageTF.getText());
    			pay = hours * wage;
    			annual = wage * 2080;
    			overtime = Double.parseDouble(overtimehoursTF.getText());
    			overtimewage = wage * 1.5;
    			extrapay = overtimewage * overtime;
    			overall = pay + extrapay;
    			payTF.setText("$" + pay);
    			yearTF.setText("$" + annual);
    			overtimeTF.setText("$" + extrapay);
    			overallpayTF.setText("$" + overall);
    		}
    	}
    	
    	public class ExitButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    	
    	public class ResetButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			hoursTF.setText("");
    			wageTF.setText("");
    			payTF.setText("");
    			yearTF.setText("");
    			overtimeTF.setText("");
    			overtimehoursTF.setText("");
    			overallpayTF.setText("");
    		}
    	}
    	
    	public class SaveButtonHandler implements ActionListener
    	{
    		public void actionPerformed(Action Event e)
    		{
    			******I need a save button here, and if can't go here please help me find a spot where it can go.		}
    	}
    	public static void main(String[] args)
    	{
    		new RectangleProgram();
    	}
    	
    }
    Last edited by Norm; 04-04-2012 at 01:40 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: How do you create a save button?

    A button is a GUI component that would be shown. Your comments about a button are in a listener method.
    When should the button be shown to the user?

    Can you explain the sequence of events you want your program to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: How do you create a save button?

    I want the button to be seen after you enter data, and hit the calculate button.Also an answer to your first question is that my program starts up and you input three things into the program. Once you have inputted those you click the calculate button and it gives you three things.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: How do you create a save button?

    Where do you want the button to appear? Is there a blank space in the GUI reserved where you want to display it?
    Another choice would be to show the button initially but have it disabled until it is ready to be used.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: How do you create a save button?

    I like the idea of the last choice, and I have a space reserved under pane.add area. It is called (blank2L). I am going to put the button there when it is complete.

Similar Threads

  1. the save button
    By agater in forum AWT / Swing
    Replies: 4
    Last Post: 02-09-2012, 01:05 PM
  2. the save button
    By agater in forum Advanced Java
    Replies: 1
    Last Post: 02-08-2012, 07:25 PM
  3. Save button not working
    By twinytwo in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2009, 12:05 AM
  4. Save button not working ,,,,,
    By twinytwo in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 08:22 PM
  5. Disable Save button - Acrobar Reader
    By Deepa in forum New To Java
    Replies: 1
    Last Post: 03-06-2009, 12:15 PM

Tags for this Thread

Posting Permissions

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