Results 1 to 2 of 2
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default 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)) );
    	}
    
    }
    Java 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)) );
    	}
    }
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Moncleared View Post
    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.

    It would if you didn't also set the preferredSize of your Navigation JPanel:

    Java Code:
       public Navigation() {
          this.setLayout(new GridBagLayout());
    
          //..... etc......
    
          this.setPreferredSize(new Dimension((int) (xSize * 0.20), (int) (ySize * 0.90))); // *** !!! ***
       }
    Get rid of that and you'll get rid of the artificial constraint on its size and it will scroll just fine.

Similar Threads

  1. problem positioning a JPanel in a JScrollPane
    By gib65 in forum AWT / Swing
    Replies: 5
    Last Post: 09-06-2013, 09:27 AM
  2. JScrollPane
    By UJJAL DHAR in forum New To Java
    Replies: 12
    Last Post: 08-17-2010, 07:47 PM
  3. Replies: 1
    Last Post: 05-09-2010, 07:34 PM
  4. Replies: 3
    Last Post: 02-06-2010, 04:28 PM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •