I am trying to create a Toaster Window to display a small piece of text for
informative purposes in my GUI. My code is below: the window shows fine but
I want it to disappear after a given time, however the timer never fires,
can anybody tell me why?
Also, is there a way to make the text area appear to be maximised within the
JFrame?
Thanks
Allan
/*
* ToasterWindow.java
*
* Created on 12 March 2006, 23:05
*/
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A toaster window is a small window that displays some text at the bottom
* right of the screen.
* @author Allan M. Bruce
*/
public class ToasterWindow extends JFrame
{
private String mText;
private static final int WIDTH = 150,
HEIGHT = 100,
Y_OFFSET = 25,
DISPLAY_TIME = 5000;
private Timer mTimer;
private void createWindow()
{
// set position
Dimension lScrnSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(lScrnSize.width - WIDTH,
lScrnSize.height - HEIGHT-Y_OFFSET);
setSize(WIDTH, HEIGHT);
toFront();
// add the text area
getContentPane().setLayout(new BorderLayout());
JPanel lPanel = new JPanel();
getContentPane().add(lPanel, BorderLayout.CENTER);
JTextArea lTextArea = new JTextArea();
lTextArea.setText(mText);
lTextArea.setEditable(false);
lPanel.add(lTextArea);
// set visible
setVisible(true);
}
private void destroy()
{
mTimer.stop();
hide();
}
/**
* Creates a new instance of ToasterWindow
* @param xiText Text to display inside toaster window
*/
public ToasterWindow(String xiText)
{
super();
mText = xiText;
createWindow();
mTimer = new Timer(DISPLAY_TIME, new ActionListener()
{
public void actionPerformed(ActionEvent xiEvent)
{
System.out.println("Timer Fired"); // temp
destroy();
}
});
}
}
klynn47@comcast.net - 14 Mar 2006 00:26 GMT
Where do you start the Timer?
Allan M. Bruce - 14 Mar 2006 00:32 GMT
> Where do you start the Timer?
Ok, how stupid am I? Thanks - that was the problem.
Now, about making that text area fill the JFrame??
Allan
Knute Johnson - 14 Mar 2006 03:50 GMT
>> Where do you start the Timer?
>
[quoted text clipped - 3 lines]
>
> Allan
Lose the JPanel and just add your JTextArea to the JFrame.
BorderLayout causes the components in it to be expanded to fill the
available space. Your JPanel fills the inside of your JFrame. The
JPanel defaults to a FlowLayout. FlowLayout doesn't expand the
components that it lays out so your JTextArea doesn't fill the available
space. So lose the JPanel or change its layout manager.

Signature
Knute Johnson
email s/nospam/knute/
Roedy Green - 14 Mar 2006 00:31 GMT
On Mon, 13 Mar 2006 22:31:10 -0000, "Allan M. Bruce"
<allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
someone who said :
> My code is below: the window shows fine but
>I want it to disappear after a given time, however the timer never fires,
>can anybody tell me why?
see http://mindprod.com/jgloss/timer.html for how to use timers.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 14 Mar 2006 02:14 GMT
On Mon, 13 Mar 2006 22:31:10 -0000, "Allan M. Bruce"
<allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
someone who said :
> this.setLocation(lScrnSize.width - WIDTH,
> lScrnSize.height - HEIGHT-Y_OFFSET);
> setSize(WIDTH, HEIGHT);
That is coding in C. Only layout managers should ever use those
methods.
See http://mindprod.com/jgloss/layout.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Allan M. Bruce - 14 Mar 2006 10:54 GMT
> On Mon, 13 Mar 2006 22:31:10 -0000, "Allan M. Bruce"
> <allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
[quoted text clipped - 8 lines]
>
> See http://mindprod.com/jgloss/layout.html
And how else do you suggest I position my window in the bottom right of my
screen?
Thanks
Allan
Monique Y. Mudama - 15 Mar 2006 00:19 GMT
>> On Mon, 13 Mar 2006 22:31:10 -0000, "Allan M. Bruce"
>> <allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
[quoted text clipped - 11 lines]
> And how else do you suggest I position my window in the bottom right of my
> screen?
You could use GridBagLayout and a variety of GridBagConstraints
settings ...
There's probably a way to do this in any number of layout managers.
That's just one example.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 15 Mar 2006 03:22 GMT
On Tue, 14 Mar 2006 09:54:22 -0000, "Allan M. Bruce"
<allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
someone who said :
>And how else do you suggest I position my window in the bottom right of my
>screen?
It is not really your business. You should be remembering the
location from last time and putting it there.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 15 Mar 2006 03:36 GMT
On Tue, 14 Mar 2006 09:54:22 -0000, "Allan M. Bruce"
<allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
someone who said :
>>> this.setLocation(lScrnSize.width - WIDTH,
>>> lScrnSize.height - HEIGHT-Y_OFFSET);
>>> setSize(WIDTH, HEIGHT);
to do that sort of thing for the entire frame there is no other way
that setLocation. However, for anything INSIDE the frame, you should
be using a layout manager.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Allan M. Bruce - 15 Mar 2006 11:13 GMT
> On Tue, 14 Mar 2006 09:54:22 -0000, "Allan M. Bruce"
> <allanmb@takeaway.dsl.pipex.com> wrote, quoted or indirectly quoted
[quoted text clipped - 7 lines]
> that setLocation. However, for anything INSIDE the frame, you should
> be using a layout manager.
This is purely for the whole JFrame - thats why I used setLocation().
Allan