Results 1 to 6 of 6
  1. #1
    cgiguere is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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

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

    Default

    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.

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

    Default

    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.

  4. #4
    cgiguere is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    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
    Attached Files Attached Files

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    My code in attachment.
    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.

    db

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

    Default

    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

  1. Replies: 5
    Last Post: 08-03-2012, 10:47 AM
  2. Filling arrays from an input file?
    By hiei_yasha in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 05:01 AM
  3. Filling a JTable
    By aborgeld in forum Advanced Java
    Replies: 0
    Last Post: 01-08-2011, 01:37 PM
  4. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  5. Replies: 2
    Last Post: 06-14-2010, 10:18 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
  •