Note that there are some explanatory texts on larger screens.

plurals
  1. POActionListener not working with all of the objects
    text
    copied!<p>I have a problem that when I create a scrollpane with panels in them that are objects I want to add a button to every panel that when I click on it it will return the name of the product and put it in a variable. Problem is that it is only the last object panel in the scrollpane that gets connected to the Action Listener. Here is the code for the scrollpane and individual panels: </p> <pre><code>try{ System.out.println(sql); ResultSet rs = data.SQL.executeQuery(sql); String list = ""; int count=0; while (rs.next()){ count++; } ResultSet result = data.SQL.executeQuery(sql); ProductDisplayPanel.removeAll(); JPanel addPanel = new JPanel(); addPanel.setLayout(new GridLayout (count, 1)); JScrollPane scroll = new JScrollPane(); while (result.next()) { searchDisplay = new SearchDisplay (result); scroll.add(searchDisplay); addPanel.add(searchDisplay); } scroll.setPreferredSize(new Dimension(425,390)); scroll.setViewportView(addPanel); ProductDisplayPanel.add(scroll); ProductDisplayPanel.revalidate(); ProductDisplayPanel.repaint(); System.out.println(list); SearchDisplay.AddToCart.addActionListener(action); frame.add(SearchDisplay.AddToCart); } catch (Exception ae){ ae.printStackTrace(); } } catch (Exception e1) { e1.printStackTrace(); } </code></pre> <p>Class that creates the actual panels:</p> <pre><code>public SearchDisplay(ResultSet result) throws Exception{ setPreferredSize(new Dimension(500, 156)); setVisible(true); String link = result.getString("image"); System.out.println(link); BufferedImage icecream = ImageIO.read( new URL( "file:///"+link)); JLabel lblImage = new JLabel(new ImageIcon (icecream)); name = result.getString("Name"); JLabel lblName = new JLabel(name); String category = result.getString("Pieces"); JLabel lblFlavour = new JLabel("Pieces: "+category); String productID = result.getString("ProductID"); JLabel lblPrice = new JLabel("Product ID: " + productID); String price = result.getString("Price"); JLabel lblType = new JLabel("Price: "+ price +" kr"); String age= result.getString("Age"); JLabel lblBrand = new JLabel("Age: "+age); AddToCart = new JButton("Add to cart"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload