How can you detect when a JDialog has been resized? (WindowListener would have been my guess, but that doesn't seem to have a resizing event handler.)
Try using a ComponentListener:
YourDialog.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
// do stuff
}
});
Related
Here is a piece of code that I wrote but the close button on the top of the application doesn't work please help
Code:
import java.awt.*;
import java.awt.event.*;
public class App extends Frame implements MouseMotionListener {
App() {
addMouseMotionListener(this);
setSize(200, 200);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.RED);
g.fillRect(e.getX(), e.getY(),10, 10);
}
public void mouseMoved(MouseEvent e) {
}
public static void main(String[] args)throws Exception {
App a = new App();
}
}
Image:
You need to add Listener and call dispose while initializing your applet.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
You need to add above line of code inside the constructor.
Try it with that little code:
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
You have to insert it in "App()". It will close the program "System.exit(0);"
when you press the close button.
im not using AWT often but heres my Solution :)
addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Add a new WindowAdapter to your App Constructor and call system.exit(0) on the Window Close Event
You can use WindowContants on JFrame#setDefaultCloseOperation to achieve the desired action. This allows you to dispose the frame, entirely terminate the application and a few more with a single line in the constructor:
this.setDefaultCloseOperation(WindowContants.DISPOSE_ON_CLOSE);
will dispose the frame containing the app. This is sufficient to terminate the program you presented.
This approach doesn't allow handling any events though, but simply closes the frame.
What code is called when a JFrame is minimized? Is it hooked up to a listener? I just want to know what happens internally when the frame is minimized.
EDIT:
Im actually looking for the code that is called when the frame is minimized. For example, the code for the actual windowListener. Ive been searching through JFrame, Frame, and Window searching for windowIconified but have been unable to find the actual code.
Reason being, when my program runs, it has a small defect with one of the Panels, but when I minimize and maximize the JFrame, the problem goes away. I wanted to see what was going on so that I can apply whatever is going on to my Panel so it paints right.
you can listening by using WindowListener
for example
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class WinStateListener implements WindowListener {
static JFrame window = new JFrame("Window State Listener");
public WinStateListener() {
window.setBounds(30, 30, 300, 300);
window.addWindowListener(this);
window.setVisible(true);
}
public static void main(String[] args) {
WinStateListener winStateListener = new WinStateListener();
}
public void windowClosing(WindowEvent e) {
System.out.println("Closing");
window.dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {
System.out.println("Opened");
}
public void windowClosed(WindowEvent e) {
System.out.println("Closed");
}
public void windowIconified(WindowEvent e) {
System.out.println("Iconified");
}
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
}
public void windowActivated(WindowEvent e) {
System.out.println("Activated");
}
public void windowDeactivated(WindowEvent e) {
System.out.println("Deactivated");
}
}
You want to read about WindowListeners and WindowEvents. The event you are talking about is called Iconifying the window. Read more here:
http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html
EDIT:
Use revalidate() then repaint() on the JPanel that is acting up.
When minimizing the JFrame application a window event windowIconified is called. If you want to process such window events by your own then either implement WindowListener interface or use WindowAdapter abstract class.
What code is called when a JFrame is minimized?
As noted in How to Make Frames: Specifying Window Decorations, "window decorations are supplied by the native window system." The article goes on to describe some changes you can make to the host platform's default.
Addendum: Reading your update, note that restoring an iconified window repaints it. As #Andrew Thompson points out, you may need to verify that you're building on the event dispatch thread. You may also need to schedule a repaint(). An sscce might clarify things.
I'm making a program with a logger. The logger has its own JFrame.
I'm trying to override the reaction from clicking on the minimize-button of that frame.
I would like the frame to either setVisible(false) or do the defaultCloseOperation (as i set that to hide earlier).
How should I do this?
Thanks in advance
Use a JDialog instead of a JFrame. JDialogs don't have a minimize button.
You can add a WindowListener and add a iconified handler that will react when the window is minimized.
Maybe:
frame.addWindowListener(new WindowAdapter(){
public void windowIconified(WindowEvent e){
frame.setVisible(false);
}
});
You can use the WindowStateListener like this
f.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent arg0) {
if (arg0.getNewState() == Frame.ICONIFIED) {
// do stuff
}
}
});
Try this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent event)
{
//do your stuff
}
});
What listener should I register in a JFrame instance to be notified if a modal JDialog is shown on top of the frame (the frame is the owner of the dialog)? Thanks in advance.
I think JFrame.addWindowListener(...) would work and then pay attention to WindowListener.windowDeactivated(...)
ETA:
jFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowDeactivated(WindowEvent e) {
if(e.getOppositeWindow() instanceof JDialog) {
JDialog dialog = (JDialog) e.getOppositeWindow();
if(dialog.isModal()) {
// do stuff
}
}
}
});
I have a JTextField with a documentListener on it. I want to change the background color when I add or remove characters to this textfield. I should be using a document listener correct? It works, but it also fires when I gain and lose focus on this JTextfield, which is undesired. I do not add a focus listener on this JTextField. Here is my code, any suggestions on how I can fix my problem?
numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);
numPlotRowsJTextField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e)
{
}
#Override
public void insertUpdate(DocumentEvent e)
{
numPlotRowsJTextField.setBackground(Color.cyan);
}
#Override
public void changedUpdate(DocumentEvent e)
{
}
});
Also note that I am using JGoodies Binding which I am starting to believe is the root of this problem. Swing w/o JGoodies shouldn't be firing off document listener events by changing focus...
You must have something looking at the focus or you think it is firing and it is not.
I took your code and made a complete example and it does not have the problem you describe.
JFrame frame = new JFrame();
final JTextField numPlotRowsJTextField = new JTextField(3);
numPlotRowsJTextField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void changedUpdate(DocumentEvent e) {
}
#Override
public void insertUpdate(DocumentEvent e) {
numPlotRowsJTextField.setBackground(Color.cyan);
}
#Override
public void removeUpdate(DocumentEvent e) {
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JTextField(2));
frame.getContentPane().add(numPlotRowsJTextField);
frame.setVisible(true);
Have you looked at the DocumentEvent to see what information it contains? Does it actually contain a string that has changed. Or is it just an event with a 0 length string. If it is the latter then maybe you can just ignore that case.
I figured it out. It 100% had to do with JGoodies Binding.
This code works:
ValueModel valueModelNumberPlotRowsJTextField = adapter.getBufferedModel("numberOfPlotRows");
valueModelNumberPlotRowsJTextField.addValueChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt)
{
numPlotRowsJTextField.setBackground(Color.cyan);
}
});
numPlotRowsJTextField = BasicComponentFactory.createIntegerField(valueModelNumberPlotRowsJTextField);
Since I am using JGoodies Binding, I have a ValueModel backing my JTextField. The listener has to be set there and not on the JTextField.