Help Wanted pls: JScrollPane dilemma : (

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]

Comments

  • : 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:
    [code]
    : 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);
    : }
    [/code]
    The while loop above keeps repeating, since the variable w isn't updated within it. You should use an if-statement instead.
  • Hi!
    Thanx u very much. The loop problem is gone. But now, there is another problem. I'm trying to display a button onto the scrollpane. When a user clicks on a radiobutton, the 'Testing' button should appear on the pane; but it doesn't. 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 panel1=new JPanel();
    JPanel panel= new JPanel();
    PaneDisplay pd;

    JScrollPane scrollpane;

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


    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(new JScrollPane(pd),BorderLayout.CENTER);


    try {


    allBrands.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent event) {
    if (allBrands.isSelected()) {

    pd=new PaneDisplay();

    }
    }
    }
    });

    allCategories.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent event) {
    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) {
    for (int i=0;i<rwit.getLength();i++){
    w=rwit.allBrandsandCategories();
    while (w != null){
    addtoPanel(w[i],panel2);
    }
    }
    }
    });

    }

    class PaneDisplay extends JPanel {
    JButton button = new JButton("Testing");

    PaneDisplay(){
    setLayout(new FlowLayout());
    add(button);

    }
    }




    }

    [end of BrowseViewTest]

  • : Hi!
    : Thanx u very much. The loop problem is gone. But now, there is another problem. I'm trying to display a button onto the scrollpane. When a user clicks on a radiobutton, the 'Testing' button should appear on the pane; but it doesn't. Pls help :) Here's the code:
    [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 panel1=new JPanel();
    : JPanel panel= new JPanel();
    : PaneDisplay pd;
    :
    : JScrollPane scrollpane;
    :
    : 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");
    :
    :
    : 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(new JScrollPane(pd),BorderLayout.CENTER);
    :
    :
    : try {
    :
    :
    : allBrands.addItemListener(new ItemListener() {
    : public void itemStateChanged(ItemEvent event) {
    : if (allBrands.isSelected()) {
    :
    : pd=new PaneDisplay();
    :
    : }
    : }
    : }
    : });
    :
    : allCategories.addItemListener(new ItemListener() {
    :
    : public void itemStateChanged(ItemEvent event) {
    : 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) {
    : for (int i=0;i<rwit.getLength();i++){
    : w=rwit.allBrandsandCategories();
    : while (w != null){
    : addtoPanel(w[i],panel2);
    : }
    : }
    : }
    : });
    :
    : }
    :
    : class PaneDisplay extends JPanel {
    : JButton button = new JButton("Testing");
    :
    : PaneDisplay(){
    : setLayout(new FlowLayout());
    : add(button);
    :
    : }
    : }
    :
    :
    :
    :
    : }
    :
    : [end of BrowseViewTest]
    [/code]
    :
    :
    Where do you add the PaneDisplay to the scrollpane? And next time please use the code formatting for your code. See Stylecodes below the message entry box. It makes the code much more readable
  • Hi, sorry.

    I've put a comment to show where I added PaneDisplay to the scrollpane.
    [code]

    : 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 panel1=new JPanel();
    : JPanel panel= new JPanel();
    : PaneDisplay pd;
    :
    : JScrollPane scrollpane;
    :
    : 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");
    :
    :
    : 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(new JScrollPane(pd),BorderLayout.CENTER);//PaneDisplay ADDED HERE
    :
    :
    : try {
    :
    :
    : allBrands.addItemListener(new ItemListener() {
    : public void itemStateChanged(ItemEvent event) {
    : if (allBrands.isSelected()) {
    :
    : pd=new PaneDisplay();
    :
    : }
    : }
    : }
    : });
    :
    : allCategories.addItemListener(new ItemListener() {
    :
    : public void itemStateChanged(ItemEvent event) {
    : 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) {
    : for (int i=0;i<rwit.getLength();i++){
    : w=rwit.allBrandsandCategories();
    : while (w != null){
    : addtoPanel(w[i],panel2);
    : }
    : }
    : }
    : });
    :
    : }
    :
    : class PaneDisplay extends JPanel {
    : JButton button = new JButton("Testing");
    :
    : PaneDisplay(){
    : setLayout(new FlowLayout());
    : add(button);
    :
    : }
    : }
    :
    :
    :
    :
    : }
  • : Hi, sorry.
    :
    : I've put a comment to show where I added PaneDisplay to the scrollpane.
    : [code]
    :
    : : 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 panel1=new JPanel();
    : : JPanel panel= new JPanel();
    : : PaneDisplay pd;
    : :
    : : JScrollPane scrollpane;
    : :
    : : 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");
    : :
    : :
    : : 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(new JScrollPane(pd),BorderLayout.CENTER);//PaneDisplay ADDED HERE
    : :
    : :
    : : try {
    : :
    : :
    : : allBrands.addItemListener(new ItemListener() {
    : : public void itemStateChanged(ItemEvent event) {
    : : if (allBrands.isSelected()) {
    : :
    : : pd=new PaneDisplay();
    : :
    : : }
    : : }
    : : }
    : : });
    : :
    : : allCategories.addItemListener(new ItemListener() {
    : :
    : : public void itemStateChanged(ItemEvent event) {
    : : 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) {
    : : for (int i=0;i<rwit.getLength();i++){
    : : w=rwit.allBrandsandCategories();
    : : while (w != null){
    : : addtoPanel(w[i],panel2);
    : : }
    : : }
    : : }
    : : });
    : :
    : : }
    : :
    : : class PaneDisplay extends JPanel {
    : : JButton button = new JButton("Testing");
    : :
    : : PaneDisplay(){
    : : setLayout(new FlowLayout());
    : : add(button);
    : :
    : : }
    : : }
    : :
    : :
    : :
    : :
    : : }
    :
    In other words, you add the DisplayPane (which is nil) at the creation of the frame, but create it in response to the users command. You should add it to the ScrollPane when you create it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories