1

In my buildBar(....) method I have a Panel to which I add a JScrollPane(scroll_pane). Inside the JScollPane I add an array of JPanels, but no panels are being displayed

class MyFrame extends JFrame {

    private JPanel contentPane;
    JPanel panel;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
               } catch (Exception e) {
                    e.printStackTrace();
                }
            }
       });
    }

    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        panel = new JPanel();
        panel.setBounds(42, 41, 319, 161);
        contentPane.add(panel);
        panel.setLayout(null);

        buildBar(3,124,85,100,100);
    }

    JPanel[] myPanel;
    private void buildBar(int no_of_bars, int prev_x, int prev_y, int width,            int height){
        JScrollPane scroll_pane = new JScrollPane();
        scroll_pane.setBounds(124, 85, 2, 2);
        myPanel = new JPanel[no_of_bars];
        for(int i=0; i<no_of_bars; i++){
            myPanel[i] = new JPanel();
            myPanel[i].setBounds(prev_x,prev_y,width,height);
            myPanel[i].setBackground(new Color(255,0,0));
            prev_x = prev_x + width + 10;
            scroll_pane.add(myPanel[i]);
        }
        panel.setLayout(new BorderLayout());
        panel.add(scroll_pane,BorderLayout.CENTER);
        panel.revalidate();
        panel.repaint();
    }

}

When I run the above code, all i get is red colored lines on the bottom and right side edge of the panel

Jerry
  • 410
  • 6
  • 17
  • 1
    Please edit your question to include a [mcve], for [example](http://stackoverflow.com/a/7820703/230513). Instead of `setBounds()`, use layouts or a charting library like [tag:jfreechart]. – trashgod Mar 05 '16 at 11:20
  • @Jerry Sangma With limited codes, it is hard to troubleshoot what went wrong. There are numerous possibilities. – user3437460 Mar 05 '16 at 11:24
  • @user3437460 updated – Jerry Mar 05 '16 at 11:55
  • 1
    You should be adding your panels to a container ie another panel with the layout you want, and add that one panel to the scroll pane. AFAICT a scrollpane is designed to have only one "viewport". – TT. Mar 05 '16 at 13:40

0 Answers0