Results 1 to 6 of 6
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Create a form, input some data and save to file

    Hi.
    I have a frame and button in that frame. click on the button opens form (new window) with fields (JTextFields). when the fields are filled click on the form's button stores the data in the file.

    My doubt is:
    Which component to use as a form window? Use JFrame or something else?
    Maybe a stupid question, but I do not know what to use when I have the main window and I need to open another window.

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

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thank you.
    I have written almost the entire program, with JFrame as main window, and new JFrame as form window. when I'm done with the program I'll post it here. Later, I'll try to write it with JDialog or JOptionPane.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    It's almost done. I have a problem with MouseListener : program does not write to file.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.File;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class OpeningForm {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				BasicForm frame = new BasicForm("Basic Frame");
    				InputForm inputForm = new InputForm();
    				frame.createGUI();
    			}
    		});
    	}
    
    }
    
    class BasicForm extends JFrame {
    	public BasicForm(String name) {
    		setTitle(name);
    		BasicPanel panel = new BasicPanel();
    		add(panel);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		
    	}
    	public void createGUI() {
    		setVisible(true);
    	}
    }
    
    class InputForm extends JFrame {
    	public InputForm() {
    		InputPanel panel = new InputPanel();
    		add(panel);
    		setSize(200,200);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(false);
    	}
    }
    
    class BasicPanel extends JPanel {
    	public BasicPanel() {
    		JButton button = new JButton("New...");
    		button.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent arg0) {
    				InputForm inputForm = new InputForm();
    				inputForm.setVisible(true);
    			}
    		});
    		add(button);
    	}
    }
    
    class InputPanel extends JPanel implements MouseListener {
    	JTextField firstNameField;
    	 JTextField secondNameField;
    	 JTextField phoneNumber;
    
    	public InputPanel() {
    		 firstNameField = new JTextField(20);
    		 secondNameField = new JTextField(20);
    		 phoneNumber = new JTextField(15);
    		 
    		 JButton saveToFileButton = new JButton("Save to file");
    		 
    		 saveToFileButton.addMouseListener(this);
    		 
    		 add(firstNameField);
    		 add(secondNameField);
    		 add(phoneNumber);
    		 add(saveToFileButton);
    	}
    	
    				@Override
    				public void mouseClicked(MouseEvent arg0) {
    					BufferedWriter writer;
    					try {
    						File file = new File("contacts.txt");
    						writer = new BufferedWriter(new FileWriter(file));
    						writer.write(firstNameField.getText() + "\t" + secondNameField + "\t" + phoneNumber);
    					} catch(FileNotFoundException e) {
    						e.printStackTrace();
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    				@Override
    				public void mouseEntered(MouseEvent arg0) {
    					// TODO Auto-generated method stub
    					
    				}
    				@Override
    				public void mouseExited(MouseEvent arg0) {
    					// TODO Auto-generated method stub
    					
    				}
    				@Override
    				public void mousePressed(MouseEvent arg0) {
    					// TODO Auto-generated method stub
    					
    				}
    				@Override
    				public void mouseReleased(MouseEvent arg0) {
    					// TODO Auto-generated method stub
    					
    				}
    		 
    }

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

    Default

    Some problems immediately leap to view:

    1) You really don't want to add a MouseListener to a JButton as you lose many of the benefits of the button's behavior (you can't disable a JButton and expect the mouselistener to be silent for instance). Also, when you use mouseClicked, it won't fire if you press the mouse down and move it slightly before releasing it. What happens if the user presses the spacebar while the JButton has focus causing the button to depress but causing no action to be fired. Use an ActionListener for the JButton's listener -- that's what they're for.

    2) You also appear to be creating some objects for no reason, for instance the InputForm object that you create in the main method -- why is that being created?

    3) I think that you're better off using a JDialog to show your second window. I wouldn't even subclass JDialog -- rather just use one and place your InputPanel into it.

    4) Your GUI classes shouldn't be implementing any of the action interfaces as that's asking the classes to do too much. Sure this may be OK in very basic example programs, but it's not good practice and a good habit to get out of as early as possible.
    Last edited by Fubarable; 05-07-2010 at 03:55 AM.

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Your advices to me were very helpful. Thank you.

    I have completed the program:
    Java Code:
    import java.awt.GridLayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.File;
    
    import javax.swing.JLabel;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class OpenningForm {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				BasicForm frame = new BasicForm("Basic Frame");
    				frame.createGUI();
    			}
    		});
    	}
    
    }
    
    class BasicForm extends JFrame {
    	public BasicForm(String name) {
    		setTitle(name);
    		BasicPanel panel = new BasicPanel();
    		add(panel);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		
    	}
    	public void createGUI() {
    		setVisible(true);
    	}
    }
    
    class BasicPanel extends JPanel {
    	public BasicPanel() {
    		JButton button = new JButton("New...");
    		button.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent arg0) {
    				InputDialog inputForm = new InputDialog();
    				inputForm.setVisible(true);
    			}
    		});
    		add(button);
    	}
    }
    
    class InputDialog extends JDialog {
    	public InputDialog() {
    		InputPanel panel = new InputPanel();
    		setTitle("Input Dialog");
    		add(panel);
    		setSize(200,150);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    		setVisible(false);
    	}
    }
    
    
    class InputPanel extends JPanel {
    	JTextField firstNameField;
    	 JTextField secondNameField;
    	 JTextField phoneNumber;
    	 
    	public InputPanel() {
    		GridLayout gridLayout = new GridLayout(4,2);
    		setLayout(gridLayout);
    		 firstNameField = new JTextField(20);
    		 secondNameField = new JTextField(20);
    		 phoneNumber = new JTextField(15);
    		 JLabel firstNameLabel = new JLabel("First name: ");
    		 JLabel secondNameLabel = new JLabel("Second name: ");
    		 JLabel phoneNumberLabel = new JLabel("Phone number: ");
    		 
    		 JButton saveToFileButton = new JButton("Save to file");
    		 
    		 saveToFileButton.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				BufferedWriter writer;
    				try {
    					//File file = new File("contacts.txt");
    					writer = new BufferedWriter(new FileWriter("contacts.txt",true));
    					writer.write(firstNameField.getText() + "\t" + secondNameField.getText() + "\t" + phoneNumber.getText());
    					writer.newLine();
    					writer.close();
    				} catch(FileNotFoundException ex) {
    					ex.printStackTrace();
    				} catch (IOException ex) {
    					ex.printStackTrace();
    				}
    				
    			}
    		});
    		 add(firstNameLabel);
    		 add(firstNameField);
    		 add(secondNameLabel);
    		 add(secondNameField);
    		 add(phoneNumberLabel);
    		 add(phoneNumber);
    		 add(saveToFileButton);
    	}					
    }
    Last edited by cselic; 05-07-2010 at 01:35 PM.

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2010, 09:41 AM
  2. Input data from file to arrays problem
    By PVL268 in forum New To Java
    Replies: 18
    Last Post: 03-16-2009, 06:05 AM
  3. enctype=multipart/form-data with form data in struts
    By vk_satheesh in forum New To Java
    Replies: 0
    Last Post: 09-19-2008, 01:48 PM
  4. Replies: 0
    Last Post: 04-04-2008, 10:36 AM
  5. how to upload a file along with html form data
    By pranith in forum Java Servlet
    Replies: 3
    Last Post: 07-30-2007, 03:33 AM

Posting Permissions

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