Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I have a Jframe that includes a Jspinner. When the JSpinner is increased a new JTextField is created in my panel headerPanel. The script creates the textboxes with an integer attached to their variable name (tFrame0, tFrame1, and so on).
- spinner = new JSpinner();
- spinner.addContainerListener(new ContainerAdapter() {
- @Override
- public void componentAdded(ContainerEvent arg0) {
- boxesChanged();
- }
- });
- spinner.addChangeListener(new ChangeListener() {
- public void stateChanged(ChangeEvent arg0) {
- int spinnerValue = (Integer) spinner.getValue();
- if (spinnerValue == headerPanel.getComponentCount()) {
- System.out.println("Error, spinner shouldn't change to same value");
- }
- if (spinnerValue < headerPanel.getComponentCount()) {
- headerPanel.removeAll();
- }
- while (spinnerValue > headerPanel.getComponentCount()) {
- boxesChanged();
- // frame.getContentPane().add(tField);
- frame.revalidate(); // For JDK 1.7 or above.
- // frame.getContentPane().revalidate(); // For JDK 1.6 or below.
- frame.repaint();
- }
- }
- });
- Now I'm trying to loop through my code based on the number of components in my headerPanel but I don't know how to append my for loop int i to a variable name.
- Variables
- Public JTextField() tField;
- Public String nameTField = "tFrame";
- Public Object[] inputData;
- Creating the text boxes
- public void boxesChanged() {
- tField = new JTextField();
- tField.setName(nameTField + (headerPanel.getComponentCount()));
- headerPanel.add(tField);
- }
- Loop attempt (Located in a separate class with access to the GUI)
- int hp = headerPanel.getComponentCount();
- inputData = new Object[hp];
- for (int i = 0; i < hp; i++) {
- tField = new JTextField();
- tField.setName(nameTField + i);
- inputData[i] = tField.getText();
- }
- EDIT
- I was able to make some headway with this https://stackoverflow.com/questions/24642059/for-loop-output-in-jframe?fbclid=IwAR1piMmk8EjygobXdxkyxfM9idA5pCh4WJrpBx9QINbsMcmc5MvaUZ3gb0I. When creating the textboxes, I can assign a Document Listener to them and then they are able to pass me information. I'm still having some issues with how I can assign each input from these to my for loop however.
- public void boxesChanged() {
- tField = new JTextField();
- tField.setName(nameTField + (headerPanel.getComponentCount()));
- tField.getDocument().addDocumentListener(new DocumentListener(){
- public void changedUpdate(DocumentEvent e) {
- warn();
- }
- public void removeUpdate(DocumentEvent e) {
- warn();
- }
- public void insertUpdate(DocumentEvent e) {
- warn();
- }
- public void warn() {
- if(tField.getText()!=null) {
- System.out.println(tField.getText());
- }
- }
- });
- headerPanel.add(tField);
- }
- EDIT 2
- I've managed to make a change that adds each textfields information to my inputData Object[], but only if I add data sequentially (tField0 must be filled out, then tField1, etc).
- For some reason each time I add a new JTextField it changes the document listener to that field only, even though I create the document listener in each new JTextField.
- public void boxesChanged() {
- int hp = headerPanel.getComponentCount();
- tField = new JTextField();
- textFieldList.add(tField);
- tField.setName(nameTField + hp);
- tField.getDocument().addDocumentListener(new DocumentListener() {
- public void changedUpdate(DocumentEvent e) {
- warn();
- }
- public void removeUpdate(DocumentEvent e) {
- warn();
- }
- public void insertUpdate(DocumentEvent e) {
- warn();
- }
- public void warn() {
- int digits = 1;
- if (hp > 9) {
- digits = 2;
- }
- String getFieldString;
- int getFieldNum;
- getFieldString = tField.getName().substring(tField.getName().length() - digits);
- getFieldNum = Integer.parseInt(getFieldString);
- if (tField.getText() != null) {
- Object[] currentData = new Object[getFieldNum + 1];
- currentData[getFieldNum] = tField.getText();
- inputData[getFieldNum] = tField.getText();
- for (int i = 0; i < inputData.length; i++) {
- if (inputData[i] != null) {
- System.out.println("for i = " + i + ": " + inputData[i]);
- }
- }
- }
- }
- });
- With this setup if I set the default JTextField to "Box 1" my console output will look like this.
- for i = 0: B
- for i = 0: Bo
- for i = 0: Box
- for i = 0: Box
- for i = 0: Box 1
- If I create the second JTextField and then type in "Box 2" in that field it will output like this
- //Same as above
- for i = 0: Box 1
- for i = 1: B
- for i = 0: Box 1
- for i = 1: Bo
- for i = 0: Box 1
- for i = 1: Box
- for i = 0: Box 1
- for i = 1: Box
- for i = 0: Box 1
- for i = 1: Box 2
- However say I decide to change the boxes to "Box 0" and "Box 1" my console after making both of these changes will output this.
- //Same as above
- for i = 0: Box 1 //Deleted "1" from box 1
- for i = 1: Box 2 //Deleted "1" from box 1
- for i = 0: Box 1 //Added "0" to former box 1
- for i = 1: Box 2 //Added "0" to former box 1
- for i = 0: Box 1 //Deleted 2 from box 2
- for i = 1: Box //Deleted 2 from box 2
- for i = 0: Box 1 //Added 1 to former box 2
- for i = 1: Box 1 //Added 1 to former box 2
- If I were to keep going and add a 3rd JTextField etc. only the last created JTextField will show a change in output on the console.
- EDIT 3
- I have managed to pass the code along a little further now. Now I can store all of my data properly within an ArrayList that will hold my data, but only if I enter my data sequentially.
- Variables
- Public JTextField() tField; //textField element is public and created in boxesChanged()
- Public String nameTField = "tFrame"; //Name of TextField before appending variable in spinner
- Public Object[] headerData; //inputData changed to headerData in Edit 3.
- private ArrayList<JTextField> textFieldList = new ArrayList<JTextField>(); //ArrayList that holds JTextFields
- private JPanel headerPanel; //Panel that holds JTextFields
- My Spinner
- spinner = new JSpinner(); //New spinner
- spinner.addContainerListener(new ContainerAdapter() {
- @Override
- public void componentAdded(ContainerEvent arg0) { //On creation Spinner value is 1, so runs boxesChanged() to set the first JTextFrame
- boxesChanged();
- }
- });
- spinner.addChangeListener(new ChangeListener() {
- public void stateChanged(ChangeEvent arg0) { //Each time the spinner changes
- int spinnerValue = (Integer) spinner.getValue();
- if (spinnerValue == headerPanel.getComponentCount()) {
- System.out.println("Error, spinner shouldn't change to same value"); //Was reaching this during some debuging earlier, but not recently
- }
- if (spinnerValue < headerPanel.getComponentCount()) {
- headerPanel.removeAll(); //removes all existing JTextFields
- }
- while (spinnerValue > headerPanel.getComponentCount()) { //Populates headerPanel with JTextFields until spinnerValue == headerPanel.getComponentCount
- boxesChanged();
- frame.revalidate();
- frame.repaint();
- }
- }
- });
- My boxesChanged() method
- public void boxesChanged() { //Runs whenever spinner value != number of JTextPanels in headerPanel
- int hp = headerPanel.getComponentCount();
- tField = new JTextField(); //Create JTextField
- textFieldList.add(tField); //adds the current JTextField to ArrayList<JTextField> textFieldList
- tField.setName(nameTField + hp); //sets name of current JTextField to String nameTField + current JTextPanelCount (tFrame0, tFrame1, ...)
- tField.getDocument().addDocumentListener(new DocumentListener() { //Adds a listener to each instance of JTextField
- //If any changes to JTextField, runs warn()
- public void changedUpdate(DocumentEvent e) {
- warn();
- }
- public void removeUpdate(DocumentEvent e) {
- warn();
- }
- public void insertUpdate(DocumentEvent e) {
- warn();
- }
- public void warn() {
- int digits = 1; //sets the number of digits to subtract from substring
- if (hp > 9) { //if >9 uses 2 didgits, Spinner max is 100, starting at 0, so 99 is max
- digits = 2;
- }
- String getFieldString; //String used to hold substring
- int getFieldNum; //int for parseInt of getFieldString
- getFieldString = tField.getName().substring(tField.getName().length() - digits); //Gets the last digit(s) from tField.getName
- getFieldNum = Integer.parseInt(getFieldString); //Parses substring into int
- headerData = new Object[hp + 1]; //Create headerData with size of number of JTextField +1 (if 0, will instantiate a null array)
- if (tField.getText() != null) { //null check
- headerData[getFieldNum] = tField.getText(); //sets the headerData value to current JTextField's text
- for (int i = 0; i < textFieldList.size(); i++) { //Loop through to see if each text field list contains correct data
- System.out.println("TFL i :" + i + " " + textFieldList.get(i).getText());
- }
- }
- }
- });
- headerPanel.add(tField); //Accidentally left this part out of the previous edit, hope it wasn't what was causing my issue
- }
- With this setup if I set the default JTextField to "Box 1" my console output will look like this.
- TFL i :0 B
- TFL i :0 Bo
- TFL i :0 Box
- TFL i :0 Box
- TFL i :0 Box 1
- TFL i :0 Box 1
- TFL i :1 B
- TFL i :0 Box 1
- TFL i :1 Bo
- TFL i :0 Box 1
- TFL i :1 Box
- TFL i :0 Box 1
- TFL i :1 Box
- TFL i :0 Box 1
- TFL i :1 Box 2
- However, this is only if I add data sequentially (tField0 must be filled out before I increase the spinner, then tField1 must be filled out, increase the spinner, etc.).
- Otherwise I get an ArrayIndexOutOfBoundsException at
- headerData[getFieldNum] = tField.getText();
- Edit 5
- Variables
- private static String nameTField = "tField";
- private Object[] headerData;
- public String name;
- public List<JTextField> findTextFields() {
- List<JTextField> fields = new LinkedList<>();
- Component[] children = headerPanel.getComponents();
- // this is your 'search-loop':
- for (Component child : children) {
- if (child instanceof JTextField) {
- JTextField childField = (JTextField) child;
- name = childField.getName();
- if (name.startsWith(nameTField)) {
- fields.add(childField);
- }
- }
- }
- return fields;
- }
- public static final Pattern POSTFIX_PATTERN = Pattern.compile(nameTField + "(\\d+)");
- JSpinner
- spinner = new JSpinner();
- spinner.addContainerListener(new ContainerAdapter() {
- @Override
- public void componentAdded(ContainerEvent arg0) {
- boxesRemoved();
- boxesChanged();
- }
- });
- spinner.addChangeListener(new ChangeListener() {
- public void stateChanged(ChangeEvent arg0) {
- int spinnerValue = (Integer) spinner.getValue();
- if (spinnerValue == headerPanel.getComponentCount()) {
- System.out.println("Error, spinner shouldn't change to same value");
- }
- if (spinnerValue < headerPanel.getComponentCount()) {
- boxesRemoved();
- frame.revalidate();
- frame.repaint();
- }
- while (spinnerValue > headerPanel.getComponentCount()) {
- boxesChanged();
- // frame.getContentPane().add(tField);
- frame.revalidate(); // For JDK 1.7 or above.
- // frame.getContentPane().revalidate(); // For JDK 1.6 or below.
- frame.repaint();
- }
- }
- });
- boxesRemoved() method
- public void boxesRemoved() {
- for (JTextField textField : findTextFields()) {
- String name = textField.getName();
- Matcher matcher = POSTFIX_PATTERN.matcher(name);
- if (matcher.matches()) {
- String strPostfix = matcher.group(1);
- int postFixNumeric = Integer.parseInt(strPostfix);
- System.out.println("for postFix = " + postFixNumeric + ": " + textField.getText());
- if (postFixNumeric < findTextFields().size()) {
- System.out.println("PFN: " + postFixNumeric);
- System.out.println("FTF: " + findTextFields().size());
- findTextFields().remove(findTextFields().size() - 1);
- headerPanel.remove(findTextFields().size() - 1);
- }
- }
- }
- }
- boxesChanged() method
- public void boxesChanged() {
- int hp = headerPanel.getComponentCount();
- tField = new JTextField();
- tField.setName(nameTField + hp);
- textFieldList.add(tField);
- tField.getDocument().addDocumentListener(new DocumentListener() {
- public void changedUpdate(DocumentEvent e) {
- warn();
- }
- public void removeUpdate(DocumentEvent e) {
- warn();
- }
- public void insertUpdate(DocumentEvent e) {
- warn();
- }
- public void warn() {
- int digits = 1;
- if (hp > 9) {
- digits = 2;
- }
- String getFieldString;
- int getFieldNum;
- getFieldString = tField.getName().substring(tField.getName().length() - digits);
- getFieldNum = Integer.parseInt(getFieldString);
- headerData = new Object[hp + 1];
- for (JTextField textField : findTextFields()) {
- Matcher matcher = POSTFIX_PATTERN.matcher(name);
- System.out.println(name);
- if (matcher.matches()) {
- String strPostfix = matcher.group(1);
- int postfixNumeric = Integer.parseInt(strPostfix);
- System.out.println("for postfix = " + postfixNumeric + ": " + textField.getText());
- } else {
- System.out.println("matcher error: " + matcher);
- }
- }
- }
- });
- headerPanel.add(tField);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement