Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-29
11
Medium Priority
?
1,303 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

971 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