Solved

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

Posted on 2010-11-29
11
1,226 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

820 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