Solved

Java Core GUI APIs: Force JFrame to resize in only diagonal direction

Posted on 2010-11-29
11
1,258 Views
Last Modified: 2013-11-23
I'm looking for a way to force a JFrame to only resize along a diagonal. I need to maintain a particular width:length ratio for the entire frame. I know I have seen this is other applications (probably not in Java though). So to clarify, the JFrame is resizable, but you cannot size in only the vertical or in only the horizontal directions (shouldn't even get the resize icon when you put the mouse along those borders). But, if the mouse is in the lower right corner, the diagonal resize icon shows up and the entire JFrame resizes along the diagonal maintaining the aspect ratio. Any thoughts?
0
Comment
Question by:amitabhg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 34230045
there are no settings available as such, you may try by calling setResizable true|false based on the mouse location?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 34265067
I think the way to go is to add a ComponentAdapter and inside that one you implement the method

public void componentResized(ComponentEvent e) {
}

Inside that method you must assure that the aspect ratio of the JFrame is kept constant.
 
>> (shouldn't even get the resize icon when you put the mouse along those borders)
I would keep all the cursors as they are by default. But if the user makes the JFrame wider/smaller the above method should assure that its height also becomes bigger/smaller. And vice versa.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34265641
>>componentResized

I think, this gets notified after the component resized and can't stop it!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 37

Expert Comment

by:zzynx
ID: 34265869
>> I think, this gets notified after the component resized
Indeed. But what's wrong with that?
As I said:
>> if the user makes (read as "has made") the JFrame wider/smaller the above method should assure that its height also becomes bigger/smaller. And vice versa.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34265886
isn't the asker wants to just have the resize functionality only at the corner and not at the sides?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 34266594
I let him/her answer that question
0
 

Author Comment

by:amitabhg
ID: 34300542
I have implemented ComponentListener  and used component resize method but it always generate the event the moment mouse is released.
My requirement was to get the event while dragging which can solve my problem. Then i override the validate method and  i got the event during dragging the frame  but then there is one problem.

 validate method is working on one system but not on another.
can somebody tell me why this is happening...thanks in advance

0
 
LVL 37

Expert Comment

by:zzynx
ID: 34300571
>> Then i override the validate method
In what class?
Can you show us your validate() method?
0
 

Author Comment

by:amitabhg
ID: 34307751
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainFrame extends JFrame implements ComponentListener {

    JButton button;

    public MainFrame() {
        button = new JButton();
        button.setBounds(30, 30, 10, 10);
        add(button);

        addComponentListener(this);
        setSize(300, 300);
        setVisible(true);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void validate() {
        System.out.println("in validate");
        super.validate();
    }

    @Override
    public void componentResized(ComponentEvent e) {
        System.out.println("component resized");

    }

    public static void main(String args[]) {
        MainFrame frame = new MainFrame();
    }

    @Override
    public void componentShown(ComponentEvent e) {
        // TODO Auto-generated method stub

    }

}

I am posting here a sample code..not actual code.(actual code is very big).. I just wanna know that what is the actual functionality of validate function.......
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 34316931
>> I just wanna know that what is the actual functionality of validate function.......

Java help for Container.validate() method:

public void validate()
Validates this container and all of its subcomponents.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

Overrides:
validate in class Component
0
 
LVL 37

Expert Comment

by:zzynx
ID: 34500258
Thanx 4 axxepting
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question