Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By jashburn
  • 1 Post By jashburn

Thread: How Can I Sequence Through Components?

  1. #1
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default How Can I Sequence Through Components?

    I know my problem is my lack of understanding of OOP but I have searched for a solution and can't even seem to get close.

    This is a very simplified version of what I will need to do. I have a button and six textboxes. When the button is pressed I want to take the text from textbox6 and place it in textbox1, textbox2, textbox3, textbox4, and textbox5.

    I need sometthing better then this brut force method.

    ************************************************** *******
    Java Code:
    package textboxes;
    
    
    public class TextBoxesGUI extends javax.swing.JFrame {
    
        public String NewText;
    
        public TextBoxesGUI() {
            initComponents();
        }
    
                            
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // Get the contents of textBox6 and put it in textboxes 1 through 5.
    
            NewText=jTextField6.getText();
    
            jTextField1.setText(NewText);
            jTextField2.setText(NewText);   
            jTextField3.setText(NewText);
            jTextField4.setText(NewText); 
            jTextField5.setText(NewText);   
        }                                        
    
     
     
    
         
    }
    Last edited by petec; 03-29-2014 at 01:07 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: How Can I Sequence Through Components?

    Put references to the textboxes in an array and loop through that.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    Sorry, did I do the [code] thing right?

    Is there another way to create the array besides typing out the name of each element (textbox1, textbox2, textbox3, textbox4, and textbox5)? As I said this is a simplified example. I actually have a lot more then 5 textboxes. And a lot more other components that I need to sequence through.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: How Can I Sequence Through Components?

    Putting references in an array is a common way to do it.

    Another way to get references to components that have been added to a container is to use one of the container's methods that will return an array of all the components that were added to the container.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    I have been banging my head against that idea. I can't seem to figure out what the container is called. Examining the "Created code" looks like it might be called "layout" because that is where all the components are placed but that doesn't work. "Controls" doesn't work. I have searched and found numerous examples but when I copy the code I can't get them to work. And I can't understand the errors either. I ponder for 30 or 40 minutes trying things then move on to the next example that fails. It is very frustrating to find my question asked and answered and the answer given doesn't work for me and furthermore I can't figure out why.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: How Can I Sequence Through Components?

    The container is the class that components are added to. Containers extend tbe Container class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    It is all gibberish to me

  8. #8
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    Sorry. But my frustration is chewing me apart. If I could just see something work for once I think I could start to figure things out for myself.

    Does this container have a name?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: How Can I Sequence Through Components?

    A container is a class that extends the Container class. Read the API doc for the Container class to see what other classes extend it.

    When you created the GUI for your program you must have used one or more classes that extend the Container class.
    Last edited by Norm; 03-29-2014 at 02:50 AM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    Well, part of the problem may be that I created the GUI with Netbeans.
    But I just copied an example GUI (Hello World) to see how a GUI is manually created.

    I guess this is too simple because there is no "extend" or "Container" in the text.

    As for reading any more java docs. I'm dead there. I can't get anything out of them anymore. I have the JavaTutorial on my kindle and have been reading it for weeks. I also have it open on my desktop (along with 14 "search results" that don't seem to help).

    I have worked all the examples in the Tutorial. I guess I'm just too dense and need to go back to programming in assembly language.

  11. #11
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How Can I Sequence Through Components?

    The 2 ways to do this, quoting Norm:

    1. Get references to components that have been added to a container

    Here's a working example:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TextBoxes extends JFrame {
    
    	public TextBoxes() {
    		setTitle("Text Boxes");
    		setSize(300, 200);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    		JTextField textField1 = new JTextField("one");
    		textField1.setName("tf_one");
    		JTextField textField2 = new JTextField("two");
    		textField2.setName("tf_two");
    		JTextField textField3 = new JTextField("three");
    		textField3.setName("tf_three");
    
    		final JTextField textField6 = new JTextField("six");
    		textField6.setName("tf_six");
    
    		final JPanel textPanel = new JPanel(new GridLayout());
    		textPanel.add(textField1);
    		textPanel.add(textField2);
    		textPanel.add(textField3);
    		textPanel.add(textField6);
    
    		add(textPanel, BorderLayout.NORTH);
    
    		JButton button = new JButton("Press Me");
    		add(button, BorderLayout.SOUTH);
    
    		button.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String tf6Text = textField6.getText();
    
    				Component[] panelComps = textPanel.getComponents();
    				for (Component comp : panelComps) {
    					if (!"tf_six".equals(comp.getName())) {
    						((JTextField) comp).setText(tf6Text);
    					}
    				}
    			}
    		});
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    
    			@Override
    			public void run() {
    				TextBoxes textBoxes = new TextBoxes();
    				textBoxes.setVisible(true);
    			}
    		});
    	}
    }
    Note:
    • textField1.setName("tf_one") (and so on): Set the name as an identifier for the text fields.
    • All the text fields are added to the textPanel JPanel (has Container as an ancestor)
    • Component[] panelComps = textPanel.getComponents(): This is how you get all the components in a container.

    2. Putting references in an array

    I'm using an ArrayList in this case:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    public class TextBoxesList extends JFrame {
    
    	private List<JTextField> updatableTextFields = new ArrayList<>();
    
    	public TextBoxesList() {
    		setTitle("Text Boxes List");
    		setSize(300, 200);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    		// North text panel
    
    		JTextField textField1 = new JTextField("one");
    		updatableTextFields.add(textField1);
    
    		JTextField textField2 = new JTextField("two");
    		updatableTextFields.add(textField2);
    
    		final JTextField textField6 = new JTextField("six");
    
    		JPanel textPanelNorth = new JPanel(new GridLayout());
    		textPanelNorth.add(textField1);
    		textPanelNorth.add(textField2);
    		textPanelNorth.add(textField6);
    
    		add(textPanelNorth, BorderLayout.NORTH);
    
    		// Center text panel
    
    		JTextField textField3 = new JTextField("three");
    		updatableTextFields.add(textField3);
    
    		JTextField textField4 = new JTextField("four");
    		// We're not going to update textField4, so not adding it to updatableTextFields
    
    		JPanel textPanelCenter = new JPanel(new GridLayout());
    		textPanelCenter.add(textField3);
    		textPanelCenter.add(textField4);
    
    		add(textPanelCenter, BorderLayout.CENTER);
    
    		JButton button = new JButton("Press Me");
    		add(button, BorderLayout.SOUTH);
    
    		button.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				String tf6Text = textField6.getText();
    
    				for (JTextField textField : updatableTextFields) {
    					textField.setText(tf6Text);
    				}
    			}
    		});
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    
    			@Override
    			public void run() {
    				TextBoxesList textBoxes = new TextBoxesList();
    				textBoxes.setVisible(true);
    			}
    		});
    	}
    }
    Note:
    • Text fields are added to separate JPanels.
    • Text fields that will be updated by the button press are added to the updatableTextFields ArrayList.
    • textField4 is deliberately not added to updatableTextFields to show this method is more flexible; it doesn't rely on component names.
    • Updating text fields is just a simple matter of iterating through updatableTextFields.

    Creating GUIs with NetBeans without first having a good understanding of Swing is, imo, part of the problem. It'll be better to code by hand first, and for each GUI-related class, refer to the API docs (pay attention to the class hierarchy!). Use a GUI builder only when you have a good grasp of it. Heck, depending on who you talk to, some think GUI builders are evil. The generated code is usually hard to decipher (for all but the simplest GUIs), and when (not if) things go wrong, it's great fun trying to diagnose the problem. And if you post generated code into forums...

    (Btw, it's very late now where I am. Hopefully I've not made silly mistakes in my code above...)
    AlexGraal likes this.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: How Can I Sequence Through Components?

    the problem may be that I created the GUI with Netbeans.
    Yes, that will be the problem.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    petec is offline Member
    Join Date
    Feb 2014
    Location
    Near Buffalo NY
    Posts
    33
    Rep Power
    0

    Default Re: How Can I Sequence Through Components?

    The NetBeans GUI builder was a large part of the problem. I kept looking through the "Generated Code" from the GUI builder and couldn't find the container. I tried everything I could think of. JFrame, TextBoxesGUI, layout. Nothing worked.

    jashburn, tracing through your first example led me to create a Jpanel and put the components in it. Now I knew the container. I used the rest of your example or a version I got somewhere else and, SUCCESS! http://www.java-forums.org/images/smilies/skype/(y).gif

    I believe I will have to do the ArrayList version because as I said this was a simplified version of my problem. What I need to do (still simplified) is something like put the text in jTextField1 the first time the button is clicked, then put it in jTextField2 the second time, etc.

    I haven't been able to make "jTextField + i" work or any variation of that. So I think I will try to increment through the ArrayList each click.

    I will try your second method now.

    Just one more question, what is the construct to the left of the "." in

    ((JTextField) comp).setText(tf6Text)

    called? I don't remember reading about it. If it has a name or terminology, I would like to research how it works

    Thanks,

    Pete

  14. #14
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How Can I Sequence Through Components?

    Quote Originally Posted by petec View Post
    What I need to do (still simplified) is something like put the text in jTextField1 the first time the button is clicked, then put it in jTextField2 the second time, etc.

    I haven't been able to make "jTextField + i" work or any variation of that. So I think I will try to increment through the ArrayList each click.
    If there is a definite order in which the JTextFields are populated on each click, then perhaps you can place the JTextFields into the ArrayList in the prescribed order, use a counter to track the click number, and match the number to the index of the ArrayList to get a reference to the specific JTextField.

    Quote Originally Posted by petec View Post
    Just one more question, what is the construct to the left of the "." in

    ((JTextField) comp).setText(tf6Text)

    called?
    Short answer, 'cast'.

    The break this up for you,
    • comp is of type Component, defined in for (Component comp : panelComps)
    • panelComps is from Component[] panelComps = textPanel.getComponents()
    • only instances of JTextFields were earlier added to textPanel, so they are returned by the getComponents() call
    • note that the API for the getComponents() method specifies the Component array return type (Component is a supertype/class of JTextField - recall that I suggested paying attention to the class hierarchy in the API docs)
    • in order to be able to call the setText() method that is defined in JTextField, we need to first cast comp from Component to JTextField before making the call
    AlexGraal likes this.

Similar Threads

  1. Replies: 1
    Last Post: 01-23-2013, 06:06 PM
  2. Sum sequence
    By Aero in forum New To Java
    Replies: 2
    Last Post: 09-26-2011, 06:30 PM
  3. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  4. calling sequence
    By rocky in forum Web Frameworks
    Replies: 0
    Last Post: 04-27-2009, 08:35 PM
  5. Fetching sequence
    By Shivraj in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 02:30 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
  •