Results 1 to 2 of 2
Thread: JPanel/JScrollPane issue
- 09-23-2010, 03:34 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
JPanel/JScrollPane issue
Hi,
I'm trying to create a simple application that has a Navigation Panel and a Content Panel. What I've created thus far is a Frame that holds a JPanel. This JPanel contains a JSplitPanel. The JSplitPanel holds my Nav/Content Panels.
My issue currently is that when I add say lots of objects to the Navigation JPanel it only holds X amounts. For example, if I add 500 JLabels using GridBagLayout down the rows I only see a few of them.
I assumed that since I had added this JPanel inside my JScrollPane and the JScrollPane was added to the SplitPanel that if I added too many Components it would scroll.
Hopefully the code can explain better what I am attempting :)
Any feedback/recommendation is welcome.
Java Code:import java.awt.*; import javax.swing.*; public class Main { public static void main(String args[]){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.out.println("Issue with setting LookAndFeel!"); } javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); frame.add(new frontPanel().getJPanel(), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); //Set Full Screen items frame.setExtendedState(Frame.MAXIMIZED_BOTH); } } @SuppressWarnings("serial") /** * This class will be out front JPanel. We'll have multiple JPanels embedded in this JPanel. * The vision is that we'll have a JPanel for navigation on the LEFT (20%) and a JPanel for * editor pane on the RIGHT (80%) */ class frontPanel extends JPanel{ public frontPanel(){ this.setLayout(new BorderLayout()); //Create our two Custom JPanels Navigation nav = new Navigation(); Editor editor = new Editor(); //Create our ScrollPanes to place our JPanels inside JScrollPane navSP = new JScrollPane(nav); JScrollPane editorSP = new JScrollPane(editor); //Create our JSplitPane which holds our two ScrollPanes JSplitPane sp = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, navSP, editorSP); //Setup Some Common Characteristics for the JScrollPanes //MinimumSize with (0,0) means user can drag all the way from left/right etc. navSP.setMinimumSize(new Dimension(150,0)); editorSP.setMinimumSize(new Dimension(0,0)); //Setup Some Common Characteristics for the JSplitPane sp.setOneTouchExpandable(true); sp.setDividerLocation(150); //Add to our FrontPanel this.add(sp,BorderLayout.CENTER); } public JPanel getJPanel(){ return this; } }Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Toolkit; import javax.swing.JLabel; import javax.swing.JPanel; public class Navigation extends JPanel{ private static final long serialVersionUID = -9175807403229139466L; public Navigation(){ this.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); this.setBackground(Color.BLUE); //Determine what the default size of the navigation panel should be Toolkit tk = Toolkit.getDefaultToolkit(); int xSize = ((int) tk.getScreenSize().getWidth()); int ySize = ((int) tk.getScreenSize().getHeight()); for ( int i=0; i<500; i++){ c.gridy = i; this.add(new JLabel(i+"_"), c); } this.setPreferredSize(new Dimension((int) (xSize*0.20),(int)(ySize*0.90)) ); } }ThanksJava Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JPanel; public class Editor extends JPanel { private static final long serialVersionUID = 5226641103819463968L; public Editor() { this.setBackground(Color.GREEN); //Determine what the default size of the navigation panel should be Toolkit tk = Toolkit.getDefaultToolkit(); int xSize = ((int) tk.getScreenSize().getWidth()); int ySize = ((int) tk.getScreenSize().getHeight()); this.setPreferredSize(new Dimension((int) (xSize*0.75),(int)(ySize*0.90)) ); } }
-
It would if you didn't also set the preferredSize of your Navigation JPanel:
Get rid of that and you'll get rid of the artificial constraint on its size and it will scroll just fine.Java Code:public Navigation() { this.setLayout(new GridBagLayout()); //..... etc...... this.setPreferredSize(new Dimension((int) (xSize * 0.20), (int) (ySize * 0.90))); // *** !!! *** }
Similar Threads
-
JScrollPane
By UJJAL DHAR in forum New To JavaReplies: 12Last Post: 08-17-2010, 06:47 PM -
problem positioning a JPanel in a JScrollPane
By gib65 in forum AWT / SwingReplies: 4Last Post: 08-02-2010, 10:34 AM -
Mouselistener for a Jscrollpane which contains Jpanel within it.
By bikashg in forum New To JavaReplies: 1Last Post: 05-09-2010, 06:34 PM -
Dynamically changing JPanel in a JScrollPane - incorrect sizing
By david.stefka in forum AWT / SwingReplies: 3Last Post: 02-06-2010, 03:28 PM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks