2013-12-13 55 views
0

我有一個問題,當我創建一個面板的滾動面板是對象我想爲每個面板添加一個按鈕,當我點擊它時,它將返回產品並將其放入一個變量中。問題在於它只是滾動窗格中最後一個連接到Action Listener的對象面板。這裏是滾動面板和個別面板的代碼:創建實際的面板ActionListener不適用於所有的對象

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(); 
    } 

類:

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"); 
+1

不要使用'JScrollPane的#add',這是而不是你如何設置滾動窗格的視圖。而是使用'JScrollPane#setViewportView'。查看[如何使用滾動窗格](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html)瞭解更多詳情。請記住,滾動窗格只能有一個視圖 – MadProgrammer

+0

所有面板都很好地顯示,這不是問題。我添加一個JButton到每個面板和一個監聽器到這個按鈕。問題是,它只適用於其中一個面板,而不是所有當你點擊它 –

+1

恥辱,你沒有提供的代碼,演示如何動作監聽器註冊或工作...(另外,它實際上是一個系列是小的意外,實際上讓滾動窗格工作) – MadProgrammer

回答

1

我只是不知道你怎麼想到這工作...

您創建了一系列的SearchDisplay S和他們(通過addPanel)添加到滾動窗格

while (result.next()) { 
     searchDisplay = new SearchDisplay (result);    
     // nb- Bad idea 
     scroll.add(searchDisplay); 
     addPanel.add(searchDisplay); 
    }  
    // nb- Worrisome... 
    scroll.setPreferredSize(new Dimension(425,390)); 
    scroll.setViewportView(addPanel); 

然後你似乎一個ActionListener添加到一些靜態對象

SearchDisplay.AddToCart.addActionListener(action); 

有什麼聯繫?這個AddToCart按鈕如何知道它應該添加什麼?你在過程中設置了一些其他的static變量?

我會IMAGIN說的SearchDisplay每個實例都將有自己的AddToCart,它會有它自己的ActionListener哪曉得該項目是與...相關

+0

我必須在創建searchdisplay對象後添加actionlistener,否則我會得到一個空指針異常 –

+0

@GarfieldMcCracker:如果您嘗試使用他的解決方案時遇到問題,那麼您將希望顯示您的代碼嘗試,顯示所有錯誤消息,並指出哪一行引發異常。 1+這個答案。 –

+0

@GarfieldMcCracker我看到它的問題是你正在添加一個「單個」動作監聽器,你想要應用到一個「組」可能的行動。問題是,「單一」動作偵聽器如何知道任何其他項目? – MadProgrammer