Results 1 to 6 of 6
- 05-29-2011, 02:53 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Looping through file and filling textfield
Dear forum colleagues,
I am reading a textfile using FileInputStream, and putting koma delimited values in an array, temp. I want then to read each value of my array and fill the textfield of my swing GUI. I am using a controle where the program must check what for component is on the form, and if it is a textfield, we put the first value of the array in the first found textField, and so on. Until there are no more values in the array. I am getting an error message on the last line of my code. I am almost there and it is probably something small. The error says that they cannof find the symbo.
Can s.o. indicates to me what I am doing wrong?
Txs in advance
Java Code:public void actionPerformed(ActionEvent e){ Object source = e.getSource(); if (source == HaalInfo){ try{ //Open the file FileInputStream fstream = new FileInputStream("Test.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; String[] temp; String delimiter = ","; while ((strLine = br.readLine()) != null) { // JOptionPane.showMessageDialog(null ,strLine); temp = strLine.split(delimiter); for (Component c: pane.getComponents()) { if (c instanceof JTextField){ /* print substrings */ for(int i =0; i < temp.length ; i++) { // System.out.println(temp[i]); ((JTextField)c.setText((temp[i]))); //I get a compiling error hier. Cannot find symbol } } } }Last edited by Fubarable; 05-29-2011 at 03:13 PM. Reason: code tags added
-
Code tags added to original post to help allow the code to retain its formatting and be readable.
To the original poster: please post the entire error message, not your interpretation of it.
-
Also, this won't work:
Java Code:((JTextField)c.setText((temp[i])));
because you're calling setText on c, a Component variable. In order to call the method on the JTextField representation of it, you need to wrap the parenthesis around it, like so:
Java Code:((JTextField)c[b][color="red"])[/color][/b].setText(temp[i]);
You might want to ease off on your use of parenthesis and only use them where required, otherwise they clutter your code making it hard to read.
- 05-29-2011, 05:28 PM #4
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Thank you! I see where I went wrong. It compiles now.
I get however a second problem. Now the values copied in my textfields is allways 0. Must be in my array. I know it reads the file because i tested with a messagebox. May I ask you to review my code and give me an indication where it is going wrong?
My code in attachment.
Tks in advance
- 05-29-2011, 05:46 PM #5
Many members here, self included, won't follow external links. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.My code in attachment.
db
-
I have no idea what's wrong with your code since it is a bit large and we have no data file to test, but I would recommend that you organize it differently. Consider using a Map<String, JTextField> to hold some of your JTextFields and using the JLabel Strings as the key to the map. This way you can iterate through the map rather than your fragile iteration through a JPanel's components (what if you change the GUI later and nest the JTextFields in another JPanel?). For example:
Java Code:import java.awt.BorderLayout; import java.awt.event.*; import java.util.HashMap; import java.util.Map; import javax.swing.*; @SuppressWarnings("serial") public class LabelsAndFields extends JPanel { public static final String[] LABEL_TEXTS = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; private static final int FIELD_COLS = 10; private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>(); public LabelsAndFields() { setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); for (String labelText : LABEL_TEXTS) { JTextField textField = new JTextField(FIELD_COLS); textFieldMap.put(labelText, textField); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JLabel(labelText), BorderLayout.NORTH); panel.add(textField); int gap = 8; panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap)); add(panel); } JButton fillFields = new JButton("Fill Fields"); fillFields.addActionListener(new FillFieldsListener()); JPanel btnPanel = new JPanel(new BorderLayout()); btnPanel.add(fillFields, BorderLayout.SOUTH); add(btnPanel); } private class FillFieldsListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { for (String key : LABEL_TEXTS) { String fieldText = ""; if (key.equalsIgnoreCase("Saturday") || key.equalsIgnoreCase("Sunday")) { fieldText = "Weekend"; } else { fieldText = "Week Day"; } JTextField textField = textFieldMap.get(key); textField.setText(fieldText); } } } private static void createAndShowUI() { JFrame frame = new JFrame("LabelsAndFields"); frame.getContentPane().add(new LabelsAndFields()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Similar Threads
-
how to save output (from TextField) to a particular Folder as txt file
By doha786 in forum New To JavaReplies: 5Last Post: 08-03-2012, 10:47 AM -
Filling arrays from an input file?
By hiei_yasha in forum New To JavaReplies: 1Last Post: 02-26-2011, 05:01 AM -
Filling a JTable
By aborgeld in forum Advanced JavaReplies: 0Last Post: 01-08-2011, 01:37 PM -
How can I save the content of textfield in a text file?
By fred in forum Java AppletsReplies: 7Last Post: 08-17-2010, 06:00 PM -
textfield - printstream - text file - to much text
By keneid in forum New To JavaReplies: 2Last Post: 06-14-2010, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks