I am creating a JList like so:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
contentPanel.add(scrollPane, gbc_scrollPane);
{
JList listAvail = new JList();
listAvail.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
scrollPane.setViewportView(listAvail);
}
and I'm trying to access it when a button is clicked:
JButton btnAdd = new JButton("Add ->");
btnAdd.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
if (!listAvail.isSelectionEmpty())
{
int[] selects = listAvail.getSelectedIndices();
for (int i = selects.length - 1; i >= 0; i--)
{
appliedM.add(0, availM.getElementAt(selects[i]));
appliedFeatures.add(0, availFeatures.get(selects[i]));
availFeatures.remove(availFeatures.get(selects[i]));
availM.remove(selects[i]);
}
}
}
});
Of course, I'm getting an error saying "listAvail cannot be resolved" since it's out of the button's scope. Is there a getter method or a way to drill down to a specific component? I'm thinking something along the lines of:
contentPanel.scrollPane.listAvail
There must be something I'm missing but I haven't seen anything like this in the docs.
Edit: I am asking specifically about drilling down to a swing component. This is not a generic variable scoping question and not an exact duplicate of the referenced question. This question has yet to be answered at the time of this edit.