Add JScrollPane to a JPanel
Hello,
I have this interface to create. I have a problem with the JScrollPane:
I posted in Add JScrollPane to a JPanel - Stack Overflow but I didn't get answer.
http://i.imgur.com/gJ6GU.png
I declared a JPanel with a Gridlayout(8,1,0,2), I want 8 rows appear in this panel. A row is a JPanel to, I set the size to make the 8 row panels appear in the big panel. If the number of rows pass 8, I get two columns ... I added a JScrollPane but it doesn't appear. Testing button at the place of button, the scrollpane appear but returning to panel it disappear..
How can I do ??
Re: Add JScrollPane to a JPanel
If you want help with code, you'll have to provide an SSCCE that demonstrates the problem. Note that this does NOT mean you should post all of your code- boil it down to as few lines as possible.
Re: Add JScrollPane to a JPanel
I have a frame.
I applyed AbsoluteLayout in this frame Then I added a panel with a GridLayout.
Clicking with mouse left button and Surround with, I chose JscrollPane .
Yhis is the code without Sourround with JScrollPane
Code :
package d06.m03;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.SystemColor;
public class ActionExample4 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ActionExample4 frame = new ActionExample4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ActionExample4() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 594, 426);
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(SystemColor.activeCaption);
panel.setBounds(91, 11, 154, 264);
getContentPane().add(panel);
panel.setLayout(new GridLayout(8, 1, 1, 2));
for(int i=0;i<9;i++)
{
JPanel panel_1 = new JPanel();
panel.add(panel_1);
}
}
}
and with surround :
Code :
package d06.m03;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import java.awt.SystemColor;
public class ActionExample4 extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ActionExample4 frame = new ActionExample4();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ActionExample4() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 594, 426);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(91, 11, 154, 264);
getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setBackground(SystemColor.activeCaption);
panel.setLayout(new GridLayout(8, 1, 1, 2));
for(int i=0;i<9;i++)
{
JPanel panel_1 = new JPanel();
panel.add(panel_1);
}
}
}
this is the output with both codes:
http://i.imgur.com/BgK5b.png
I want it like this:
http://i.imgur.com/3IOEW.png
Thanks in advance.