Results 1 to 6 of 6
- 05-06-2010, 10:08 PM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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.
-
- 05-06-2010, 11:05 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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.
- 05-07-2010, 12:34 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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 } }
-
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 02:55 AM.
- 05-07-2010, 12:28 PM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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 12:35 PM.
Similar Threads
-
file io operation to desire form of data dynamically
By balaiah in forum New To JavaReplies: 1Last Post: 03-17-2010, 08:41 AM -
Input data from file to arrays problem
By PVL268 in forum New To JavaReplies: 18Last Post: 03-16-2009, 05:05 AM -
enctype=multipart/form-data with form data in struts
By vk_satheesh in forum New To JavaReplies: 0Last Post: 09-19-2008, 12:48 PM -
how to search xml file data based on the given keyword from html form?
By nicemothi in forum XMLReplies: 0Last Post: 04-04-2008, 09:36 AM -
how to upload a file along with html form data
By pranith in forum Java ServletReplies: 3Last Post: 07-30-2007, 02:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks