Solved

JFrame Resize Event

Posted on 2006-06-26
10
6,149 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:mayankeagle
mayankeagle 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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now