componentResized() - Linux

Hello,

I've been trying to detect when a JFrame is resized by using a component listener:

publicclass myClassextends JFrame{

public myClass(){

addComponentListener(new ComponentAdapter(){

publicvoid componentResized(ComponentEvent e){

System.out.println("HERE");

}

});

}

This works in Windows but not in Linux (JDK 6.0 for both). I replaced componentResized() with other ComponentAdapter methods and they all work fine.

Has anyone experienced this before? Is there a workaround?

Thanks for your help!

Anna

[1034 byte] By [annachua] at [2007-11-27 9:48:52]
# 1

Works ok for me on Fedora 7 with java 1.6.0_01. I would avoid using the adapters and just use the listeners. It's nice in that you don't need to implement every method in the listener, but if you mistype your method, there is no way to know there was a typo using the compiler. I don't see any typos in what you posted, but i don't know if you just typed this example or if it is the original code.

I suggest changing ComponentAdapter to ComponentListener and implement each method, even if it is empty. If componentResized is misspelled or something else is out of place, compiling with ComponentListener will tell you what is wrong.

Although this won't explain why it works in Windows and not Linux...case sensitivity in the class file name...idk.

What version of Linux?

robtafta at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for your reply.I'm using Ubuntu-Feisty Fawn.I actually did try implementing ComponentListener as well, but the same thing happened :(
annachua at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 3

I'm running Feisty Fawn and using the ComponentListener() is working perfectly for me. There's going to be a great deal of chatter but you're going to get that no matter how you go about it.

PS.

Show me the code where you're implementing the listener.

Message was edited by:

puckstopper31

puckstopper31a at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 4

Hmm...interesting.

Here's the code:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Temp extends JFrame {

private JFrame frame;

private JPanel panel;

public Temp() {

frame = this;

this.addComponentListener(new ComponentAdapter() {

public void componentResized(ComponentEvent e){

panel.repaint();

}

});

this.setBackground(Color.WHITE);

this.add(panel());

this.pack();

this.setSize(new Dimension(400, 400));

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setVisible(true);

}

private JPanel panel() {

panel = new JPanel() {

public void paintComponent(Graphics g) {

int xValue = (int) (frame.getSize().width * 0.15);

int yValue = (int) (frame.getSize().height * 0.15);

g.setColor(Color.BLACK);

g.drawRect(0, 0, xValue, yValue);

g.dispose();

}

};

return panel;

}

public static void main(String args[]){

new Temp();

}

}

Thanks again!

annachua at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 5
I am curious as to why you need to call repaint in this listener. doLayout and paint should both get called when you resize the frame.
robtafta at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 6
Hi,I tried it without calling repaint() before and it didn't work out, even in Windows.
annachua at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 7

package com.fw.sandbox.frameresize;

import javax.swing.*;

import java.awt.* ;

import java.awt.event.* ;

public class ResizeableFrame extends JFrame{

private JPanel panel ;

public ResizeableFrame(){

super() ;

init() ;

initComponents() ;

}

private void init(){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e){

JOptionPane.showMessageDialog(this, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE) ;

}

setTitle("Resizeable Frame Test") ;

getContentPane().setLayout(new BorderLayout()) ;

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addComponentListener(new ComponentListener(){

public void componentHidden(ComponentEvent e){

System.out.println("Hiding the frame") ;

}

public void componentMoved(ComponentEvent e){

System.out.println("Moving the frame") ;

}

public void componentResized(ComponentEvent e){

System.out.println("Resizing the frame") ;

}

public void componentShown(ComponentEvent e){

System.out.println("Showing the frame") ;

}

}) ;

}

private void initComponents(){

panel = new JPanel(new FlowLayout(FlowLayout.CENTER)) ;

panel.setPreferredSize(new Dimension(640, 480)) ;

panel.add(new JLabel("this is an example frame.")) ;

getContentPane().add(panel, BorderLayout.CENTER) ;

pack() ;

}

public void centerAndShow(){

Dimension d = Toolkit.getDefaultToolkit().getScreenSize() ;

int xPos = (int) (d.getWidth() - getWidth())/2 ;

int yPos = (int) (d.getHeight() - getHeight())/2 ;

setLocation(new Point(xPos, yPos)) ;

setVisible(true) ;

}

}

Here is an example of how I did it. It works equally well in either Linux or Windows.

PS.

Addendum: The behavior of this code is somewhat different between Windows and Linux. In particular the resize event. On Linux the resize event is triggered any time the frame is resized. On Windows if you drag one of the borders it only fires the resize when you drop it.

Message was edited by:

puckstopper31

puckstopper31a at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 8

Thanks for putting up that sample code :)

