0

I cannot display the JList in the JScrollPane when it's inside the ActionListener. I have another list that prints without problems in another scroll pane, but not in action listener.

    btnSelecteazaBd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          String t = new String();
          t = list.getSelectedValue().toString();

          try {                                                    
                w = cautaTabele(t);
          } catch (SQLException ex) {
                Logger.getLogger(Conexiune.class.getName()).log(Level.SEVERE, null, ex);
          }                        
            listaTabele = new JList(w);    
            listaTabeleScrollPane = new javax.swing.JScrollPane(listaTabele);

        }
      });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amenna
  • 3
  • 1
  • Why not add the list to the scroll pane at start-up & simply add items to the model when needed? If that does not work for this scenario, use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson May 29 '16 at 17:20

1 Answers1

2
listaTabeleScrollPane = new javax.swing.JScrollPane(listaTabele);

You create a new JScrollPane, but you never add the scroll pane to the frame.

Don't create a new JScrollPane.

Instead you just update the viewport of the existing scrollPane:

//listaTabeleScrollPane = new javax.swing.JScrollPane(listaTabele);
listaTabeleScrollPane.setViewportView( listaTabele );

Or the other option is to just update the ListModel of the JList:

listaTabele.setModel( w );

So now there is no need to create a new JList or reset the viewport of the scrollpanel.

camickr
  • 321,443
  • 19
  • 166
  • 288