Results 1 to 4 of 4
  1. #1
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Question JScrollPane is not aware of panelsize of the including JSplitPane-wing

    I want to show two JTables at either side of a JSplitPane. At top of either JTable there shall be some associated JButtons. To allow for scrolling, I will put the JTables to JScrollPanes. To arrange the JButtons and the JTables the way I described before I will put the JButtons NORTH and the JScrollPanes to the CENTER of JPanels. But when doing so, the JScrollPane is no longer scrolling. It is not aware of shrinking the JSplitPane. To show the effect I've build an example, where the left side of a JSplitPane is as described and the right wing contains just the JScrollPane. In the example the right side recognizes the size is scrollable.
    I wanted to add a PropertyChangeListener to the JSplitPane and to adjust the JPanel and the JScrollPane but neither of the components reacts to the setSize()-methods.
    I'm looking for an explanation and more than this to a solution for my problem.

    Java Code:
    package de.gombers.zos.smf.ui;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.*; 
    import javax.swing.*;
    import javax.swing.border.Border;
    
    public class MusterSplitPane extends JSplitPane implements PropertyChangeListener {
    	String[] fields;
    	String[] titles;
    	JButton select = new JButton("Select"); 
    	Vector<String[]> sfields = new Vector<String[]>(20, 10);
    	Border redline, greenline;
    	static Dimension dimension=new Dimension(600, 400);
    	public MusterSplitPane(String[] fields, String[] titles) {
    		this.setOrientation(HORIZONTAL_SPLIT);
    		this.fields=fields;
    		this.titles=titles;
    		this.addPropertyChangeListener(this);
    
    		redline = BorderFactory.createLineBorder(Color.red);
    		greenline = BorderFactory.createLineBorder(Color.green);
    
    		// left Pane
    		String[][] fieldtab = new String[fields.length][2];
    		String[] colNames = {"Fields", "Titles"};
    		for (int i=0; i<fields.length; i++) {
    			fieldtab[i][0]=fields[i];
    			fieldtab[i][1]=titles[i];
    		}
    		JTable lTab = new JTable(fieldtab, colNames );
    		JScrollPane lPane=new JScrollPane();
    		lPane.setBorder(greenline);
    		lPane.setSize(150, 150);
    		lTab.setSize(150, 150);
    		lPane.setViewportView(lTab);
    
    		JPanel leftPane = new JPanel();
    		leftPane.setBorder(redline);
    
    		leftPane.add(new JButton("myButton"), BorderLayout.NORTH);
    		leftPane.add(lPane, BorderLayout.CENTER);
    		leftPane.setSize(150, 150);
    
    		// right Pane
    		JTable rTab = new JTable(fieldtab, colNames );
    		JScrollPane rPane=new JScrollPane();
    		rPane.setViewportView(rTab);
    
    		this.setLeftComponent(leftPane);
    		this.setRightComponent(rPane);
    	}
    
    	public void propertyChange(PropertyChangeEvent pce) {
    		System.out.println(pce.getPropertyName() + "  " + this.getDividerLocation() + " PanelHeigth=" + this.getHeight());
    	}
    	
    	public static void main(String[] args) {
    		JFrame jf = new JFrame("Test JSplitPane");
    		jf.setSize(dimension);
    		String[] fields={"one", "two", "three", "four"};
    		String[] titles={"isOne", "isTwo", "isThree", "isFour"};
    		MusterSplitPane msp = new MusterSplitPane(fields, titles);
    		jf.add(msp);
    		jf.setVisible(true);
    	}
    }

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

    Default Re: JScrollPane is not aware of panelsize of the including JSplitPane-wing

    Your leftPane JPanel will use its default FlowLayout, not BorderLayout since you never specifically give it a layout manager. Also, you should never set sizes of things and only occasionally set preferred sizes.

  3. #3
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: JScrollPane is not aware of panelsize of the including JSplitPane-wing

    Thank you, that fixed it. Though I've have no clue why scrolling did not work for FlowLayout. Can you tell - I feel uncomfortable not understanding this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: JScrollPane is not aware of panelsize of the including JSplitPane-wing

    FlowLayout associates a preferredSize that lays out all the components, at their preferredSizes, in a single row.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JSplitPane separator customization
    By azzardo in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 01:31 PM
  2. displaying JSplitPane in JPanel
    By sandysm in forum AWT / Swing
    Replies: 4
    Last Post: 07-26-2010, 09:13 AM
  3. Managing jPanels in jSplitPane
    By calexander in forum Advanced Java
    Replies: 6
    Last Post: 11-13-2008, 07:06 PM
  4. JSplitPane nightmare
    By SwinGirl in forum SWT / JFace
    Replies: 1
    Last Post: 05-01-2008, 08:24 PM
  5. I need create a JSplitPane
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:12 AM

Posting Permissions

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