Hello!
I'm having trouble displaying contents onto a scrollpane. My program is supposed to display contents from a panel onto a scrollpane once a user selects a radiobutton. The problem is when a radiobutton is clicked, nothing gets displayed on the pane. I used System.out.println(...)between my code to see what's going on. I can see that an endless string of 'casio' is being printed(I'm trying 2 create a watch inventory). Pls help. Here's the code:
[start of BrowseViewTest]
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BrowseViewTest extends JPanel {
ReadWatchesInfoTest rwit = new ReadWatchesInfoTest();
JPanel panel= new JPanel();
JPanel panel2 = new JPanel();
JScrollPane scrollpane;
StringBuffer sb = new StringBuffer(20);
WatchesTest[] w;
double currency;
String dollars;
JRadioButton allBrands= new JRadioButton("All Brands");
JRadioButton allCategories=new JRadioButton("All Categories");
JRadioButton both=new JRadioButton("Both");
ButtonGroup group = new ButtonGroup();//"Browse Options"
public BrowseViewTest() {
setLayout(new BorderLayout());
panel.setLayout(new FlowLayout());
allBrands= new JRadioButton("All Brands");
allCategories=new JRadioButton("All Categories");
both=new JRadioButton("Both");
scrollpane = new JScrollPane(panel2);
both.setSelected(true);
panel.add(allBrands);
panel.add(allCategories);
panel.add(both);
group.add(allBrands);
group.add(allCategories);
group.add(both);
add(panel,BorderLayout.NORTH);
add(scrollpane,BorderLayout.CENTER);
try {
allBrands.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());
for (int i=0;i<2;i++){
w=rwit.allBrand();
while (w != null){
//System.out.println(w[i].getBrand());//prints infinite casio's
addtoPanel(w[i],panel2);
}
}
}
});
allCategories.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());
for (int i=0;i<rwit.getLength();i++){
w=rwit.allBrand();
while (w != null){
addtoPanel(w[i],panel2);
}
}
}
});
both.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
panel.setLayout(new BorderLayout());
for (int i=0;i<rwit.getLength();i++){
w=rwit.allBrandsandCategories();
while (w != null){
addtoPanel(w[i],panel2);
}
}
}
});
}catch(NullPointerException npe) {
System.out.println("null exception");
}
}
public void addtoPanel(WatchesTest w, JPanel panel) {
String[] tempfeatures;
JLabel feature=null;
System.out.println(w.getBrand());//prints infinite casio's
JLabel brand = new JLabel(w.getBrand());
JLabel category = new JLabel(w.getCategory());
for (int x=0; x< w.featureLength();x++) {
tempfeatures=w.getFeatures();
feature = new JLabel(tempfeatures[x]+", ");
}
if (w.getImagePath()==null) {
panel.add(new JButton(new ImageIcon("nographic.jpeg",BorderLayout.CENTER)));
}
else {
panel.add(new JButton(new ImageIcon (w.getImagePath(),BorderLayout.CENTER)));
panel.add(brand,BorderLayout.SOUTH);
panel.add(category,BorderLayout.SOUTH);
double currency = w.getPrice();
String dollars=sb.append("$").append(currency).toString();
JLabel dollar = new JLabel(dollars);
panel.add(dollar,BorderLayout.SOUTH);
panel.add(feature,BorderLayout.SOUTH);
}
}
}
[end of BrowseViewTest]