www.webdeveloper.com
Recent Articles
  • SQL Server 2005 Express Edition - Part 4: Surface Area Configration
  • Hierarchical TableAdapters 301
  • Simple Game Programming in JavaScript
  • Ajax and PHP Part 4 - Forms and JavaScript Limitations
  • Oracle, MySQL and PostgreSQL feature comparison - Part 1
  • Speeding Up .NET
  • .NET Tip: Restrict the Access to Your Properties!
  • Learning Oracle by the Numbers
  • Logical Functions: IsGeneration(): Conditional Logic within Filter Expressions
  • Rendering Images in ASP.NET Directly from Your Database

  •  

    Go Back   WebDeveloper.com > Server-Side Development > Java
    (NOT JavaScript!)
    Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      # 1  
    Old 02-18-2006, 10:59 AM
    The One The One is offline
    Registered User
     
    Join Date: Feb 2006
    Posts: 98
    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
    THANKS

    **********************************
    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);

    out.close();

    }
    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);

    //Buttons
    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  
    Old 02-18-2006, 09:16 PM
    Oak's Avatar
    Oak Oak is offline
    J-Avatar
     
    Join Date: May 2004
    Location: Perth, WA
    Posts: 172
    No.
    __________________
    Perth Web Development
    Web Developer
    Perth, Western Australia
      # 3  
    Old 02-20-2006, 09:03 AM
    The One The One is offline
    Registered User
     
    Join Date: Feb 2006
    Posts: 98
    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  
    Old 02-20-2006, 11:37 AM
    Khalid Ali's Avatar
    Khalid Ali Khalid Ali is offline
    Super Moderator
     
    Join Date: Dec 2002
    Location: Calgary, Canada
    Posts: 8,788
    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 ();
            
    fileChooserDialogue . setFileSelectionMode ( JFileChooser . FILES_ONLY );
            
    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 );
                }
            }
            return
    file ;
        }

    if you are not using static methods then remove the static keyword from the method name decalration....
    __________________
    Cheers

    Khalid

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


    Go Back   WebDeveloper.com > Server-Side Development > Java
    (NOT JavaScript!)

    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread :

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread :

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    vB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:45 PM .



    Acceptable Use Policy

    JupiterOnlineMedia

    internet.com earthweb.com Devx.com mediabistro.com Graphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices , Licensing , Reprints , & Permissions , Privacy Policy .

    Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Powered by vBulletin® Version 3.5.7
    Copyright ©2000 - 2007, Jelsoft Enterprises Ltd.