Solved

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

Posted on 2010-11-29
11
1,194 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now