-1

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.

Z. Reticulan
  • 145
  • 4
  • 11

1 Answers1

2

Your question boils down to -- how do I access an object reference when it is assigned to a very locally scoped variable, here the object being the JList, and the variable it's being assigned to is listAvail, a variable buried within its own set of curly braces (why?) within a method or constructor.

The solution -- get the variable declaration out of the local scope and make it instead an instance field of the class. e.g.,

public class MyClass {
    private JList listAvail = new JList(); // should be generic btw

Another issue is here:

btnAdd.addMouseListener(new MouseAdapter() {....

Don't use a MouseListener with a JButton where an ActionListener is much more appropriate. This has ramifications if you want the button's actions to be disabled when the button itself is disabled.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Answered as a community wiki until I could find a dupe – Hovercraft Full Of Eels Jun 07 '17 at 02:28
  • First, to answer your question about the curly braces: They were put there automatically by WindowBuilder and I'm afraid to delete something which might be important. – Z. Reticulan Jun 07 '17 at 02:30
  • @Chris: Don't use a window builder until you understand the language and the library. Seriously, else you will end up painting yourself into corners too often. – Hovercraft Full Of Eels Jun 07 '17 at 02:31
  • Your solution presented here was my initial idea. I made the variable an instance variable, but that caused all the UI components to move around the dialog and change positions on subsequent openings. – Z. Reticulan Jun 07 '17 at 02:34
  • @Chris: read the tutorials, learn the Java language and the Swing library is about all I can tell you at this point. .... Or move to JavaFX, a more modern Java GUI library. – Hovercraft Full Of Eels Jun 07 '17 at 02:35
  • Do you know if there is a way to drill down to a specific swing component? My question was marked as a duplicate so I doubt I will get an answer from anyone else. – Z. Reticulan Jun 07 '17 at 02:41
  • Yes, and the best answer -- don't do it it's ludicrous to try to base your model on the structure of the view as the GUI structure is brittle and likely to change. Really, don't go this route. – Hovercraft Full Of Eels Jun 07 '17 at 02:46
  • I don't understand. Respectfully, I'd rather leave personal opinions aside on a question and answer site. I'm simply asking for help on how to drill down to a nested swing component because I was unable to find it in the Java documentation. – Z. Reticulan Jun 07 '17 at 02:57
  • @Chris: Your documentation is here: [Component API](http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html) and [Container API](http://docs.oracle.com/javase/8/docs/api/java/awt/Container.html). You would have to search containers for their components using `getComponents()`, check to see which components *are* containers, and then recursively search -- a holy mess. – Hovercraft Full Of Eels Jun 07 '17 at 03:04