Issue
(using netbeans)
So for my project I need to add a JscrollPane
so that the user can see all of the JTextArea
output, a piechart and the two buttons I have added. This is the code I have implementing the JscrollPane
. However it is causing the program to no longer produce an output screen. My question is do I need to add the JscrollPane
to the JPanel or to the JFrame and if so what am I doing wrong (tried to include as much of the code as I thought was relevant)
P.S Should I change from Borderlayout
to a Boxlayout
? Would that make a difference in terms of adding a jscroll?
JFrame frame1 = new JFrame("Portfolio Results");
frame1.setSize(800,800);
// frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// output screen declartions
frame1.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
frame1.add(panel1,BorderLayout.PAGE_START);
panel1.setLayout(new BorderLayout());
JTextArea area1 = new JTextArea();
area1.setPreferredSize(new Dimension(600,600));
panel1.add(area1,BorderLayout.PAGE_START);
JScrollPane scp1 = new JScrollPane(frame1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame1.add(scp1);
//code for Pie chart and two button
DefaultPieDataset piedata = new DefaultPieDataset();
piedata.setValue("test", new Integer (100));
JFreeChart chart = ChartFactory.createPieChart("test", piedata, true, true, true);
PiePlot p = (PiePlot)chart.getPlot();
ChartPanel testpan = new ChartPanel(chart);
panel1.add(testpan,BorderLayout.CENTER);
JButton button= new JButton("SAVE");
// button.setPreferredSize(new Dimension(80,20));
// Listener listener = new Listener();
// button.addActionListener(this);
panel1.add(button,BorderLayout.PAGE_END);
JButton pbutton=new JButton("Print");
panel1.add(pbutton,BorderLayout.SOUTH);
Solution
You should init the JScrollPane
with the object you want to scroll through.
In your example, it seems the JTextArea
is the object you want, so:
JScrollPane scp1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
From the Oracle
docs:
JScrollPane(Component view)
Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.
Also, see this Oracle
example.
Answered By - MordechayS
Answer Checked By - Willingham (JavaFixing Volunteer)