Solved

JFrame Resize Event

Posted on 2006-06-26
10
6,272 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

635 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