Use the method JScrollPane.setViewportView for adding the panel. Thus, instead of:
paneScrollPane.add(panGraphics);
use:
paneScrollPane.setViewportView(panGraphics);
Question
Why JPanel
panGraphics
is not displayed in JScrollPane
? However, if I put panGraphics directly on the JTabbedPane
, it works: tabbedPaneData.addTab("Chart View", panGraphics);
JTabbedPane tabbedPaneData = new JTabbedPane();
JScrollPane paneScrollPane = new JScrollPane();
JPanel panGraphics = createGraphics();
paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
paneScrollPane.setPreferredSize(new Dimension(550, 320));
paneScrollPane.setMinimumSize(new Dimension(10, 10));
paneScrollPane.add(panGraphics);
tabbedPaneData.addTab("Chart View", paneScrollPane);
tabbedPaneData.addTab("Table View", new JPanel());
Solution
Use the method JScrollPane.setViewportView for adding the panel. Thus, instead of:
paneScrollPane.add(panGraphics);
use:
paneScrollPane.setViewportView(panGraphics);
OTHER TIPS
Here is My collection sample code:
-----------------------------------
package testing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestScroller extends JPanel
{
JPanel panel;
JScrollPane scrollPane;
public TestScroller() {
// TODO Auto-generated constructor stub
//this is in the constructor of the mother object, JPanel
panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
//panel.setBounds(0,0,415,420);
panel.setPreferredSize(new Dimension(415,420));
panel.setBackground(Color.BLACK);
scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(415,400));
add(scrollPane);
// add(scrollPane, 0, 40, 415, 400);
}
/* //method add defined in the mother object
public void add(Component c, int x, int y, int sX, int sY)
{
add(c);
c.setBounds(x,y,sX,sY);
}*/
public static void main( String args[] )
{
JPanel baseView = new TestScroller();
baseView.setPreferredSize(new Dimension(500,500));
baseView.setOpaque(false);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(baseView);
frame.setSize(500,500);
frame.setVisible(true);
frame.repaint();
}
}