Q:

Adding text to multiple JTextAreas in different tabs of a JTabbedPane dynamically?

So I am dynamically adding JTextAreas to a JTabbedPane on the click of a JTable cell. I was wondering how to also set the text of the JTextArea dynamically. I was looking at trying to use getSelectedIndex() nested in getComponentAt(), but this returns a Component rather than a JTextArea so I will not be able to setText() this way. I'm wondering if instead I need to build an Array or ArrayList of new JTextAreas and add to the Array or ArrayList each time a cell is selected and then from the getSelectedIndex() and set the corresponding JTextAreas text. The necessary code is below:
table.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            int row = table.getSelectedRow();
            viewerPane.addTab((String) table.getValueAt(row, 0), null , new JPanel().add(new JTextArea()), (String) table.getValueAt(row, 0));
            viewerPane.setSelectedIndex(viewerPane.getComponentCount()-1);
        }
    }
});

A:

You can use a Map keyed with the tab index and valued with the JTextArea at that index:
Map<Integer, JTextArea> indexToTextArea = new HashMap<>();//Instance variable
....
//in the MouseListener:
JTextArea textArea = new JTextArea();
viewerPane.addTab((String) table.getValueAt(row, 0), null , new JPanel().add(textArea), (String) table.getValueAt(row, 0));
viewerPane.setSelectedIndex(viewerPane.getComponentCount()-1);
indexToTextArea.put(viewerPane.getComponentCount()-1, textArea);

When you want to get the JTextArea of the currently selected tab index for instance, just look up in the Map
JTextArea selectedTextArea =  indexToTextArea.get(viewer.getSelectedIndex());

