Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: jsplipane and resizing

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default jsplipane and resizing

    Hello, newbie here.
    I want to do a three way split with jsplipane (two panels on top and one at bottom).
    And I want the top-left panel to remain a square no matter what. When the user moves either the vertical or horizontal splitpane (or window is resized in other ways), the three panels are to be adjusted so that the top-left pane remains a square but with different dimension. How do I achieve that ?

    thank you,


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: jsplipane and resizing

    Can anyone help please? Maybe i asked the question in the wrong forum ?

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jsplipane and resizing

    Just a word to the wise, bumping your thread like that actually decreases your chances of getting help- people will see that the post has been replied to already, so they'll move on to one with zero replies instead.

    But you can nest JSplitPanes, and put a PropertyChangeListener on each JSplitPane that sets the value of the other JSplitPane divider as appropriate.

    Recommended reading: How to Use Split Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    MultiSplitPane: Splitting Without Nesting | Java.net
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: jsplipane and resizing

    Quote Originally Posted by KevinWorkman View Post
    Just a word to the wise, bumping your thread like that actually decreases your chances of getting help- people will see that the post has been replied to already, so they'll move on to one with zero replies instead.
    Sorry but I disagree. People may equally have thought I found a solution somewhere else which I didn't. I waited for two days (48 hours as advised when I registered in the forum) before bumping... I would say that is fair, no ?
    But you can nest JSplitPanes, and put a PropertyChangeListener on each JSplitPane that sets the value of the other JSplitPane divider as appropriate.

    Recommended reading: How to Use Split Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    MultiSplitPane: Splitting Without Nesting | Java.net
    Thanks for the tip. I read the whole of the first link and a lot of other pages before I posted my question here but none addressed the problem I was having.
    Also I needed to do the same thing when the window is re-sized, so I was hoping there would a "panel needs resizing" event that I can use to calculate and override the width and height.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: jsplipane and resizing

    I tried your idea but it doesn't work. When any of them gets moved I set the others splitpane position to 0.5 but it doesn't work as intended (sometimes it does).
    When the window is resized by dragging the frame, this listener doesn't sense it at all.
    So my question still remains unanswered if someone wants to help..
    import java.awt.BorderLayout;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
     
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JSplitPane;
     
    public class Hello {
    	public static void main(String args[]) {
    		JFrame frame = new JFrame("Property Split");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JComponent b1 = new JButton("A");
    		JComponent b2 = new JButton("B");
    		JComponent b3 = new JButton("C");
    		final JSplitPane hpane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,b1,b2);
    		final JSplitPane hpane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,hpane1,b3);
     
    		PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
    			public void propertyChange(PropertyChangeEvent changeEvent) {
    				JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
    				String propertyName = changeEvent.getPropertyName();
    				if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
    					if(sourceSplitPane == hpane1)
    						hpane2.setDividerLocation(0.5);
    					else
    					    hpane1.setDividerLocation(0.5);
    					System.out.println(" " + hpane1.getDividerLocation() 
    							         + " " + hpane2.getDividerLocation()
    							         );
    				}
    			}
    		};
     
    		hpane1.addPropertyChangeListener(propertyChangeListener);
    		hpane2.addPropertyChangeListener(propertyChangeListener);
     
    		frame.add(hpane2, BorderLayout.CENTER);
    		frame.setSize(300, 150);
    		frame.setVisible(true);
    	}
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jsplipane and resizing

    Quote Originally Posted by dabdi View Post
    Sorry but I disagree. People may equally have thought I found a solution somewhere else which I didn't. I waited for two days (48 hours as advised when I registered in the forum) before bumping... I would say that is fair, no ?
    It's fair, in that I'm not going to waste time arguing about the best way to receive answers- I can just tell you, from experience, what many people do is go to the "What's New?" tab, then look for posts that have zero responses. Or they use the "Unanswered Threads" link, which only shows threads with zero replies. We often don't have time to read every post. Plus, the original post seems to have been over the weekend, and I'm not sure how many people are around before Monday. Anyway, I offer that not as an argument, just something to think about for next time.

    Quote Originally Posted by dabdi View Post
    Also I needed to do the same thing when the window is re-sized, so I was hoping there would a "panel needs resizing" event that I can use to calculate and override the width and height.
    Did you google anything like "panel resize listener"? I believe what you're looking for is a ComponentListener.
    Last edited by KevinWorkman; June 7th, 2011 at 09:17 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jsplipane and resizing

    Quote Originally Posted by dabdi View Post
    I tried your idea but it doesn't work. When any of them gets moved I set the others splitpane position to 0.5 but it doesn't work as intended (sometimes it does).
    That's not exactly what I said to do- I wouldn't expect setting both to .5 to accomplish your goal. You're going to have to figure out what the dimensions of the square for the upper-left component should be, which is going to be different depending on which JSplitPane was resized.

    Quote Originally Posted by dabdi View Post
    When the window is resized by dragging the frame, this listener doesn't sense it at all.
    Again, I wouldn't expect it to. That's a different listener, which I mentioned above.

    Another thing you might want to take into account is whether setting the divider location inside the PropertyChangeListener triggers the listener, which sets the divider location, which triggers the listener... which might lead to weird behavior.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: jsplipane and resizing

    That's not exactly what I said to do- I wouldn't expect setting both to .5 to accomplish your goal. You're going to have to figure out what the dimensions of the square for the upper-left component should be, which is going to be different depending on which JSplitPane was resized.
    Ofcourse, that was just a quick example. My actual goal is to avoid distortion of an image in a panel. So once I know the dimensions of the image, I would enforce the location of the splitpanes in such a way that the image is not distorted in any way (only scaled up or down).

    Again, I wouldn't expect it to. That's a different listener, which I mentioned above.

    Another thing you might want to take into account is whether setting the divider location inside the PropertyChangeListener triggers the listener, which sets the divider location, which triggers the listener... which might lead to weird behavior.
    Exactly. This method sounds recursive and has already given me weird behaviours. So instead of listening for splitpane movements, I would prefer listening to resizing event for the panel which contains the image (if there is one). Only one listener will be needed in that case. But even with that I am afraid there could be the same problem... Maybe if there is an event which tells "component is about to be resized" ?
     public void componentResized(ComponentEvent e) {
            Dimension dim = getcomponesize ....
            Adjust dim ....
            Resize this component (triggers recursion) ...
        }

    I have not tried it, so I will come back with the result later.
    thanks.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jsplipane and resizing

    You get around the recursive nature of the listeners by calling the remove___Listener() methods, then making the change, then calling add___Listener() again.

    But it sounds like what you really want to do is extend JPanel, override the paintComponent() method, and draw the image yourself.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: jsplipane and resizing

    Finally it works, with a bit of a hack though When panel is resized , set the divider location (avoids recursion) instead of resizing immediately. And also set the divider_resize_weight to some random value so that the component listener is triggered when window frame is resized. Without the latter panel doesn't adjust itself.
    Thanks
    import java.awt.*;
    import java.beans.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class Hello {
    	public static void main(String args[]) {
    		JFrame frame = new JFrame("Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JComponent b1 = new JPanel();
    		JComponent b2 = new JButton("B");
    		JComponent b3 = new JButton("C");
    		final JSplitPane hpane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,b1,b2);
    		final JSplitPane hpane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,hpane1,b3);
     
    		ComponentListener cl = new ComponentListener() {
    			public void componentHidden(ComponentEvent e) {}
    		    public void componentMoved(ComponentEvent e) {}
    		    public void componentShown(ComponentEvent e) {}
    		    public void componentResized(ComponentEvent e) {
    		    	hpane1.setDividerLocation(0.5);
    		    	hpane2.setDividerLocation(0.5);           
    		    }
    		};
    		hpane1.setResizeWeight(0.1); //needed only to trigger component
    		                             //listener when window is resized
    		hpane2.setResizeWeight(0.1);
    		hpane1.addPropertyChangeListener(propertyChangeListener);
    		hpane2.addPropertyChangeListener(propertyChangeListener);
    		b1.addComponentListener(cl);
     
     
    		frame.add(hpane2, BorderLayout.CENTER);
    		frame.setSize(300, 150);
    		frame.setVisible(true);
    	}
    }
    Last edited by dabdi; June 7th, 2011 at 12:13 PM.

  11. The Following User Says Thank You to dabdi For This Useful Post:

    copeg (June 8th, 2011)

Similar Threads

  1. Resizing JPanels in JScrollPanes
    By aussiemcgr in forum AWT / Java Swing
    Replies: 4
    Last Post: November 29th, 2010, 02:13 PM
  2. Dynamically resizing an applet
    By mjpam in forum AWT / Java Swing
    Replies: 6
    Last Post: September 19th, 2010, 10:32 PM
  3. dynamically resizing 2d array
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 2nd, 2010, 11:10 AM
  4. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM