Solved

JFrame Resize Event

Posted on 2006-06-26
10
6,226 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 71
mysql jsp example issue 32 70
junit as external jar or library 7 37
Java class and jar 3 41
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

756 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