Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to update TextBox which was created with for loop

  1. #1
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Question How to update TextBox which was created with for loop

    Hello everyone :)

    I have created below mainPanelCls class, which is part of my UI. As a beginner I have to base on samples from google :) and one of that is this code which uses SpringUtilities to allocate Label with TextBox, but now I have a problem :( I have no idea how to write setter method to update Texboxs :(

    Can someone advice me how or where shall I start, an example would be nice if you don't mind

    Java Code:
    String[] labels = {"File Title: ", "File Path: "};
            int numPairs = labels.length;
    
            controlPanel = new JPanel(new SpringLayout());
    
            for (int i = 0; i < numPairs; i++) {
                JLabel l = new JLabel(labels[i], JLabel.TRAILING);
                controlPanel.add(l);
                JTextField textField = new JTextField(50);
                textField.setEnabled(false);
                l.setLabelFor(textField);
                controlPanel.add(textField);
            }
    
            SpringUtilities.makeCompactGrid(controlPanel,
                    numPairs, 2, //rows, cols
                    1, 1, //initX, initY
                    1, 1);       //xPad, yPad

    Java Code:
    package qa.test;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SpringLayout;
    
    /**
     *
     * @author pi4r0n
     */
    public class mainPanelCls {
    
        private JPanel mainPanel = new JPanel();
        private JPanel controlPanel = new JPanel();
        private JPanel chosePanel = new JPanel();
        private JButton choseButton = new JButton("...");
        JFileChooser fc = new JFileChooser();
    
        public mainPanelCls() {
            mainPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    
            String[] labels = {"File Title: ", "File Path: "};
            int numPairs = labels.length;
    
            controlPanel = new JPanel(new SpringLayout());
    
            for (int i = 0; i < numPairs; i++) {
                JLabel l = new JLabel(labels[i], JLabel.TRAILING);
                controlPanel.add(l);
                JTextField textField = new JTextField(50);
                textField.setEnabled(false);
                l.setLabelFor(textField);
                controlPanel.add(textField);
            }
    
            SpringUtilities.makeCompactGrid(controlPanel,
                    numPairs, 2, //rows, cols
                    1, 1, //initX, initY
                    1, 1);       //xPad, yPad
    
            chosePanel.add(choseButton);
            choseButton.addActionListener(new ListenButtonClicked());
    
            mainPanel.add(controlPanel, BorderLayout.WEST);
            mainPanel.add(chosePanel, BorderLayout.CENTER);
        }
    
        /**
         * This getter method will return JPanel object
         * @return 
         */
        public JPanel getPanel() {
            return this.mainPanel;
        }
    
        /**
         * This is action listener for Quit menu options
         */
        public class ListenButtonClicked implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                //Handle open button action.
                if (e.getSource() == choseButton) {
                    int returnVal = fc.showOpenDialog(choseButton);
    
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File f1 = fc.getSelectedFile();
                        System.out.println(f1.getName());
                        System.out.println(f1.getParent());
                    } else {
                    }
                }
            }
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to update TextBox which was created with for loop

    Your problem is that you are not retaining a reference to the JTextfield outside the loop. You could get an array of components from you JPanel, iterate over it until you find your JTextfield and then update it.

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

    Default Re: How to update TextBox which was created with for loop

    Question: what is a "textbox" as there is no such animal in the Swing library.
    camickr likes this.

  4. #4
    pi4r0n is offline Member
    Join Date
    Jan 2011
    Posts
    19
    Rep Power
    0

    Default Re: How to update TextBox which was created with for loop

    Hello ALL

    Junky thank you very much for your help ;) I have managed to find a simple way for now, which is to use getComponent(int)

    Java Code:
    JTextField textTitleField = (JTextField) controlPanel.getComponent(1);
    I know its not the best option, but it will do for now. I will definitely get it done the way Junky suggested soon ;)

    Cheers guys ;)

Similar Threads

  1. How I can draw on textbox
    By iraqy2010 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-19-2011, 05:48 PM
  2. Update JLabel during loop automatically
    By carnado2008 in forum AWT / Swing
    Replies: 4
    Last Post: 01-22-2011, 02:55 PM
  3. How to set value to a TextBox
    By kishan in forum JavaServer Faces (JSF)
    Replies: 3
    Last Post: 06-29-2010, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •