Results 1 to 4 of 4

Thread: file chooser help?

  1. #1
    Join Date
    Feb 2006

    Lightbulb file chooser help?

    Would someone be so kind to add on a file chooser, ie when i click save a window pops up asking for the destination to save?? aswell if possible the same for open???
    cos right now everything saves in one file but that data is always over righted wen new data is added. so i need a file chooser.
    Ic tried addin a file choose but cant seem to get it workin or put in the right place.

    PLEASE help

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    public class GUITest1 {
    public static void Salary () {

    int Button_width = 65;
    int Button_height = 25;

    //Total Gross Salary
    JLabel BasicSalaryLabel = new JLabel ("Basic Salary:");
    JLabel LunchAllowLabel = new JLabel ("Lunch Allowance:");
    JLabel HousingAllowLabel = new JLabel ("Housing Allowance:");
    JLabel TravelAllowLabel = new JLabel ("Travel Allowance:");
    JLabel BonusLabel = new JLabel ("Bonus:");
    JLabel OvertimeLabel = new JLabel ("Overtime:");
    JLabel GrossSalaryLabel = new JLabel ("GROSS SALARY:");
    final JTextField BasicSalarytextfield = new JTextField(15);
    final JTextField LunchAllowtextfield = new JTextField (15);
    final JTextField HousingAllowtextfield = new JTextField (15);
    final JTextField TravelAllowtextfield = new JTextField (15);
    final JTextField Bonustextfield = new JTextField (15);
    final JTextField Overtimetextfield = new JTextField (15);
    final JTextField GrossSalarytextfield = new JTextField (15);

    //Total Deductions
    JLabel AdvanceLabel = new JLabel ("Advance:");
    JLabel LoanLabel = new JLabel ("Loan:");
    JLabel PAYELabel = new JLabel ("PAYE:");
    JLabel NAPSALabel = new JLabel ("NAPSA:");
    JLabel TDeductionsLabel = new JLabel ("TOTAL DEDUCTIONS:");
    final JTextField Advancetextfield = new JTextField(15);
    final JTextField Loantextfield = new JTextField (15);
    final JTextField PAYEtextfield = new JTextField (15);
    final JTextField NAPSAtextfield = new JTextField (15);
    final JTextField TDeductionstextfield = new JTextField (15);

    //Final Net Salary
    JLabel NetSalaryLabel = new JLabel ("NET SALARY:");
    final JTextField NetSalarytextfield = new JTextField (15);

    JButton Calcbutton = new JButton("Calculate");
    Calcbutton.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Savebutton = new JButton("Save");
    Savebutton.setPreferredSize (new Dimension (Button_width, Button_height));

    //Calculate Class
    class CalcButtonListener implements ActionListener {

    public void actionPerformed (ActionEvent event) {

    String BasicSalary1 = BasicSalarytextfield.getText();
    String LunchAllow1 = LunchAllowtextfield.getText();
    String HousingAllow1 = HousingAllowtextfield.getText();
    String TravelAllow1 = TravelAllowtextfield.getText();
    String Bonus1 = Bonustextfield.getText();
    String Overtime1 = Overtimetextfield.getText();
    int BasicSalary2 = Integer.parseInt (BasicSalary1);
    int LunchAllow2 = Integer.parseInt (LunchAllow1);
    int HousingAllow2 = Integer.parseInt (HousingAllow1);
    int TravelAllow2 = Integer.parseInt (TravelAllow1);
    int Bonus2 = Integer.parseInt (Bonus1);
    int Overtime2 = Integer.parseInt (Overtime1);

    int Total1 = (BasicSalary2 + LunchAllow2 + HousingAllow2 + TravelAllow2 + Bonus2 + Overtime2);;
    GrossSalarytextfield.setText (new Integer(Total1).toString());

    String Advance1 = Advancetextfield.getText();
    String Loan1 = Loantextfield.getText();
    String PAYE1 = PAYEtextfield.getText();
    String NAPSA1 = NAPSAtextfield.getText();
    int Advance2 = Integer.parseInt (Advance1);
    int Loan2 = Integer.parseInt (Loan1);
    int PAYE2 = Integer.parseInt (PAYE1);
    int NAPSA2 = Integer.parseInt (NAPSA1);

    int Total2 = (Advance2 + Loan2 + PAYE2 + NAPSA2);
    TDeductionstextfield.setText (new Integer(Total2).toString());

    int Totalf = (Total1 - Total2);
    NetSalarytextfield.setText (new Integer(Totalf).toString());


    ActionListener Calc = new CalcButtonListener ();
    Calcbutton.addActionListener (Calc);

    //Save Class
    class SaveButtonListener implements ActionListener {

    public void actionPerformed (ActionEvent event) {

    try {

    FileWriter writer = new FileWriter ("Salary.txt");
    PrintWriter out = new PrintWriter (writer);

    String AddBasicSalary = BasicSalarytextfield.getText();
    String AddLunchAllow = LunchAllowtextfield.getText();
    String AddHousingAllow = HousingAllowtextfield.getText();
    String AddTravelAllow = TravelAllowtextfield.getText();
    String AddBonus = Bonustextfield.getText();
    String AddOvertime = Overtimetextfield.getText();
    String AddGrossSalary = GrossSalarytextfield.getText();
    out.println (AddBasicSalary);
    out.println (AddLunchAllow);
    out.println (AddHousingAllow);
    out.println (AddTravelAllow);
    out.println (AddBonus);
    out.println (AddOvertime);
    out.println (AddGrossSalary);

    String AddAdvance = Advancetextfield.getText();
    String AddLoan = Loantextfield.getText();
    String AddPAYE = PAYEtextfield.getText();
    String AddNAPSA = NAPSAtextfield.getText();
    String AddTDeductions = TDeductionstextfield.getText();
    out.println (AddAdvance);
    out.println (AddLoan);
    out.println (AddPAYE);
    out.println (AddNAPSA);
    out.println (AddTDeductions);

    String AddNetSalary = NetSalarytextfield.getText();
    out.println (AddNetSalary);


    catch (IOException exception) {

    System.out.println ("can't write to file");

    ActionListener listener = new SaveButtonListener ();
    Savebutton.addActionListener (listener);

    final JFrame frame = new JFrame ("Salary Sheet");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (15,2));

    //Gross salary
    frame.add (BasicSalaryLabel);
    frame.add (BasicSalarytextfield);
    frame.add (LunchAllowLabel);
    frame.add (LunchAllowtextfield);
    frame.add (HousingAllowLabel);
    frame.add (HousingAllowtextfield);
    frame.add (TravelAllowLabel);
    frame.add (TravelAllowtextfield);
    frame.add (BonusLabel);
    frame.add (Bonustextfield);
    frame.add (OvertimeLabel);
    frame.add (Overtimetextfield);

    //Total deductions
    frame.add (AdvanceLabel);
    frame.add (Advancetextfield);
    frame.add (LoanLabel);
    frame.add (Loantextfield);
    frame.add (PAYELabel);
    frame.add (PAYEtextfield);
    frame.add (NAPSALabel);
    frame.add (NAPSAtextfield);

    //Calculated values
    frame.add (GrossSalaryLabel);
    frame.add (GrossSalarytextfield);
    frame.add (TDeductionsLabel);
    frame.add (TDeductionstextfield);
    frame.add (NetSalaryLabel);
    frame.add (NetSalarytextfield);

    frame.add (Savebutton);
    frame.add (Calcbutton);

    frame.pack ();
    frame.show ();


    //Main Class
    public static void main (String [] args) {

    int Button_width = 65;
    int Button_height = 25;

    JButton Namebutton = new JButton("Name");
    Namebutton.setPreferredSize (new Dimension (Button_width, Button_height));

    JButton Salarybutton = new JButton("Salary Sheet");
    Salarybutton.setPreferredSize (new Dimension (Button_width, Button_height));

    final JFrame frame = new JFrame ("Salary Sheet");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.setLayout (new GridLayout (2,1));

    frame.add (Namebutton);
    frame.add (Salarybutton);

    frame.pack ();
    frame.show ();

    //Salary button listener
    class SalaryButtonListener implements ActionListener {

    public void actionPerformed (ActionEvent event) {

    Salary ();
    frame.setVisible (false);

    ActionListener Salary = new SalaryButtonListener ();
    Salarybutton.addActionListener (Salary);


  2. #2
    Join Date
    May 2004
    Fremantle, WA
    Company List
    Australian Local Business Directory

  3. #3
    Join Date
    Feb 2006

    Unhappy Help

    please does someone have at least the basic code so that wen i press save for example u can select the name and directory??? HELP PLS

  4. #4
    Join Date
    Dec 2002
    Calgary, Canada
    ok...I guess I can bite the nail here then...below is a function that will do what u want exactly. When this method is called it will show the file chooser dialigue and will return a handle to the file that a user may selects..

    PHP Code:
         * creates a file selection dialogue. And then returns the selected file for users work.
         * @return File
    public static File getFile() {
    File file;
    file null;

    JFileChooser fileChooserDialogue = new JFileChooser();
    int result fileChooserDialogue.showOpenDialog(parent);
            if (
    result == 0) {
    file fileChooserDialogue.getSelectedFile();
                if (
    file == null) {
    JOptionPane.showMessageDialog(null"Invalid file name""Invalid file name"JOptionPane.ERROR_MESSAGE);
    if you are not using static methods then remove the static keyword from the method name decalration....


    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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