13 Replies - 576 Views - Last Post: 05 August 2012 - 04:53 AM Rate Topic: -----

#1 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Data from textfield to textfile

Posted 03 August 2012 - 12:03 AM

// this programme helps you to enter any data to a text file in a predetermined place
//by means of a text box and a action button....
/*
 * Input.java
 *
 * Created on August 3, 2012, 11:59 AM
 */

package me.org.source;

import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 *
 * @author  Vbabey
 */
public class Input extends javax.swing.JFrame {
    public String data;
    /** Creates new form Input */
    public Input() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        dataText = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Favourite Quote");

        okButton.setMnemonic('O');
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Favourite Quote");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dataText, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(okButton)
                .addGap(17, 17, 17))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(dataText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        data =dataText.getText();
        try{
  // Create file 
  FileWriter fstream = new FileWriter("C:\\Users\\Vbabey\\Desktop\\java\\out.txt");
  BufferedWriter out = new BufferedWriter(fstream);
  out.write(data);
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Input().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JTextField dataText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton okButton;
    // End of variables declaration
    
}

This post has been edited by smohd: 03 August 2012 - 12:40 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Data from textfield to textfile

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Data from textfield to textfile

Posted 03 August 2012 - 12:42 AM

So where is the question?
Also dont use NetBeans GUI Builder, it code is hard to be understood and maintained, and also wont help you to understand swing...
Was This Post Helpful? 0
  • +
  • -

#3 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 03 August 2012 - 02:03 AM

help me in return to code it in a far better way as you say so....can you help me...
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Data from textfield to textfile

Posted 03 August 2012 - 05:03 AM

Thjat should do it in fewer readable lines
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class QuoteWriter extends JFrame implements ActionListener {

	private JTextField tf;
	
	QuoteWriter() {
		super("Quote Writer");
		
		JPanel p = new JPanel();
		p.add(new JLabel("Favorite quote:"));
		tf = new JTextField(40);		

		Box box = Box.createHorizontalBox();
		box.add(Box.createHorizontalStrut(10));
		box.add(tf);
		box.add(Box.createHorizontalStrut(10));
		p.add(box);
		add(p);
		
		JButton b = new JButton("Save");
		b.addActionListener(this);
		p = new JPanel();
		p.add(B)/>;
		add(p, BorderLayout.SOUTH);
		
		setSize(500, 120);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e) {
		try {
			PrintWriter pw = new PrintWriter("quote.txt");
			pw.println(tf.getText());
			pw.close();
			tf.setText("Quote saved in quote.txt");
		}
		catch(IOException ioe) {
			System.out.println("Error: " + ioe);
		}
	}
	
	public static void main(String[] args) {
		new QuoteWriter();
	}
}


Was This Post Helpful? 1
  • +
  • -

#5 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 03 August 2012 - 07:06 AM

pbl...yes thanks...I used GUI builder to make it easy...Is it not good for using netbeans after getting knowing to most of the things in java...And also what do you use to make guiz in java???
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Data from textfield to textfile

Posted 03 August 2012 - 10:26 AM

View Postvbabey, on 03 August 2012 - 10:06 AM, said:

And also what do you use to make guiz in java???

What do you mean ?
Was This Post Helpful? 0
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Data from textfield to textfile

Posted 03 August 2012 - 11:36 AM

View Postvbabey, on 03 August 2012 - 09:06 AM, said:

pbl...yes thanks...I used GUI builder to make it easy...Is it not good for using netbeans after getting knowing to most of the things in java...And also what do you use to make guiz in java???


I certainly can't speak for pbl, since he is a a lot smarter than I am, but I write the code for my GUIs from scratch in an IDE, such as Eclipse.
Was This Post Helpful? 0
  • +
  • -

#8 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 03 August 2012 - 01:54 PM

I mean what you suggest me (still a beginner) to use in making GUI applications...
Use netbeans GUI builder of use normal coding...
Coding everything by myself???
Help me in making a real progress...

Thank you very much for your comment...

I also prefer netbeans or eclipse most of the time...
Was This Post Helpful? 0
  • +
  • -

#9 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Data from textfield to textfile

Posted 03 August 2012 - 01:58 PM

View Postvbabey, on 03 August 2012 - 03:54 PM, said:

I mean what you suggest me (still a beginner) to use in making GUI applications...
Use netbeans GUI builder of use normal coding...
Coding everything by myself???
Help me in making a real progress...

Thank you very much for your comment...

I also prefer netbeans or eclipse most of the time...

Being a beginner is tough, and very frustrating. But you need to work on the basics before you worry about a GUI, or you will never really understand what you're doing.

There are a few beginner tutorials in the java tutorial section that would be a great place to start. There are tons of books as well. Good luck.
Was This Post Helpful? 0
  • +
  • -

#10 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 03 August 2012 - 01:59 PM

View Postpbl, on 03 August 2012 - 05:26 PM, said:

View Postvbabey, on 03 August 2012 - 10:06 AM, said:

And also what do you use to make guiz in java???

What do you mean ?


I mean what you suggest me (still a beginner) to use in making GUI applications...
Use netbeans GUI builder of use normal coding...
Coding everything by myself???
Help me in making a real progress...
Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Data from textfield to textfile

Posted 03 August 2012 - 02:00 PM

Whatever you do don't use Java GUI builders - they are horrible creations and make us cry inside. If you get the urge to use one, stop yourself immediately or uninstall Netbeans (the drastic option).

As has been said before you should code all your GUI's by yourself, by hand. You can still do this in Netbeans/Eclipse, just choose a different project type.

If you don't know how to do this, then I suggest going back to basics and learn Java and Swing properly before jumping in the deep end. There are thousands of tutorials on how to code by hand on the internet just do a Google search. Good luck.
Was This Post Helpful? 1
  • +
  • -

#12 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 03 August 2012 - 02:05 PM

View Postsynlight, on 03 August 2012 - 08:58 PM, said:

View Postvbabey, on 03 August 2012 - 03:54 PM, said:

I mean what you suggest me (still a beginner) to use in making GUI applications...
Use netbeans GUI builder of use normal coding...
Coding everything by myself???
Help me in making a real progress...

Thank you very much for your comment...

I also prefer netbeans or eclipse most of the time...

Being a beginner is tough, and very frustrating. But you need to work on the basics before you worry about a GUI, or you will never really understand what you're doing.

There are a few beginner tutorials in the java tutorial section that would be a great place to start. There are tons of books as well. Good luck.


Thank you very much for the comment...
I am working on it...

View PostRyano121, on 03 August 2012 - 09:00 PM, said:

Whatever you do don't use Java GUI builders - they are horrible creations and make us cry inside. If you get the urge to use one, stop yourself immediately or uninstall Netbeans (the drastic option).

As has been said before you should code all your GUI's by yourself, by hand. You can still do this in Netbeans/Eclipse, just choose a different project type.

If you don't know how to do this, then I suggest going back to basics and learn Java and Swing properly before jumping in the deep end. There are thousands of tutorials on how to code by hand on the internet just do a Google search. Good luck.


Thank you very much for the advice...
I used eclipse and created some consoles and then jumped into GUI's by coding all in hand...Did some fun stuff,,,
I guess I should spend much time with coding by hand...
Thanks again for your advice...
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Data from textfield to textfile

Posted 03 August 2012 - 05:22 PM

View Postvbabey, on 03 August 2012 - 04:54 PM, said:

I mean what you suggest me (still a beginner) to use in making GUI applications...
Use netbeans GUI builder of use normal coding...

Write your own GUI

View Postvbabey, on 03 August 2012 - 04:54 PM, said:

Coding everything by myself???

Yes

View Postvbabey, on 03 August 2012 - 04:54 PM, said:

I also prefer netbeans or eclipse most of the time...

Pick up the one you prefer but do not use the GUI builder
Was This Post Helpful? 0
  • +
  • -

#14 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Data from textfield to textfile

Posted 05 August 2012 - 04:53 AM

Thank you very much pbl...
I started doing it,,,
Coding everything myself...
Just created a JSpinner...and worked a little bit...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1