Hi,
This is my first post,
I usually find solution by searching this forums
However, I been working on my problem for 6 hours with no solution.

Here what I'm trying to do

I have a class that extends as JPanle
also it implements ActionListener

I'm trying to add JScrollPane to it,
here is the code

public class Shelf extends JPanel implements ActionListener {

    Driver dv = new Driver(); //this is the main frame
    private JPanel p;
    private JScrollPane sc;

    public Shelf() {
    //I'm sure the code should go here
    //I tried everything, first
    //s=new JScrollPane(thi);
    //and also tried do the SetPerf... to diminoson..
    //but no results

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        dv.getFrame().getContentPane().removeAll();
        dv.getFrame().getContentPane().add(this.p);
        this.removeAll();
        ArrayList<Mybook> books = new ArrayList<Mybook>();
        books = Library.getMydb().getAllBooks();
        for (Mybook bk : books) {
            JButton text = new JButton(bk.getTitle());
            this.p.add(text);
        }
        dv.getFrame().validate();
        dv.getFrame().repaint();
    }

}

Recommended Answers

All 4 Replies

Have you tried the add() method of the JPanel?

example:
In the default constructor you may put something like this:

sc = new JScrollPane();
//define some properties of the JScrollPane

//now add the JScrollPane sc to the Shelf class
add(sc)

if you want to add scroll pane into panel, then you have to use add method with the panel object.

So from your given code if you want to add scroll pane,use p.add(sc) and display all your data on this scroll pane.
So at line 25 this.p.add, write instead sc.add and then p.add(sc).

I think this might be work.

tried before,
but could you tell me what properties should I write?

sc.setPreferredSize(new Dimension(500,500));

and I also, did other things before
is is about the layout?

thanx for you reply :)

you could set properties like bounds, font, foreground color etc. After setting those propeties, try using the add() method of the JPanel to where you want to add your JScrollPane.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.