aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Does this piece of code hang for you all also ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Does this piece of code hang for you all also ?" Watch "Does this piece of code hang for you all also ?" New topic
Author

Does this piece of code hang for you all also ?

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hello all,
I was checking some of the methods in TextField and TextArea classes. During that process I found that this piece of code makes the program hang when executed. Can anybody add anything to this?. I was able to create a Button object but NOT TextField object. This was tested under win98/JDK1.2.2.
I searched if I had any other TextField class happened to be in the classpath dirs also. No nothing.
Thank you all.
regds
maha anna
<pre>
import java.awt.*;
class Test{
public static void main(String[] arg) {
//Button b = new Button(); This is ok
TextField t1 = new TextField(); // hangs!
TextField t2 = new TextField(10); // hangs!
TextField t3 = new TextField("Java"); // hangs!
TextField t4 = new TextField("Java",10); // hangs!
}
}
</pre>
[This message has been edited by maha anna (edited April 02, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have seen that too. It hangs. However the code runs oK if the
code is modified to
<code>
import java.awt.*;
class TextTest{
TextField t2 = new TextField(10);
TextField t1 = new TextField();
TextField t3 = new TextField("Java");
TextField t4 = new TextField("Java",10);
public static void main(String[] arg)
{Button b = new Button();

}
}
</code>
Does anyone know a clue to this. I would guess it has something to do with the TextComponent class which cannot be instantiated but I dont know why it works if it is a class variable and not a method variable.
[This message has been edited by Howard Stern (edited April 02, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Funny! I have trouble too, but with JTextField!
I'm obliged to replace JTextField of BruceEckel Threads chapter examples with TextField, otherwise no counter shows in JTextField! (Win95).
JRoch

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

JRoch- I'd be interested in hearing which examples you had problems with. I just tried the first one I found in chapter 14 under "Threading for a responsive Interface" - c14:Counter2.java - and it worked fine for me - but I see there are many more. Which one did you see problems with? I'm running jdk 1.2.2 and 1.3 beta under Win 98.
Howard- under your code, TextTest is never instantiated, and so the member fields are never created. None of the TextField constructors are being called. Only the static method main() is executed, and it never creates an instance. So that's why you're seeing different behavior.
Maha- I'm not sure what you mean by "hangs" exactly - maybe we're seeing different things. When I run it, the main() method completes normally. The only unusual thing I see is that the JVM does not exit at this point. Normally this is because whenever a GUI component is displayed, the JVM does not exit when main() completes because there is still an active user thread - namely the event handler thread which processes any events associated with GUI components. Of course, in this case, there is no GUI component actually displayed, and so there is no chance of an event ever occurring as near as I can see. I guess it's a bug in the TextField that an event handling thread keeps going even if the component is not visible, while a Button evidently does not have this problem. But it's not much of a bug - there's no reason to create invisible components normally, and once you create a visible component you want the event handler thread to be there preventing the JVM from exiting.
Does that make sense? Is that the behavior that's bothering you, or was it something else?

"I'm not back." - Bill Harding, Twister
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jim,
Yes. I got the same behaviour as you mentioned. Your post makes sense. I know that when we display a GUI component, an Event handling thread runs to handle user inputs. This is the reason we have to add the WindowListener event handler to a Frame explicitly and override the 'windowclosing(WindowEvent e)' method in order to close the window when we click the 'close' window control.
I also understand that when we create a GUI component we add to GUI interface and go from there. Do you rembember ? Long back I posted a qstn asking about the 'propotional font' concept in a TextField. We both discussed there. But that time we didn't see this because I knew I didn't add a WindowListener so I just press cntl-c and quit. But for this post, I saw in the API that the minimumSize() of TextField and TextArea returns a 'Dimention' object. So I was wondering for TextArea it is ok. I expected that the minimumsize() may return the row and col as width and height of the Dimention object. What about TextField ? It has only variable widht. That means what is the height of the TextField? Will it always be Same for diff size of TextFields having different cols? This thinking triggered me to coin this piece of code and test. But I couldn't make it. Thanks for your reply Jim.
regds
maha anna
[This message has been edited by maha anna (edited April 03, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, I think you can still test this - just ignore the fact that the JVM doesn't actually exit when you're done until you press ctrl-C. You can still print out what the width and height fields of the Dimension minimumSize are. I think that the values will be in pixels, not rows or columns - though those should be somewhat proportional. You may also want to see what happens if you change font size or style. Let us know if anything strange happens.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jim,
Sorry for hanging onto this. You said You can still print out what the width and height fields of the Dimension minimumSize are. I think that the values will be in pixels, not rows or columns - though those should be somewhat proportional. I tested this with various fonts and with various size related methods like getSize(),getMinimumSize(),getMinimumSize(int col),getPreferredSize(..) etc. We get SAME result NOT propotional as you mentioned. Did u want me to get this strange result again?
<pre>
For Button
----------
Monospaced,Font.PLAIN,24 getSize():Width=0Height=0
Monospaced,Font.ITALIC,44 getSize():Width=0Height=0
For TextField
-------------
Monospaced,Font.PLAIN,24 getSize():Width=0Height=0
Monospaced,Font.PLAIN,24 getMinimumSize():Width=0Height=0
Monospaced,Font.PLAIN,24 getMinimumSize(int):Width=0Height=0
Monospaced,Font.PLAIN,24 getPreferred():Width=0Height=0
Monospaced,Font.PLAIN,24 getPreferred(int):Width=0Height=0
Monospaced,Font.PLAIN,44 :Width=0Height=0
Serif,Font.PLAIN,44 :Width=0Height=0
Serif,Font.ITALIC,44 :Width=0Height=0

</pre>
<pre>
import java.awt.*;
class mframe extends Frame{
public mframe(){
super("maha");
setLayout(new FlowLayout());
Button b1 = new Button("Button1");
b1.setFont(new Font("Monospaced",Font.PLAIN,24));
System.out.println("Monospaced,Font.PLAIN,24 getSize():"
+"Width="+b1.getSize().width
+"Height="+b1.getSize().height);
b1.setFont(new Font("Monospaced",Font.ITALIC,44));
System.out.println("Monospaced,Font.ITALIC,44 getSize():"
+"Width="+b1.getSize().width
+"Height="+b1.getSize().height);

TextField tf1 = new TextField("12345",5);
TextField tf2 = new TextField("12345",5);
TextField tf3 = new TextField("12345",5);
TextField tf4 = new TextField("12345",5);
tf1.setFont(new Font("Monospaced",Font.PLAIN,24));
System.out.println("Monospaced,Font.PLAIN,24 getSize():"
+"Width="+tf1.getSize().width
+"Height="+tf1.getSize().height);
System.out.println("Monospaced,Font.PLAIN,24 getMinimumSize():"
+"Width="+tf1.getMinimumSize().width
+"Height="+tf1.getMinimumSize().height);
System.out.println("Monospaced,Font.PLAIN,24 getMinimumSize(int):"
+"Width="+tf1.getMinimumSize(5).width
+"Height="+tf1.getMinimumSize(5).height);
System.out.println("Monospaced,Font.PLAIN,24 getPreferred():"
+"Width="+tf1.getPreferredSize().width
+"Height="+tf1.getPreferredSize().height);
System.out.println("Monospaced,Font.PLAIN,24 getPreferred(int):"
+"Width="+tf1.getPreferredSize(5).width
+"Height="+tf1.getPreferredSize(5).height);

tf2.setFont(new Font("Monospaced",Font.PLAIN,44));
System.out.println("Monospaced,Font.PLAIN,44 :"
+"Width="+tf2.getMinimumSize().width
+"Height="+tf2.getMinimumSize().height);
tf3.setFont(new Font("Serif",Font.PLAIN,44));
System.out.println("Serif,Font.PLAIN,44 :"
+"Width="+tf3.getMinimumSize().width
+"Height="+tf3.getMinimumSize().height);
tf4.setFont(new Font("Serif",Font.ITALIC,44));
System.out.println("Serif,Font.ITALIC,44 :"
+"Width="+tf3.getMinimumSize().width
+"Height="+tf3.getMinimumSize().height);
add(b1);
add(tf1);
add(tf2);
add(tf3);
add(tf4);
setSize(200,100);
setVisible(true);
}
public static void main(String args[]){
new mframe();
}
}
</pre>
regds
maha anna
[I split up some of the longer lines of the program to avoid forcing a scrollbar to appear on the browser - Jim]
[This message has been edited by Jim Yingst (edited April 04, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Jim,
It is BruceEckel c14:Counter1.java example.
I'm running under JAVA version .. wait a second ..
jdk1.2.2
Under Win95.
I discovered that JTextField doesn't work properly. At first I thought it was freezing, but in fact, the THREAD counter was indeed looping its loop, but silently, without obbeying the display statement.
while (true){
t.setText(i); // if JTextField, doesn't work. But TextField OK!
Thread.sleep(1000);
}
Replacing JTextField by TextField solved this oddity!
JRoch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
JRoch- interesting. I originally skipped Counter1 since it looked like it was intended as an example of "how not to make a responsive GUI". But I see now from Eckel's comments that it was still at least supposed to display the counter, even if it did ignore the user inputs. Strange that TextField would work there while JTextField does not. Note that JTextField works fine in Counter2, where Eckel has placed the counter update code in a separate thread. So the problem seems to be that JTextField can't update (at least on our platforms) until the event handling thread is freed up again and able to respond. In Counter1 the go() method is originally called from an actionPerformed() method and never returns, which is why no further events can be handled. Many GUI components have troubles in this situation; I haven't paid a lot of attention to exactly which functions of which components are dependent on the event processing thread in order to function - some are, and some aren't. It's strange that JTextField's visual update does need the event handling thread, and TextField's update does not. But I guess the point is that for most GUI components to work properly we need to put all time-consuming tasks in separate threads - as long as we do this, JTextField works fine it appears.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Maha- it looks like all the Size attributes of a Component are zero until the component is added to a Container and the conatiner is made visible. When this is done, then the sizes are indeed related to the font size and name (though not to style that I've seen so far). Try the following:

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thanks a lot Jim.
regds
maha anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does this piece of code hang for you all also ?