Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

size of a JPanel

Posted on 2003-03-21
9
Medium Priority
?
457 Views
Last Modified: 2013-11-23
2 questions:

1. What is the best way to enable a JPanel or JFrame to close when a user presses the ESCAPE button?

2. How do you make a JPanel or JFrame NOT resizable. So that a user cannot change it's size by dragging the edges with the mouse?
0
Comment
Question by:rich420
[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
  • 5
  • 3
9 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179590
If you want a JFrame to close when the user presses 'Esc', you should add a key-listener in your JFrame like:

addKeyListener ( new KeyAdapter () {
  public void keyPressed ( KeyEvent ke )
  {
    if ( ke.getKeyCode () == 27 )
      dispose () ; // end if

  } // end of keyPressed ()
} ) ;


Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179600
In fact, I would say that its better to use KeyEvent.VK_ESCAPE instead of 27 in the if () statement above.

Mayank.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 180 total points
ID: 8179610
To disallow the user from resizing the JFrame, write:

setResizable ( false ) ;

in the constructor.

Hope that helps!

Mayank.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rich420
ID: 8185314
Thanks Mayank, Just one last question:
Is it posibile to make a JPanel not resizable?
0
 
LVL 1

Expert Comment

by:membersh
ID: 8185396
Either make Frame which contains the panel non resizable OR setLayout of Panel which contains this panel to null & don't forget to setbounds for inner panel.
0
 

Author Comment

by:rich420
ID: 8192367
Hey Mayank,

I'm awarding you the points for the setResizable(false) help you gave me. However, The Escape key is not working with KeyEvent.VK_ESCAPE. hmm... dunno why?

Is it because KeyEvents have a very low priority?

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8192859
If its not working with the VK_ESCAPE property, try putting 27 in its place. Or else, it may be that some other object in the JFrame has got the focus when you're pressing Esc. In that case, pressing a key won't take you to the JFrame's key-listener's method, but it will only invoke that method in case the focus is on the JFrame and you've pressed a key.

Mayank.
0
 

Author Comment

by:rich420
ID: 8198657
So how should I bring the focus back to the correct object?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8200271
Try clicking somewhere on the back-ground or on the title-bar so that the focus is on the JFrame and not on any other object (like a button or text-field or label, etc).

Mayank.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

705 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