Results 1 to 4 of 4
  1. #1
    david.stefka is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Dynamically changing JPanel in a JScrollPane - incorrect sizing

    Hi,

    I have created a simple Swing application, which consists of some JComponent (e.g., a table, replaced by a JPanel in this demo), and of a toolbar above the component. The component can add/remove additional toolbars into the toolbar panel using two buttons. The toolbar pane is contained in a JScrollPane, so that you can add many toolbars and scroll to them. For better visibility, the toolbar pane has a blue background.

    I have the following problem. If I set the ScrollPane's scrollbar policy to HORIZONTAL_SCROLLBAR_ALWAYS, all is well, BUT the scrollbar consumes space even if it is not needed. However, if I set the policy to HORIZONTAL_SCROLLBAR_AS_NEEDED, the ScrollPane begins to act strangely. If I add the first toolbar, there is too much space (as if there was a scrollbar). If I resize the component so that the scrollbar is shown, the scrollpane resizes incorrectly and the scroll bar spans over the toolbar.

    The sample code follows. I tried some black magic (validating/revalidating, setting maximum, minimum, preferred sizes, etc.), but nothing helped. What am I doing wrong?

    Thanks for any help!

    Java Code:
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    
    public class Tester2 extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    
    	
    	public Tester2() {
            JNotWorkingCrap crap = new JNotWorkingCrap();
            add(crap);
        }
    
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Tester2 newContentPane = new Tester2();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
    
        
        
        public static class JNotWorkingCrap extends JPanel {
    
        	/** panel with toolbars */
        	private JPanel toolbarPane;
        	/** scroll pane for the toolbar pane */
        	private JScrollPane toolbarSp;
        	/** central panel with the buttons*/
        	private JPanel centralPanel;
        	/** buttons for adding/removing a toolbar */
        	private JButton addToolbarBtn;
        	private JButton removeToolbarBtn;
    
        	/** counter indicating number of toolbars */
        	private static int toolbarCnt = 0;
        	
        	public JNotWorkingCrap() {
        		initComponents();
        		initLayout();
        	}
    
        	private void initComponents() {
        		centralPanel = new JPanel();
        		toolbarPane = new JPanel();
        		toolbarSp = new JScrollPane(toolbarPane);
        		
        		addToolbarBtn = new JButton(new AbstractAction("Add toolbar") {
    				
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					// create a new "toolbar"
    					JPanel newToolbar = new JPanel();
    					newToolbar.add(new JLabel("Toolbar "+toolbarCnt));
    					// with a fixed size
    					newToolbar.setPreferredSize(new Dimension(150, 50));
    					newToolbar.setMinimumSize(new Dimension(150, 50));
    					newToolbar.setMaximumSize(new Dimension(150, 50));
    					newToolbar.setSize(new Dimension(150, 50));
    					newToolbar.setBorder(BorderFactory.createTitledBorder("Toolbar " + toolbarCnt));
    					
    					// add the "toolbar" to the toolbar pane
    					toolbarPane.add(newToolbar);
    					
    					// revalidate/repaint
    					toolbarPane.revalidate();
    		    		revalidate();
    		    		revalidate();
    		    		
    		    		// increment the counter
    		    		toolbarCnt++;
    				}
    			});
    
        		removeToolbarBtn = new JButton(new AbstractAction("Remove toolbar") {
        			
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					// remove the toolbar from the toolbar pane
    					toolbarPane.remove(toolbarPane.getComponentCount()-1);
    					
    					// revalidate/repaint
    					toolbarPane.revalidate();
    		    		revalidate();
    		    		toolbarPane.repaint();
    		    		
    		    		// decrement the counter
    					toolbarCnt--;
    				}
    			});
    
        	}
    
        	/**
        	 * Lay out the components.
        	 */
        	private void initLayout() {
        		setLayout(new BorderLayout(0, 0));
        		setPreferredSize(new Dimension(600, 300));
        		
        		centralPanel.add(addToolbarBtn);
        		centralPanel.add(removeToolbarBtn);
        		
        		toolbarPane.setLayout(new BoxLayout(toolbarPane, BoxLayout.X_AXIS));
        		toolbarPane.setOpaque(true);
        		toolbarPane.setBackground(Color.BLUE);
    
        		
        		toolbarSp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        		//toolbarSp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        		toolbarSp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    
        		
        		add(toolbarSp, BorderLayout.NORTH);
        		add(centralPanel, BorderLayout.CENTER);
        	}
    
        }
    
        
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

  3. #3
    david.stefka is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    OK, the solution to the problem is to calculate the SP's size manually each time the inner panel changes. The problem and its solution is described here.

  4. #4
    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 Darryl.Burke View Post
    To his credit, he didn't cross-post until he received no reply here for a day, and he posted info towards the solution.

Similar Threads

  1. Changing JTableModel does not change JScrollpane size
    By stelzergil in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 03:14 AM
  2. Changing JPanel sizes
    By Basit in forum Java Applets
    Replies: 2
    Last Post: 08-12-2009, 07:48 PM
  3. Replies: 1
    Last Post: 04-01-2009, 11:28 AM
  4. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2008, 12:19 AM
  5. Help needed with sizing components
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 05:40 AM

Tags for this Thread

Posting Permissions

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