Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JOptionPane with Multiple JTextFields

  1. #1
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Question JOptionPane with Multiple JTextFields

    Here's My code to create multiple JTextfields in a Single Option Pane.
    My goal is simple.
    Take some input from the user and then store it a TEXT File.
    The code is simple enough so i dont think it needs any commenting.

    package printer;
     
    /**
     *
     * @author Amit
     */
     
    import java.awt.Toolkit;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.*;
     
    public class Database {
     
    	JTextField name =  new JTextField();
    	JTextField roll =  new JTextField();
    	JTextField batch=  new JTextField();
     
            Object[] inputs = {
                "Name",name,"Roll No",roll,"Batch",batch};
    	/*final JComponent[] inputs = new JComponent[]{
    			new JLabel("Name"),Name,new JLabel("Roll No"),Roll,new JLabel("Batch"),Batch	
    		};
    	*/
     
    	Database()
    	{
    		JOptionPane.showMessageDialog(null,inputs,"Enter Details",JOptionPane.PLAIN_MESSAGE);
    	}
     
    	public void saveText() throws IOException
    	{
    		BufferedWriter out = null;
    		try{
    			FileWriter fstream = new FileWriter("C:\\Users\\Amit\\Documents\\databaseFile.txt",true);
    			out = new BufferedWriter(fstream);
    			String s = "Name: "+name+System.lineSeparator()+"Roll No: "+roll+System.lineSeparator()+
    			"Batch: "+batch+System.lineSeparator();
    			out.write(s);
                            System.out.println(name.getText());
     
    		}catch(IOException e){
    			Toolkit.getDefaultToolkit().beep();
    			//Beep signal emitted on reciept of and error
    			JOptionPane.showMessageDialog(null,"Could Not Locate Database","Error",JOptionPane.WARNING_MESSAGE);
    		}finally{
    			out.close();
    		}
    	}
     
    }


    The problem with the code is that the output in the text file is not what is want.
    I want the output to be like for eg:
    Name: Amit
    Roll No: S1070
    Batch: D

    but i am getting this in the text file:

    Name: javax.swing.JTextField[,0,19,232x28,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@6df16cbd,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]
    Roll No: javax.swing.JTextField[,0,69,232x28,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@46aa1188,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]
    Batch: javax.swing.JTextField[,0,119,232x28,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@7954886d,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

    Thanks For Your Help In Advance!
    ăϻі†


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: JOptionPane with Multiple JTextFields

    That seems like an object file for one of the Swing libraries (I am not certain though). Just a quick suggestion - when you
    state the txt. file location, shouldn't it be a single '\' escape character rather than a double one '\\'? Again, only a thought.

    Looking at the file output, it does seem like the sort of code that is executed via the JVM - not really what you want.
    I am sure other can give you a better answer, thought I'd add my two cents.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane with Multiple JTextFields

    i am getting this
    That looks like the String that is returned by the class's toString() method.
    If you want the data contents of the class, you need to call one of the class's methods that will return the class's contents.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: JOptionPane with Multiple JTextFields

    Quote Originally Posted by Norm View Post
    That looks like the String that is returned by the class's toString() method.
    If you want the data contents of the class, you need to call one of the class's methods that will return the class's contents.
    When i check the data in the variable name by printing it to the console using System.out.println(name) i get the correct correct content.
    For example if i enter NORM i get NORM in console but In the file it is something like

    Name: javax.swing.JTextField[,0,19,232x28,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@6df16cbd,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]
    ăϻі†

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JOptionPane with Multiple JTextFields

    Have you solved the problem now? If not, please show what you expect the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: JOptionPane with Multiple JTextFields

    Finally Found The Solution!!!

    Earlier it was..

    out = new BufferedWriter(fstream);
    			String s = "Name: "+name+System.lineSeparator()+"Roll No: "+roll+System.lineSeparator()+
    			"Batch: "+batch+System.lineSeparator();
    			out.write(s);

    Simply changed this to..

    out = new BufferedWriter(fstream);
    			String s = "Name: "+name.getText()+System.lineSeparator()+"Roll No: "+roll.getText()+System.lineSeparator()+
    			"Batch: "+batch.getText()+System.lineSeparator();
    			out.write(s);

    And it worked...
    ăϻі†

Similar Threads

  1. jTextfields and JTables
    By JoeSmo in forum AWT / Java Swing
    Replies: 3
    Last Post: May 7th, 2013, 11:50 PM
  2. JCombo Box to populate 2 JTextFields
    By jdubicki in forum AWT / Java Swing
    Replies: 2
    Last Post: June 8th, 2012, 11:06 AM
  3. JOptionPane menu and multiple methods
    By kubera in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2012, 07:03 PM
  4. Creating an Applet program using JTextFields, JLabel, Jbutton.
    By Maxly in forum Java Theory & Questions
    Replies: 1
    Last Post: March 9th, 2012, 02:39 PM
  5. Input and Output in JTextFields and JTextAreas - Interactive Fiction
    By lucienmontierre in forum AWT / Java Swing
    Replies: 1
    Last Post: April 27th, 2011, 08:14 AM

Tags for this Thread