0

I have 2 lists displayed vertically in a JFrame. Lets say list A is an ArrayList<CustomTexField> and list B is an ArrayList<JLabel>.

I want to "update" the elements of list B that matches the same index of elements inside list A with the value from the CustomTextField.

I've tried adding document listener, but don't know how to calculate the index.

@Override
    public void insertUpdate(DocumentEvent e) {
        try {

            listB().get(INDEX).setText(e.getDocument().getText(0, e.getDocument().getLength()) + "");

        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }

    }

I have also created a method inside CustomTexField class that saves the index when its created but don't know how to 'read' it from e.getDocument()

EDIT: UPDATED TITLE

Fran
  • 521
  • 4
  • 20
  • 1
    If you're just asking about how to get the index of an item in an ArrayList, you can use `arrayList.indexOf(itemYouWant)` – Jeremy Fisher Aug 04 '15 at 12:55
  • So you want to get the element from list B that corresponds the element being inserted/updated? – Thomas Aug 04 '15 at 12:58
  • -Yes @Thomas. `indexOf` is not working, because i dont know what `itemIWant`. Depends on which txtfield is edited – Fran Aug 04 '15 at 13:02
  • I can't do `listB().inexOf(e.getDocument());` . I want to do something similar, and casting from document gives me error :( – Fran Aug 04 '15 at 13:07
  • Can you show full code that adds the document listener ? – Florent Aug 04 '15 at 13:11
  • 1
    I guess you're adding the listener to each textfield's document anyways so you could just provide that listener with a reference to the corresponding label and update it accordingly. Maintaining 2 separate lists and connecting via index isn't that robust anyways (what if only one list is changed to some programming error etc.?). – Thomas Aug 04 '15 at 13:16

3 Answers3

1

If you're just trying to get the index of an item in an arraylist, you can just use the indexOf method.

int indexOfItem = arrayList.indexOf(itemIWant)

This is just how I interpreted your question but I would love clarification.

EDIT: If you're trying to get the object attached to the DocumentListener, you can check out this question: how to find source component that generated a DocumentEvent

Basically, if you have a DocumentListener for each CustomTextField, you can use the putProperty method described in the link to attach itself to it. From there, you can use getProperty(item) to find the item. You can do something similar with the index if you want but I believe that since you have an index field in your definition of CustomTextField, just attaching the CustomTextField with the DocumentListener will be enough.

//sometime on initalization of the lists
for(CustomTextField field: listA):
    field.getDocument().putProperty("owner", field);

...

@Override
public void insertUpdate(DocumentEvent e) {
    try {
        CustomTextField field = e.getDocument().getProperty("owner");
        int index = field.getIndex(); //assuming you have a getter method
        listB().get(index).setText(listA.get(index).getText());

    } catch (BadLocationException e1) {
        e1.printStackTrace();
    }

}
Community
  • 1
  • 1
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59
  • Yes, but how to calculate "INDEX", I dont want to loop through all elements in the list everytime I write something inside a textfield. – Fran Aug 04 '15 at 13:05
  • 1
    So you're asking about how to get the index of the item that corresponds with the document listener, so you can update it's corresponding JLabel? – Jeremy Fisher Aug 04 '15 at 13:11
  • Thats exactly what i want @Jeremy Fisher – Fran Aug 04 '15 at 13:19
0

I would store all of your CustomTextFields in a Map<String, Integer> where the key is the name of the CustomTextField and the value is a unique identifier. Then I would have all of your Labels in another Map<Integer, Label> where the key is a unique identifier that corresponds to the unique identifier that matches a CustomTextField.

Now in your insertUpdate() you know which CustomTextField that's getting updated, so get it's unique identifier like:

int id = ctfMap.get(customTextField.getName());

Take this id and get your Label like this:

JLabel label = lblMap.get(id);

Set the label's text:

label.setText = customTextField.getText();
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

I found an answer here . I also added focusListener to the CustomTextFields so when overriding focusGained method I can get the Object where the event where called from and cast it to my custom class.

private class ListenForDoc implements DocumentListener, FocusListener{
    int index;
    @Override
    public void insertUpdate(DocumentEvent e) {
        try {

            listB().get(index).setText(e.getDocument().getText(0, e.getDocument().getLength()));


        } catch (BadLocationException e1) {
            e1.printStackTrace();
        }

    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        //TO DO

    }

    @Override
    public void changedUpdate(DocumentEvent e) {}


    @Override
    public void focusGained(FocusEvent e) {
        Object o = e.getSource();
        if(o instanceof CustomTextField) {
            index = ((CustomTextField)o).getIndex();    
        }
        else{
        //HandleError
        }
    }

    @Override
    public void focusLost(FocusEvent e) {
        // TODO Auto-generated method stub      
    }

}
Community
  • 1
  • 1
Fran
  • 521
  • 4
  • 20