Solved

JFrame Resize Event

Posted on 2006-06-26
10
6,193 Views
Last Modified: 2013-11-23
Hi, I am designing a GUI with my own custom JTable which has a method to resize the columns in the table, such that the 2nd column fills all of the space that the JTable has available. I have added a listener to the main JFrame to detect when it is resized so I can call my resize method.

I'm aware that you can set the .setDynamicLayout() to true for the default toolkit to enable components to be resized and moved according to the layout manager as the frame is being resized (instead of just when the user releases the mouse button after a resize)

My problem is that the listener I have registered only picks up resize events fired after the user releases the mouse button after a resize. Is there a way to call my .resize() method dynamically as the JFrame is being resized?
0
Comment
Question by:frube
  • 2
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 43 total points
ID: 16986705
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import javax.accessibility.*;

public class Z extends JFrame implements Runnable, ActionListener, ComponentListener {
  JLabel jl = new JLabel("0:0", JLabel.CENTER);
  public void init() {
    add(jl);
    addComponentListener(this);
    pack();
    center(this);
  }
  public void componentHidden(ComponentEvent e){}
  public void componentMoved(ComponentEvent e) {
    jl.setText(getWidth() + ":" + getHeight());
    validate();
  }
  public void componentResized(ComponentEvent e) {
    jl.setText(getWidth() + ":" + getHeight());
    validate();
  }
  public void componentShown(ComponentEvent e) {}
  public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() instanceof JMenuItem) {
      JMenuItem jmi = (JMenuItem)ae.getSource();
      for(UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
        if(laf.getName().equals(jmi.getText())) {
          try {
            UIManager.setLookAndFeel(laf.getClassName());
          } catch(Exception e) {
            e.printStackTrace();
          }
          SwingUtilities.updateComponentTreeUI(this);
          pack();
          center(this);
          break;
        }
      }
    }
  }
  public void run() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();
    JMenu jm = new JMenu("Look&Feel");
    for(UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
      JMenuItem jmi = new JMenuItem(laf.getName());
      jmi.addActionListener(this);
      jm.add(jmi);
    }
    jmb.add(jm);
    setLayout(new BorderLayout());
    add(jmb, BorderLayout.NORTH);
    init();
    setVisible(true);
  }
  public void center(Component c) {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dim.width - c.getWidth()) / 2, (dim.height - c.getHeight()) / 2);
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Z());
  }
}

This partially works, but I think this functionality is not implemented in Java. Also I may be wrong, but if I'm not then there is a good idea to request for enhancment the java community
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 41 total points
ID: 16986842
0
 
LVL 2

Author Comment

by:frube
ID: 16987359
To StillUnAware; Thanks, but I compiled that code you supplied and it doesn't seem to work at all, the components are repositioned after you release the mouse click (which is not what I was looking for, see original post.) I do hope this is possible, it would make the effect on the table is currently quite ugly while being resized, the scrollbars come into view and disappear again when my resize() method is called, it's not very professional-looking.

To hoomanv; Thank you, but I'm not sure if you read my question in full, I am already using dynamic layout for the frame, the problem is calling a method continuously each time the layout is adjusted dynamically. The link you provided doesn’t suggest whether this can or can not be achieved.
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 14

Expert Comment

by:hoomanv
ID: 16988023
AFAIK no pure java way is available to trap the resizing event
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 41 total points
ID: 16989554
You can try with a WindowListener.
0
 
LVL 2

Author Comment

by:frube
ID: 16993964
To mayankeagle, if you read the first paragraph, quoting myself here; "I have added a listener to the main JFrame to detect when it is resized so I can call my resize method"
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17080191
Yes but you didn't mention what type of listener it is and you didn't post any code sample as to how you are using it.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

810 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