Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: File and Textfield Selection
|
Posted: Jul 1, 2002 10:26 AM
|
|
The following will work as long as you data file has a load value for every textfield.
If you need something else, you will have to adapt to the program requirements you have.
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*;
import javax.swing.*;
public class JTextFieldTest extends JFrame implements ActionListener{ private JTextField[] textFields; private String fileName = "UserData.txt"; public JTextFieldTest(){ super("JTextFieldTest"); init(); } public static void main(String[] args){ new JTextFieldTest(); } public void init(){
load(); JPanel textFieldPanel = new JPanel(); for (int i = 0; i < textFields.length; i++){ textFieldPanel.add(textFields[i]); }
getContentPane().add(textFieldPanel,BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); JButton saveButton = new JButton("Save"); saveButton.addActionListener(this); buttonPanel.add(saveButton);
JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton);
JButton exitButton = new JButton("Exit"); exitButton.addActionListener(this); buttonPanel.add(exitButton);
getContentPane().add(buttonPanel,BorderLayout.CENTER); pack(); setLocationRelativeTo(null); show(); } public void actionPerformed(ActionEvent ae){ String actionCommand = ae.getActionCommand(); if (actionCommand.compareTo("Save") == 0) { save(); }else if (actionCommand.compareTo("Clear") == 0) { clear(); }else if (actionCommand.compareTo("Exit") == 0) { exit(); } } public void save(){ try{ FileWriter fw = new FileWriter(fileName); for (int i = 0; i < textFields.length; i++){ fw.write(String.valueOf(i) + " " + textFields[i].getText()); } fw.close(); }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } public void load(){ try{ Vector strings = new Vector(); FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); String lineRead = ""; while ((lineRead = br.readLine()) != null){ strings.add(lineRead); } fr.close(); textFields = new JTextField[strings.size()]; for (int i = 0; i < strings.size(); i++){ String nextLine = (String) strings.elementAt(i); String[] args = nextLine.split(" "); int index = toInt(args[0]); String value = args[1]; textFields[index-1] = new JTextField(value); } }catch(FileNotFoundException fnfe){ System.err.println("FileNotFoundException: " + fnfe.getMessage()); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } } public void clear(){ for (int i = 0; i < textFields.length; i++){ textFields[i].setText(""); } } private int toInt(String s){ int n = -1; try{ n = Integer.parseInt(s); }catch(NumberFormatException nfe){ n = -1; System.err.println("NumberFormatException: " + nfe.getMessage()); } return n; } public void exit(){ System.exit(0); } }
|
|