The Artima Developer Community
Sponsored Link

Java Answers Forum
File and Textfield Selection

2 replies on 1 page. Most recent reply: Jul 1, 2002 10:26 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
DAYO

Posts: 22
Nickname: done
Registered: Jun, 2002

File and Textfield Selection Posted: Jun 30, 2002 3:27 AM
Reply to this message Reply
Advertisement
A big thank you to Charles Bell.
I have got the following peice of code.
The textfile is something like:
1 asss
3 dddd
4 eee
10 fff
==
I have two things on each line, the first says which TextField and the second one just says what to put in it?
How do i write this to the textfield such that
Textfield1=asss
Textfield3=dddd
Textfield4=eee
Textfield10=fff
N.B:there are about 20 Textfields
CODE:
public void copy()
{try
{ FileReader file2 = new FileReader("d1.txt");
reading= new BufferedReader(file2);
//T1 is Textfield
for(int k=0;k<20;k++)
{ T1[k].setText(reading.readLine());
}
reading.close();
}
catch (FileNotFoundException e)
{ System.err.println("FileStreamsTest: " + e);
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: File and Textfield Selection Posted: Jun 30, 2002 9:16 PM
Reply to this message Reply
Why don't you iterate through your Collection of Components using getName() to find the matching (J)TextField, then set the text on it?

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: File and Textfield Selection Posted: Jul 1, 2002 10:26 AM
Reply to this message Reply
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);
}
}

Flat View: This topic has 2 replies on 1 page
Topic: java access Previous Topic   Next Topic Topic: dll and java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use