Results 1 to 13 of 13
  1. #1
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default Formatter with JOptionPane

    Hi Guys, :o

    I have made a GUI program with String components that involves the user entering different information and the JOptionPane shows the results. Now I have to make a file using the Formatter class to read what the user put in the JOptionPane. The code below just takes my whole GUI program and puts it into the output, how do I call just the JOptionPane??Thanks :D

    Java Code:
    import java.util.*; 
    import java.io.*;
    public class ReadFromFileWithScanner{
     
    public static void main(String[]args) {
     
     File rcfile = new File("C:\\Users\\Kelly\\Documents\\Misc\\RentalCarReservation.java"); //Declaring & Initalizing 
     try  {
     Scanner scn= new Scanner(rcfile);
     while(scn.hasNext()){
     String line = scn.nextLine();
     System.out.println(line);
    } 
       }
     catch(FileNotFoundException fnfe) {
      System.out.println("Here is your Rental Information!!"); 
      }
      Formatter output= new Formatter();
       try{ 
      output = new Formatter("C:\\Users\\Kelly\\Documents\\Misc\\RentalCarReservation.java");  //writes a new file for you but you need to open it 
      output.close(); //have to close the program
         }
     
     catch(FileNotFoundException fnfe){
     output.close();  
     System.out.println("File doesn't exist!!");   
     } // local to this catch 
     finally {
      output.close();  
     }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you elaborate your requirement bit more? Do you want to collect user information through the JOptionPane and write them into a file?

  3. #3
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Wink

    Yes, The GUI allows the user to collect information through JOptionPane and then the file I am writing below shows its supposed to make a new file with all of the users responses that they entered into JOptionPane.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't give that data file a ".java" extension.

    kind regards,

    Jos

  5. #5
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Question

    Should it be a .txt? The only reason I put it as a .java is because it actually is a .java file I am trying to get the information from. If it is a .txt how will that get me the information from the JOptionPane?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sweetpea123 View Post
    Should it be a .txt? The only reason I put it as a .java is because it actually is a .java file I am trying to get the information from.
    I'm still not clear on what it is you're trying to do and what you're stuck on, but perhaps it's me as I may not have enough blood in my caffeine system just yet. But it would be mighty unusual to use a Java file as a data file, mighty unusual.


    If it is a .txt how will that get me the information from the JOptionPane?
    Huh?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sweetpea123 View Post
    Should it be a .txt? The only reason I put it as a .java is because it actually is a .java file I am trying to get the information from. If it is a .txt how will that get me the information from the JOptionPane?
    Normally people (me included) expect to have Java source code in a .java file; I don't understand your last question.

    kind regards,

    Jos

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Normally people (me included) expect to have Java source code in a .java file;
    I remember reading once about a type of program that uses its own code file for its data file, a kind of self-referential type program, but I'm blanking out on its name. For sure, it's not what the OP wants though, and so I'm sorry to digress here.

    edit: maybe she wants to create a Quine.

    Or maybe not. :(
    Last edited by Fubarable; 03-28-2010 at 06:35 PM.

  9. #9
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    This is what my assignment says to do.
    Create a method that used Java Formatter class for your GUI so that when you click submit button, it will gather all input that the user provides and display them in a JOptionPane box and the program will then write the input into a file. I already have the JOptionPane box working to get the user information but know I am trying to work on the file part.:o

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sweetpea123 View Post
    This is what my assignment says to do. ...
    Could you post the entire assignment?

  11. #11
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    That is the entire assignment for this part of the GUI project I am working on. Like I said the first part was to make the GUI, the second part was to addActionListeners for the different componenets and the third is what I am doing now, creating a file that captures the input that I entered into JOptionPane. Its a lot of code but if you want the actionlistener part that involves the JOptionPane I can post that.


    Java Code:
     private class MyListener implements ActionListener { 
     public void actionPerformed (ActionEvent event) {
         
      String string2= new String(""); 
      String string1 = new String (); 
      
      if(event.getSource()== submit){ //submit 
       string1 = "First Name" + first1.getText()+ "\nLast Name "+last1.getText()+ "\n Drivers License Number:" + dlabel1.getText()+ "\n Pick Up Date:" +pubox.getText()+ "\n Pick Up Time:"+ hours2.getSelectedItem()
      + "\n Drop Off Date:"+ dobox.getText()+"\n Drop Off Time:" + hours.getSelectedItem()+"\n Car Type:"+ select.getSelectedItem()+ "\n Optional:"+ insurance.isSelected() + "\n Optional:"+gps.isSelected() +"\n Special Request:"+carea.getText()+
       "\n Payment Options:"+ card.isSelected()+card2.isSelected()+card3.isSelected()+card4.isSelected()+"\nCard Number:"+ cnbox.getText()+ "\nExpiration Date:" + edbox.getText();  
      
      
    	}
    	else if (event.getSource()==reset) {  
     	reset.setText(""); 
     		
    	}
    		
    	else if(event.getSource()==submit) { //credit cards visa 
    	if(card4.isSelected())
    	JOptionPane.showMessageDialog(null,"Visa");
    	}
    		
    	
    	else if(event.getSource()==submit) {
    	 if(card3.isSelected())
    	JOptionPane.showMessageDialog(null,"Mastercard");
    	
    	}
    	 
    	else if(event.getSource()==submit) {
    		if(card2.isSelected()) 
    		JOptionPane.showMessageDialog(null,"Discover");
    	}
    	
       else if(event.getSource()==submit) {
        if(card.isSelected()) 
        JOptionPane.showMessageDialog(null,"American Express");
        
    	}
    	else if(event.getSource()==submit) { //pick up hours 
    		String pickup=(String) hours.getSelectedItem();
    		ItemSelectable is = (ItemSelectable)event.getSource();
    	JOptionPane.showMessageDialog(null,"Pick Up: " + pickup);
          	}
    	
    	 else if(event.getSource()==submit){ // drop off hours 
    		String drop= (String) hours2.getSelectedItem();
    		ItemSelectable is =(ItemSelectable) event.getSource(); 
    		JOptionPane.showMessageDialog(null,"Drop Off:" + drop); 
    	}
    		
    	
    	else if(event.getSource()==submit){
    		String selectcar=(String) select.getSelectedItem(); 
    		ItemSelectable is =(ItemSelectable) event.getSource(); 
    		JOptionPane.showMessageDialog(null,"Car Type:" + selectcar); 	
    			}
    	 else if(event.getSource()==gps){
    		//	String gps=(String) select.getSelectedItem();
    			//ItemSelectable is= (ItemSelectable) event.getSource(); 
    		if(gps.isSelected())		
    	JOptionPane.showMessageDialog(null,"GPS was selected" ); 
    			}
    
    	else if (event.getSource()==insurance){
    		//String insurance=(String) select.getSelectedItem();
    		ItemSelectable is= (ItemSelectable) event.getSource(); 
    		if(insurance.isSelected())
    		JOptionPane.showMessageDialog(null,"Insurance was selected"); 	
    		}
    	else if (event.getSource()==gps){
    			if(gps.isSelected()&& insurance.isSelected()){
    		JOptionPane.showMessageDialog(null,"Insurance was selected"); 
    			}
    		} 
    	JOptionPane.showMessageDialog(null,string1); 
    			
    	} 		
    }//end of ActionListener 
    public static void main(String[]args){
     RentalCarReservation msg = new RentalCarReservation(); 
    	}
    	}

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think it would help to see your written instructions verbatim.

  13. #13
    sweetpea123 is offline Member
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Default

    Create a method that used Java Formatter class for your Rental Car Reservation System so that when you click submit button, it will gather all input that the user provides and display them in a JOptionPane box (you already did this task in homework 2) and the program will then write the input into a file.
    Note: File name must be the customer’s first name followed by “.txt” as a text file extension. As the example below, the file name should be “Bob.txt”.
    Tips: Formatter class can use a new line character “\n” when writing data to a new line however, it may be different depending on your operating system. Some OS system may use “%n” to represent a new line character.

Similar Threads

  1. Replies: 10
    Last Post: 11-01-2009, 09:42 PM
  2. JFormattedTextField / Formatter / OverWriteMode
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 08-20-2009, 11:23 AM
  3. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 10:51 PM
  4. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 02:04 AM
  5. Using java.util.Formatter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:29 PM

Posting Permissions

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