aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JDialog problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog problem" Watch "JDialog problem" New topic
Author

JDialog problem

Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Hi

I have JFrame with a JButton, and when clicked it opens up a new JDialog with a JTextField with some text in it. When this JDialog is closed, the text in the text field is erased, however when the JDialog is opened again, the old text that was erased flicker (only for an instant, if you look closely). I'd like to know if there is any way fix this. Do note that it is not something that happens all the time. It usually happens the first try for me, but it should'nt take many trys even if it does'nt.

Thanks in advance for any help.


Heres the example code (used a big font because it is easier to see that way):

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
One possible solution: create a new dialog each time the button is pressed.

Otherwise, I don't see any obvious errors in your code, but myself I'd prefer not to extend JFrame or JDialog if I'm not altering their intrinsic behavior.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
I guess that the problem is that by the time the RepaintManager schedules painting of the text field the window is not visible so the painting is not done until it is made revisible, causing the flickering.

You can force the repainting by using:



Although not relevant to the problem, I also changed you code to the following:

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Why reinvent the wheel?
Check out the various JOptionaPane.showXXXDialog() methods

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Thanks Rob that worked great

Maneesh Godbole wrote:Why reinvent the wheel?
Check out the various JOptionaPane.showXXXDialog() methods

As far as I know JOptionPane.showXXXDialog() only works on questions, and it is not something that you can modify as you wish, like you can with a JDialog or JFrame. Like adding many buttons or textfields and/or different containers.

pete stein wrote:
but myself I'd prefer not to extend JFrame or JDialog if I'm not altering their intrinsic behavior.

Is there any disadvantage with extending JFrame or JDialog, or is it only a personal preference? Good to know for future reference .
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Alexander Walker wrote:
Maneesh Godbole wrote:Why reinvent the wheel?
Check out the various JOptionaPane.showXXXDialog() methods

As far as I know JOptionPane.showXXXDialog() only works on questions, and it is not something that you can modify as you wish, like you can with a JDialog or JFrame. Like adding many buttons or textfields and/or different containers.


If you look at the API, you will notice those methods accept an Object message.
You can always construct your panel, put in all the UI components on it and pass the panel as the message to the method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Alexander Walker wrote:
Maneesh Godbole wrote:Why reinvent the wheel?
Check out the various JOptionaPane.showXXXDialog() methods

As far as I know JOptionPane.showXXXDialog() only works on questions, and it is not something that you can modify as you wish, like you can with a JDialog or JFrame. Like adding many buttons or textfields and/or different containers.

Sure it is. The message object doesn't need to be a String. It can be:
- an Icon -- a label with the icon will be added.
- a Component -- the component itself will be added. This component can be a simple component like a JTextField, or a full GUI on its own.
- an Object[] -- each element will be added separately.
- anything else -- the string value will be added as one or more labels, depending on the number of line breaks.

So yes, adding containers is quite possible indeed.

pete stein wrote:
but myself I'd prefer not to extend JFrame or JDialog if I'm not altering their intrinsic behavior.

Is there any disadvantage with extending JFrame or JDialog, or is it only a personal preference? Good to know for future reference .

It's a bad object oriented approach. Your dialog doesn't add any behaviour or anything. Instead you should just use a regular JFrame or JDialog to which you add the rest of the GUI. So instead of your class being a JFrame or JDialog, it uses one.

But I must admit, I go the lazy way myself all too often and just extend.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alexander Walker
Ranch Hand

Joined: Jun 04, 2010
Posts: 34
Rob Prime wrote:
Alexander Walker wrote:
Maneesh Godbole wrote:Why reinvent the wheel?
Check out the various JOptionaPane.showXXXDialog() methods

As far as I know JOptionPane.showXXXDialog() only works on questions, and it is not something that you can modify as you wish, like you can with a JDialog or JFrame. Like adding many buttons or textfields and/or different containers.

Sure it is. The message object doesn't need to be a String. It can be:
- an Icon -- a label with the icon will be added.
- a Component -- the component itself will be added. This component can be a simple component like a JTextField, or a full GUI on its own.
- an Object[] -- each element will be added separately.
- anything else -- the string value will be added as one or more labels, depending on the number of line breaks.

So yes, adding containers is quite possible indeed.

Maneesh Godbole wrote:
If you look at the API, you will notice those methods accept an Object message.
You can always construct your panel, put in all the UI components on it and pass the panel as the message to the method.

I did'nt know you could do it like that. Thanks for the good faqt.


Rob Prime wrote:
pete stein wrote:
but myself I'd prefer not to extend JFrame or JDialog if I'm not altering their intrinsic behavior.

Is there any disadvantage with extending JFrame or JDialog, or is it only a personal preference? Good to know for future reference .

It's a bad object oriented approach. Your dialog doesn't add any behaviour or anything. Instead you should just use a regular JFrame or JDialog to which you add the rest of the GUI. So instead of your class being a JFrame or JDialog, it uses one.

But I must admit, I go the lazy way myself all too often and just extend.

I see, that is good to know. Good explanations too.

I almost did'nt notice that small text
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDialog problem