Solved

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

Posted on 2010-11-29
11
1,186 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>componentResized

I think, this gets notified after the component resized and can't stop it!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
isn't the asker wants to just have the resize functionality only at the corner and not at the sides?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I let him/her answer that question
0
 

Author Comment

by:amitabhg
Comment Utility
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
Comment Utility
>> Then i override the validate method
In what class?
Can you show us your validate() method?
0
 

Author Comment

by:amitabhg
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Thanx 4 axxepting
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
countAbc challenge 9 49
Eclipse Neon and jdk 1.8.0 11 63
java  and programming certification ? 4 62
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 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

6 Experts available now in Live!

Get 1:1 Help Now