Unfortunately it still doesn't work for me! When I resize the window in Linux, I only get "Moving the frame" and never "Resizing the frame". Actually, "Resizing the frame" does show up once when the program is initially run. But after that one occurrence, it doesn't happen again. The same thing happened for the code that I put up earlier.

Perhaps there's something wrong with the way things are set up on my computer; you mentioned that you're using Ubuntu Feisty Fawn and Java 1.6.0_01 as well.

Do you have any idea to what it might be? I'm planning to reinstall my computer anyway, so maybe this will go away, but it would be nice to know why things turn out the way they do.

Thanks!

Anna

annachua at 2007-7-13 0:17:28 > top of Java-index,Java Essentials,Java Programming...
# 9

Working from a different angle...

If you are just repainting in that method, maybe we could figure out why it won't repaint without calling repaint, it should repaint automatically in 1.6.

I ran the sample code you posted, and as I drag the frame, the rectangle is redrawn every time the frame size changes. I don't have to let go of the frame in Windows, and it still updates the GUI. However, java 1.5 has different behavior. It only repaints when the frame is dropped, but it still repaints. In 1.4.2, it paints the old and new rectangle, so I see multiple rectangles after I drop the frame.

Does the sample code not repaint as well when you drag the frame?

robtafta at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 10

When I run my code in Windows, the rectangle updates as I'm dragging the window. In Linux it doesn't even get to the point of repainting because it doesn't detect the resize.

Let's say there was no repaint() in there, but System.out.println("Hello") instead. "Hello" would never be called when I run it in Linux.

annachua at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 11

add this to the beginning of Temp:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueue() {

protected void dispatchEvent(AWTEvent event) {

super.dispatchEvent(event);

System.out.println(event.getClass());

}

});

While resizing the window, you should see:

class java.awt.event.InvocationEvent

class sun.awt.event.IgnorePaintEvent

class sun.awt.PeerEvent

and when you drop the window, you should see

class java.awt.event.ComponentEvent

You could try listening for that PeerEvent and repaint then, I'm not sure if it will cause any side effects, but it will work if you are seeing PeerEvents,

robtafta at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 12
annachu, I ran the mini Temp application that you posted in Reply 4. I'm using Java 5 on Red Hat EL v 4 and I can see the little rectangle in the top left corner being resized as I am resizing the frame. So the listener appears to be working on my linux os.
nantucketa at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks for all the help :)I tried out what you suggested, but I didn't get any PeerEvents :(...Instead, when I clicked on the frame, I got a ComponentEvent and when I dragged, I got IgnorePaintEvents. When I let go, I didn't get anything at all.Curious....
annachua at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 14
Thanks for your input, nantuket. Right now, I'm still guessing that it's only me who has this problem and hopefully it will automagically go away after I do my reinstall.Would it make any difference that I'm programming in Eclipse?
annachua at 2007-7-13 0:17:29 > top of Java-index,Java Essentials,Java Programming...
# 15

> Thanks for your input, nantuket. Right now, I'm

> still guessing that it's only me who has this problem

> and hopefully it will automagically go away after I

> do my reinstall.

That would be helpful for you. I hope it works itself out easily enough whatever the trouble is.

> Would it make any difference that I'm programming in

> Eclipse?

Curious...I don't know. But for the sake of discussion I'll mention that I just used vi and command line to save, compile and run it. So I'm not usng eclipse. I don't know what variables eclipse would introduce.

nantucketa at 2007-7-21 23:09:55 > top of Java-index,Java Essentials,Java Programming...
# 16

No eclipse won't effect it, unless maybe you are running it from eclipse, that could effect which JRE is being used.

What Desktop Environment are you using? KDE, Gnome, another?

If you try another DE, does it still act the same way?

have you been doing yum updates on the box? (if ubuntu supports yum, I'm sure it does) It's possible you have a bad version of one of the DEs.

robtafta at 2007-7-21 23:09:55 > top of Java-index,Java Essentials,Java Programming...
# 17

> No eclipse won't effect it, unless maybe you are

> running it from eclipse, that could effect which JRE

> is being used.

That's a good point. annachu, take a quick look through this thread and see if anything there can help. It's possible you might not have java fully configured.

http://forum.java.sun.com/thread.jspa?threadID=5191319

nantucketa at 2007-7-21 23:09:55 > top of Java-index,Java Essentials,Java Programming...
# 18

Hello!

I reinstalled my OS (along with everything else) and the frame resizing can now be detected!

I'm still not sure what might have caused the problem in the first place (...perhaps I tampered with some files that I shouldn't have).

Anyway, thank you all for your help!

Have a nice day :)

Anna

annachua at 2007-7-21 23:09:55 > top of Java-index,Java Essentials,Java Programming...
# 19
You're welcome. Glad you got it working.
nantucketa at 2007-7-21 23:09:55 > top of Java-index,Java Essentials,Java Programming...