If I create a new shell using the following code:
shell = new Shell( Display.getDefault(), SWT.RESIZE);
Then this gives me a shell without a title bar or minimize / maximize buttons, which is what I want. I'm able to resize this window to any size, which works great. But the problem is, the window is fixed in its place, and I cannot move it by dragging it around.
If I add either SWT.CASCADE or SWT.CLOSE, this gives me the title bar and close button, which I don't want, but moreover, it puts a limit on how small the window can be resized, i.e I can't resize it horizontally past a certain limit.
How can I make the window moveable without the close button / title bar? If there's no native way in SWT to do it, can I do it by listening for a mouse drag event and manually setting the location of the shell? If so, how would I get the mouse coordinates from the movement of the mouse?
Help would be appreciated. Thanks!
You need use own listeners. Below code should help:-
public class Demo {
static Boolean blnMouseDown=false;
static int xPos=0;
static int yPos=0;
public static void main(final String[] args) {
Display display=new Display();
final Shell shell = new Shell( Display.getDefault(), SWT.RESIZE);
shell.open();
shell.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent arg0) {
// TODO Auto-generated method stub
blnMouseDown=false;
}
#Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
blnMouseDown=true;
xPos=e.x;
yPos=e.y;
}
#Override
public void mouseDoubleClick(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
shell.addMouseMoveListener(new MouseMoveListener() {
#Override
public void mouseMove(MouseEvent e) {
// TODO Auto-generated method stub
if(blnMouseDown){
shell.setLocation(shell.getLocation().x+(e.x-xPos),shell.getLocation().y+(e.y-yPos));
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.close();
}
}
This is my implementation:
/**
* Class to allow user to move a shell without a title.
*
* #author Laurent Muller
* #version 1.0
*/
public class MoveShellListener implements Listener {
/*
* the parent shell
*/
private final Shell parent;
/*
* the mouse down location
*/
private Point ptMouseDown;
/**
* Creates a new instance of this class.
*
* #param parent
* the shell to handle.
*/
public MoveShellListener(final Shell parent) {
if (parent == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (parent.isDisposed()) {
SWT.error(SWT.ERROR_WIDGET_DISPOSED);
}
// copy and add listener
this.parent = parent;
addControl(parent);
}
/**
* Adds the given control to the list of listened controls. If the given
* control is an instance of {#link Composite}, the children controls are
* also added.
*
* #param control
* the control to add.
*/
public void addControl(final Control control) {
// check control
if (isDisposed(control) || control.getShell() != parent) {
return;
}
// add listeners
control.addListener(SWT.MouseDown, this);
control.addListener(SWT.MouseUp, this);
control.addListener(SWT.MouseMove, this);
// children
if (control instanceof Composite) {
final Control[] children = ((Composite) control).getChildren();
for (final Control child : children) {
addControl(child);
}
}
}
/**
* Adds the given controls to the list of listened controls. If one of the
* given controls is an instance of {#link Composite}, the children controls
* are also added.
*
* #param controls
* the controls to add.
*/
public void addControls(final Control... controls) {
if (controls != null) {
for (final Control control : controls) {
addControl(control);
}
}
}
/**
* {#inheritDoc}
*/
#Override
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.MouseDown:
onMouseDown(e);
break;
case SWT.MouseUp:
onMouseUp(e);
break;
case SWT.MouseMove:
onMouseMove(e);
break;
}
}
/**
* Removes the given control to the list of listened controls. If the given
* control is an instance of {#link Composite}, the children controls are
* also removed.
*
* #param control
* the control to remove.
*/
public void removeControl(final Control control) {
// check control
if (control == parent || isDisposed(control)
|| control.getShell() != parent) {
return;
}
// remove listeners
control.removeListener(SWT.MouseDown, this);
control.removeListener(SWT.MouseUp, this);
control.removeListener(SWT.MouseMove, this);
// children
if (control instanceof Composite) {
final Control[] children = ((Composite) control).getChildren();
for (final Control child : children) {
removeControl(child);
}
}
}
/**
* Removes the given controls to the list of listened controls. If one of
* the given controls is an instance of {#link Composite}, the children
* controls are also removed.
*
* #param controls
* the controls to remove.
*/
public void removeControls(final Control... controls) {
if (controls != null) {
for (final Control control : controls) {
removeControl(control);
}
}
}
/**
* Checks if the given control is null or disposed.
*
* #param control
* the control to verify.
* #return true if the control is null or
* disposed.
*/
private boolean isDisposed(final Control control) {
return control == null || control.isDisposed();
}
/**
* Handles the mouse down event.
*
* #param e
* the event data.
*/
private void onMouseDown(final Event e) {
if (e.button == 1) {
ptMouseDown = new Point(e.x, e.y);
}
}
/**
* Handles the mouse move event.
*
* #param e
* the event data.
*/
private void onMouseMove(final Event e) {
if (ptMouseDown != null) {
final Point location = parent.getLocation();
location.x += e.x - ptMouseDown.x;
location.y += e.y - ptMouseDown.y;
parent.setLocation(location);
}
}
/**
* Handles the mouse up event.
*
* #param e
* the event data.
*/
private void onMouseUp(final Event e) {
ptMouseDown = null;
}
}
Related
I am creating a window with JavaFX, using an object oriented approach which will allow different kinds of windows to be created with the same basic framework. These windows will display html content.
I have run into a concurrency problem when trying to add an observer.
Window:
public Window(String linkName, String fName, String wName, int width, int height, GuiObserver o) throws IOException {
wApp = new WindowApp();
wApp.setObserver(o);
new Thread(new Runnable() {
public void run() {
wApp.launch(WindowApp.class,linkName,fName,wName,""+width,""+height);
/*try {
wApp.launch(WindowApp.class,linkName,fName,wName,""+width,""+height);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}).start();
}
This creates the window and sets the observer, but puts the launch into a new thread. I can confirm that the observer is added, but the observer cannot be notified. Here is the WindowApp class:
/**
* Instantiate the class with an html file
* THIS MUST NOT TAKE ANY PARAMETERS
*/
public WindowApp() throws IOException {}
#Override
public void start(Stage s) throws Exception {
this.stage = s;
//get the parameters
List<String> params = getParameters().getRaw();
String linkName = params.get(0);
updateHtmlFromFile(new File(params.get(1)));
windowName = params.get(2);
windowWidth = Integer.parseInt(params.get(3));
windowHeight = Integer.parseInt(params.get(4));
//create the window
stage.setWidth(windowWidth);
stage.setHeight(windowHeight);
Scene scene = new Scene(new Group());
browser = new WebView();
webEngine = browser.getEngine();
webEngine.setJavaScriptEnabled(true);
//add the link which will bind Java and Javascript functionality
Class jC = Class.forName(linkName);
Constructor jCon = jC.getConstructor(browser.getClass(),webEngine.getClass(),stage.getClass());
javascriptLink = (JavascriptLink) jCon.newInstance(browser,webEngine,stage);
javascriptLink.setObserver(gObserver);
//javascriptLink = new JavascriptLink(browser,webEngine,stage);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
webEngine.getLoadWorker().stateProperty()
.addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
stage.setTitle(windowName);
}
}
});
webEngine.load(htmlUrl);
scene.setRoot(scrollPane);
stage.setScene(scene);
stage.show();
}
/**
* Add an observer to the javascript Link
* #param o
*/
public void setObserver(GuiObserver o) {
gObserver = o;
}
This object also has a "Javascript link" which interacts with the HTML document after its launch:
package gui;
import java.io.FileNotFoundException;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class MainWindowJSLink extends JavascriptLink {
private GuiObserver gObserver;
/**
* This will be called by WindowApp when appropriate, and does not need to be called by a person
* #param b
* #param we
* #param st
* #throws FileNotFoundException
*/
public MainWindowJSLink(WebView b, WebEngine we, Stage st) throws FileNotFoundException {
super(b, we, st);
}
#Override
public void eventTriggered(String eventName) {
System.out.println("Event triggered -> "+eventName);
switch (eventName) {
case "codeEditorContentsChanged":
handleCodeEditorContentsChanged();
break;
}
//notify observers if necessary
this.triggerObserver(eventName, null);
}
/**
* Handle the event that the contents of the code editor have changed
* This will probably setup a timer to wait a few seconds until the user has stopped typing,
* then send the contents to the syntax highlighter and get the result back
*/
private void handleCodeEditorContentsChanged() {
//System.out.println(getJSCodeEditorContents());
}
/**
* Get the raw contents of the code editor (unedited and not stripped of html entities)
* #return
*/
public String getJSCodeEditorContents() {
String contents = (String) super.webEngine.executeScript("getCodeEditorContents()");
return contents;
}
/**
* Set the contents of the code editor
* #param contents String
* NOTE:
*/
public void setJSCodeEditorContents(String contents) {
super.webEngine.executeScript("setCodeEditorContents(`"+contents+"`)");
}
//observer observable stuff
#Override
public void setObserver(GuiObserver o) {
gObserver = o;
gObserver.setJS(this);
System.out.println("MainWindow -> observer set");
}
#Override
public void triggerObserver(String s, Object o) {
gObserver.trigger(s,o);
}
#Override
/**
* Handle information sent from the
*/
public void send(String s, Object o) {
//handle information sent to this object
}
}
Unfortunately, the gObserver.trigger does not execute. Interestingly enough, if I put a System.exit(0) before that trigger is called, the program exits. However, if I put it after it is called (or in the corresponding method), it is not.
Can anyone offer any advice on how to fix this concurrency issue? If you need more information, please let me know, and I will provide it.
I have designed a checkbox treeviewer using jface but when i run the program it doesnot display any thing on the UI.I am not able to find the where the issue is.Please help me whrer the problem is as I am new to jface.
private void createTreeMenu(Composite parentComposite){
/*treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));*/
Composite treeMenu = new Composite(parentComposite, SWT.BORDER);
treeMenu.setLayout(new GridLayout(1, false));
treeMenu.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
/*tree = new Tree (treeMenu, SWT.MULTI | SWT.CHECK |SWT.VIRTUAL );
GridData treeGD = new GridData(SWT.FILL, GridData.FILL, true, true);
tree.setLayoutData(treeGD);*/
CheckboxTreeViewer treeViewer=new CheckboxTreeViewer(tree);
treeViewer.setContentProvider(new FileTreeContentProvider());
treeViewer.setLabelProvider(new FileTreeLabelProvider1());
treeViewer.setInput(treeItem);
parameters.add("root");
/*treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));*/
tree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if(e.item == null)
{
return;
}
treeSelection = tree.getSelection();
}
});
treeMenu.addListener(SWT.MouseDown, new Listener() {
#Override
public void handleEvent(Event event) {
tree.setSelection(new TreeItem[] {});
treeSelection = null;
}
});
final Menu menu = new Menu(tree);
tree.setMenu(menu);
loadMenuDetails(menu);
}
public TreeItem getInput()
{
treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));
return treeItem;
}
/**
* This class provides the content for the tree in FileTree
*/
class FileTreeContentProvider implements ITreeContentProvider {
/**
* Gets the children of the specified object
*
* #param arg0
* the parent object
* #return Object[]
*/
public Object[] getChildren(Object arg0) {
// Return the files and subdirectories in this directory
//return ((File) arg0).listFiles();
return null;
}
/**
* Gets the parent of the specified object
*
* #param arg0
* the object
* #return Object
*/
public Object getParent(Object arg0) {
// Return this file's parent file
//return ((File) arg0).getParentFile();
return null;
}
/**
* Returns whether the passed object has children
*
* #param arg0
* the parent object
* #return boolean
*/
public boolean hasChildren(Object arg0) {
// Get the children
Object[] obj = getChildren(arg0);
// Return whether the parent has children
return obj == null ? false : obj.length > 0;
}
/**
* Gets the root element(s) of the tree
*
* #param arg0
* the input data
* #return Object[]
*/
/*public Object[] getElements(Object arg0) {
// These are the root elements of the tree
// We don't care what arg0 is, because we just want all
// the root nodes in the file system
return File.listRoots();
//return "New ROOT";
}*/
public Object[] getElements(Object inputElement) {
//TreeItem rootItem = new TreeItem((Tree) arg0, SWT.CHECK);
//rootItem.setText("ROOT");
// return new Object[] { "Session Root" }; // your root item you want to display
treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));
if (inputElement instanceof TreeItem)
{
System.out.println("getelements");
return ((List<String>)inputElement).toArray();
}
return new Object[] {parameters.toArray()};
}
/**
* Disposes any created resources
*/
public void dispose() {
// Nothing to dispose
}
/**
* Called when the input changes
*
* #param arg0
* the viewer
* #param arg1
* the old input
* #param arg2
* the new input
*/
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// Nothing to change
}
}
class FileTreeLabelProvider1 implements ILabelProvider {
// The listeners
private List listeners;
// Images for tree nodes
private Image file;
private Image dir;
// Label provider state: preserve case of file names/directories
/**
* Constructs a FileTreeLabelProvider
*/
public FileTreeLabelProvider1() {
// Create the list to hold the listeners
listeners = new ArrayList();
// Create the images
try {
file = new Image(null, new FileInputStream("images/file.gif"));
dir = new Image(null, new FileInputStream("images/directory.gif"));
} catch (FileNotFoundException e) {
// Swallow it; we'll do without images
}
}
/**
* Gets the image to display for a node in the tree
*
* #param arg0
* the node
* #return Image
*/
public Image getImage(Object arg0) {
// If the node represents a directory, return the directory image.
// Otherwise, return the file image.
return ((File) arg0).isDirectory() ? dir : file;
}
/**
* Gets the text to display for a node in the tree
*
* #param arg0
* the node
* #return String
*/
public String getText(Object arg0) {
// Get the name of the file
String text = ((File) arg0).getName();
// If name is blank, get the path
if (text.length() == 0) {
text = ((File) arg0).getPath();
}
// Check the case settings before returning the text
return text ;
}
/**
* Adds a listener to this label provider
*
* #param arg0
* the listener
*/
public void addListener(ILabelProviderListener arg0) {
listeners.add(arg0);
}
/**
* Called when this LabelProvider is being disposed
*/
public void dispose() {
// Dispose the images
if (dir != null)
dir.dispose();
if (file != null)
file.dispose();
}
/**
* Returns whether changes to the specified property on the specified
* element would affect the label for the element
*
* #param arg0
* the element
* #param arg1
* the property
* #return boolean
*/
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
/**
* Removes the listener
*
* #param arg0
* the listener to remove
*/
public void removeListener(ILabelProviderListener arg0) {
listeners.remove(arg0);
}
}
Try giving the tree object the parent composite and the flags (SWT.BORDER | SWT.CHECK)
it worked for me this way
tree = new Tree(treeMenu, SWT.BORDER | SWT.CHECK);
This is probably an elementary question. However, I have completed reading the 12th Chapter of Java Programming for the Absolute Beginner and have approached the Challenges section. I cannot quite get the progam to display a labeled button with the extended class.
The specification states:
Extend the JPRButton3D class to create a button that displays a label just like
the AWT Button class you're so familiar with by now. As an extra test, override
the isFocusable() method so that your button class can be traversed and make sure you
paint some special graphic to make it obvious when your button has focus.
How can I edit my code of LabelButton3D and LabelButton3DTest to accomplish this task?
An answer to this specification can potentially aid many new Java programmers in extending their own classes.
Thank you very much for your time and cooperation reagrding this matter.
HERE IS THE CODE FOR JPRButton3D:
package jpr.lightweight;
import java.awt.*;
import java.awt.event.*;
/**
* A lightweight 3D Button class that fires actions when clicked.
* When it is enabled it appears {#link #RAISED RAISED}, when
* it is pressed it appears {#link #SUNK SUNK}, and when it is
* not enabled, it appears {#link #FLAT FLAT}.
*/
public class JPRButton3D extends JPRRectComponent3D {
private boolean pressed;
/**
* This <code>JPRButton3D</code>'s <code>ActionListener</code>.
*/
protected ActionListener actionListener;
private String actionCommand;
/**
* Constructs a new <code>JPRButton3D</code> with minimum size
*/
public JPRButton3D() {
this(ABSOLUTE_MIN_WIDTH, ABSOLUTE_MIN_HEIGHT, 1);
}
/**
* Constructs a new <code>JPRButton3D</code> with the given dimensions.
* #param wide the width
* #param high the height
*/
public JPRButton3D(int wide, int high) {
this(wide, high, 1);
}
/**
* Constructs a new <code>JPRButton3D</code> with the given dimensions
* and border magnitude.
* #param wide the width
* #param high the height
* #param border_magnitude the border's magnitude
*/
public JPRButton3D(int wide, int high, int border_magnitude) {
super(wide, high, RAISED, border_magnitude);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public void processMouseEvent(MouseEvent e) {
if (isEnabled() & e.getModifiers() == MouseEvent.BUTTON1_MASK) {
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
pressed = true;
current_appearance = SUNK;
repaint();
break;
case MouseEvent.MOUSE_EXITED:
if (pressed) {
pressed = false;
current_appearance = RAISED;
repaint();
}
break;
case MouseEvent.MOUSE_RELEASED:
if (pressed) {
current_appearance = RAISED;
repaint();
if (actionListener != null) {
actionListener.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, actionCommand,
e.getModifiers()));
}
}
break;
}
}
super.processMouseEvent(e);
}
/**
* Adds the specified <code>ActionListener</code>
* #param listener <code>ActionListener</code> to add
*/
public void addActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.add(actionListener, listener);
}
/**
* Removes the specified <code>ActionListener</code>
* #param listener <code>ActionListener</code> to remove
*/
public void removeActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.remove(actionListener,
listener);
}
/**
* Sets the action command associated with action events.
* #param command The action command.
*/
public void setActionCommand(String command) {
actionCommand = command;
}
/**
* Gets the action command associated with action events.
* #return the action command
*/
public String getActionCommand() {
return actionCommand;
}
/**
* Enables or disables this <code>JPRButton3D</code>.
* #param b <code>true</code> to enable, <code>false</code> to disable
*/
public void setEnabled(boolean b) {
if (b) current_appearance = RAISED;
else current_appearance = FLAT;
repaint();
super.setEnabled(b);
}
}
HERE IS MY CODE FOR LabelButton3DTest (to extend JPRButton3D):
import java.awt.*;
import java.awt.event.*;
import jpr.lightweight.JPRButton3D;
public class LabelButton3DTest extends GUIFrame {
LabelButton3D[] b;
String s;
public LabelButton3DTest() {
super("LabelButton3D Test");
setLayout(new FlowLayout());
b = new LabelButton3D[1];
b[0] = new LabelButton3D("Favorite Button");
b[0] = new LabelButton3D(75, 35, 1);
add(b[0]);
pack();
setVisible(true);
}
public static void main(String args[]) {
new LabelButton3DTest();
}
}
HERE IS MY CODE FOR LabelButton3D:
public class LabelButton3D extends JPRButton3D {
public LabelButton3D(String label) {
}
public LabelButton3D(int wide, int high, int border_magnitude) {
super(wide, high, border_magnitude);
}
}
Hi, I am trying to show more detailed information after a user clicks on the message balloon tooltip.
However, I can't find how to capture that event.
Is this possible to do?
1) Is possible to listening MouseClickEvents by add ActionListener to the TrayIcon, then Message body listening for MouseClicked
2) (not asked directly) but I can't to give you an answer listening if message was closed by close button, and Message gone away from screen same way, but without catch any event(s)
3) looks like as this Java TrayIcon message close button should be only one solutions, because API doesn't implements another methods,
import java.awt.*;
import java.awt.event.*;
public class FullTray {
private static class ShowMessageListener implements ActionListener {
private TrayIcon trayIcon;
private String title;
private String message;
private TrayIcon.MessageType messageType;
ShowMessageListener(TrayIcon trayIcon, String title, String message, TrayIcon.MessageType messageType) {
this.trayIcon = trayIcon;
this.title = title;
this.message = message;
this.messageType = messageType;
}
public void actionPerformed(ActionEvent e) {
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Message Clicked");
}
});
trayIcon.displayMessage(title, message, messageType);
}
}
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
if (SystemTray.isSupported()) {
final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("gifIcon.gif");
PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);
MenuItem item = new MenuItem("Error");
item.addActionListener(new ShowMessageListener(trayIcon, "Error Title", "Error", TrayIcon.MessageType.ERROR));
popup.add(item);
item = new MenuItem("Warning");
item.addActionListener(new ShowMessageListener(trayIcon, "Warning Title", "Warning", TrayIcon.MessageType.WARNING));
popup.add(item);
item = new MenuItem("Info");
item.addActionListener(new ShowMessageListener(trayIcon, "Info Title", "Info", TrayIcon.MessageType.INFO));
popup.add(item);
item = new MenuItem("None");
item.addActionListener(new ShowMessageListener(trayIcon, "None Title", "None", TrayIcon.MessageType.NONE));
popup.add(item);
item = new MenuItem("Close");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
}
});
popup.add(item);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("Can't add to tray");
}
} else {
System.err.println("Tray unavailable");
}
}
};
EventQueue.invokeLater(runner);
}
private FullTray() {
}
}
Even though this topic isn't exactly recent, I figured I'd share my solution since this kind of thing doesn't really change.
How To Use
To use this code, just create the 1 public class and the 2 public interfaces.
Now create an instance of the CustomTrayIcon class and call the addToSystemTray() method, once you're done initializing it.
Anytime you want to show a notification bubble, call the showBubbleNotification(...) method.
To remove this tray icon, just call the removeFromSystemTray() method.
CustomTrayIcon
This class is the base which does all of the heavy lifting.
package com.samples;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* This class can be used to create an icon in the {#link SystemTray}, providing
* the current operating system supports the {#link SystemTray}. <br />
* <br />
* <b><u>Userful Methods</u>:</b>
* <ul>
* <li>
* Use the {#link CustomTrayIcon#addToSystemTray() addToSystemTray()} method to
* add this {#link CustomTrayIcon} to the {#link SystemTray}. <br />
* </li>
* <li>
* Use the {#link CustomTrayIcon#removeFromSystemTray() removeFromSystemTray()}
* method to remove this {#link CustomTrayIcon} from the {#link SystemTray}. <br />
* </li>
* <li>
* Use the
* {#link CustomTrayIcon#showBubbleNotification(String, String, MessageType, CustomTrayIconNotificationBubbleClickEvent)
* showBubbleNotification(String, String, MessageType,
* CustomTrayIconNotificationBubbleClickEvent)} method to show a bubble notification to
* the user. <br />
* </li>
* <li>
* Use the {#link CustomTrayIcon#setClickDetectionDelay_override(int)
* setClickDetectionDelay_override(int)} method to override the default delay to
* be used when detecting if a user performed a single/double/triple click.</li>
* </ul>
*
* #author Matthew Weiler
* */
public abstract class CustomTrayIcon extends TrayIcon implements CustomTrayIconMouseInterface
{
/* PRIVATE CONSTANTS */
/**
* This will be used as the millisecond delay between clicks to detect
* single or double clicks. <br />
* <br />
* <b><u>Default Value</u>:</b> 300
* */
private static final int DEFAULT_CLICK_DETECTION_DELAY = 300;
/**
* This will be used to ensure that we can synchronize the click counter
* changes.
* */
private static final Object clickLock = new Object();
/* PRIVATE VARIABLES */
/**
* This will store the override value to be used in-place of the
* {#link CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY CLICK_DETECTION_DELAY}
* default value.
*
* #see CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY
* */
private int clickDetectionDelay_override = -1;
/**
* This will store the {#link CustomTrayIconNotificationBubbleClickEvent} which
* should be executed the next time that an {#link ActionEvent} is fired by
* the user clicking a notification bubble.
* */
private CustomTrayIconNotificationBubbleClickEvent newBubbleAction = null;
/**
* This will be used to keep track of which click was responsible for the
* current click timer.
* */
private long clickOwnerIndex = 0L;
/**
* This will be used to keep track of how many clicks occurred in the
* current click detection cycle.
* */
private int clickCounter = 0;
/* CONSTRUCTORS */
/**
* This will create a new instance of a {#link CustomTrayIcon}.
*
* #param image
* The {#link Image} to display as the icon in the notification
* tray.
* */
public CustomTrayIcon(final Image image)
{
super(image);
this.postCreateTasks();
}
/**
* This will create a new instance of a {#link CustomTrayIcon}.
*
* #param image
* The {#link Image} to display as the icon in the notification
* tray.
* #param tooltip
* The string to be used as tooltip text; if the value is null no
* tooltip is shown.
* */
public CustomTrayIcon(final Image image, final String tooltip)
{
super(image, tooltip);
this.postCreateTasks();
}
/**
* This will create a new {#link CustomTrayIcon}. <br />
* The {#link CustomTrayIcon}
* */
public CustomTrayIcon(final Image image, final String tooltip, final PopupMenu popup)
{
super(image, tooltip, popup);
this.postCreateTasks();
}
/* PUBLIC METHODS */
/**
* This will determine if the current operating system supports
* {#link SystemTray}.
*
* #see SystemTray#isSupported()
*
* #return <code>true</code> if the current operating system supports
* {#link SystemTray}; <code>false</code> otherwise.
* */
public static boolean isSystemTraySupported()
{
return SystemTray.isSupported();
}
/**
* This method will add this {#link CustomTrayIcon} to {#link SystemTray} . <br />
* <br />
* <i>this method may return <code>false</code> if the current operating
* system does not have or support a {#link SystemTray}</i>
*
* #return <code>true</code> if this {#link CustomTrayIcon} was added;
* <code>false</code> otherwise.
* */
public boolean addToSystemTray()
{
// Ensure that the current operating system supports
// the SystemTray.
if (CustomTrayIcon.isSystemTraySupported())
{
try
{
// Add this CustomTrayIcon to the SystemTray.
SystemTray.getSystemTray().add(this);
return true;
}
catch (AWTException e)
{
// ignore
}
}
return false;
}
/**
* This method will remove this {#link CustomTrayIcon} from the
* {#link SystemTray}.
* */
public void removeFromSystemTray()
{
SystemTray.getSystemTray().remove(this);
}
#Override
public void displayMessage(final String caption, final String text, final MessageType messageType)
{
this.newBubbleAction = null;
super.displayMessage(caption, text, messageType);
}
/**
* This method will show a bubble notification near the
* {#link CustomTrayIcon}.
*
* #see TrayIcon#displayMessage(String, String, MessageType)
*
* #param title
* The title to be displayed in the bubble notification.
* #param message
* The message to be displayed in the bubble notification.
* #param messageType
* The type of {#link MessageType} which will denote the style of
* the bubble notification.
* #param actionOnClick
* The {#link CustomTrayIconNotificationBubbleClickEvent} which should be
* fired when the user clicks the bubble notification.
*
* #throws NullPointerException
* If both caption and text are <code>null</code>.
* */
public void displayMessage(final String title, final String message, final MessageType messageType, final CustomTrayIconNotificationBubbleClickEvent actionOnClick)
{
this.showBubbleNotification(title, message, messageType, actionOnClick);
}
/**
* This method will show a bubble notification near the
* {#link CustomTrayIcon}.
*
* #see TrayIcon#displayMessage(String, String, MessageType)
*
* #param title
* The title to be displayed in the bubble notification.
* #param message
* The message to be displayed in the bubble notification.
* #param messageType
* The type of {#link MessageType} which will denote the style of
* the bubble notification.
*
* #throws NullPointerException
* If both caption and text are <code>null</code>.
* */
public void showBubbleNotification(final String title, final String message, final MessageType messageType)
{
this.showBubbleNotification(title, message, messageType, null);
}
/**
* This method will show a bubble notification near the
* {#link CustomTrayIcon}.
*
* #see TrayIcon#displayMessage(String, String, MessageType)
*
* #param title
* The title to be displayed in the bubble notification.
* #param message
* The message to be displayed in the bubble notification.
* #param messageType
* The type of {#link MessageType} which will denote the style of
* the bubble notification.
* #param actionOnClick
* The {#link CustomTrayIconNotificationBubbleClickEvent} which should be
* fired when the user clicks the bubble notification.
*
* #throws NullPointerException
* If both caption and text are <code>null</code>.
* */
public void showBubbleNotification(final String title, final String message, final MessageType messageType, final CustomTrayIconNotificationBubbleClickEvent actionOnClick)
{
this.newBubbleAction = actionOnClick;
super.displayMessage(title, message, messageType);
}
/* GETTERS & SETTERS */
/**
* This will get the override value to be used in-place of the
* {#link CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY CLICK_DETECTION_DELAY}
* default value.
*
* #see CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY
*
* #return The override value to be used in-place of the
* {#link CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY
* CLICK_DETECTION_DELAY} default value.
* */
public int getClickDetectionDelay_override()
{
return this.clickDetectionDelay_override;
}
/**
* This will set the override value to be used in-place of the
* {#link CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY CLICK_DETECTION_DELAY}
* default value.
*
* #see CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY
*
* #param clickDetectionDelay_override
* The override value to be used in-place of the
* {#link CustomTrayIcon#DEFAULT_CLICK_DETECTION_DELAY
* CLICK_DETECTION_DELAY} default value.
* */
public void setClickDetectionDelay_override(final int clickDetectionDelay_override)
{
this.clickDetectionDelay_override = clickDetectionDelay_override;
}
/* PRIVATE METHODS */
/**
* This method will return the delay to have between each click when
* determining if the user performed a single/double/triple click.
*
* #return The delay to have between each click when determining if the user
* performed a single/double/triple click.
* */
private int getClickDetectionDelay()
{
if (this.clickDetectionDelay_override > 0)
{
return this.clickDetectionDelay_override;
}
return CustomTrayIcon.DEFAULT_CLICK_DETECTION_DELAY;
}
/**
* This method will be executed as the constructors last task. <br />
* <br />
* <i>this method will set some attributes on this {#link CustomTrayIcon} and
* apply the appropriate listeners</i>
* */
private void postCreateTasks()
{
this.setImageAutoSize(true);
this.applyCustomListeners();
}
/**
* This will apply the various listeners to this {#link CustomTrayIcon}
* object.
* */
private void applyCustomListeners()
{
// Add an ActionListener which will fire when the user
// clicks a bubble notification.
this.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if (CustomTrayIcon.this.newBubbleAction != null)
{
CustomTrayIcon.this.newBubbleAction.button1_click();
CustomTrayIcon.this.newBubbleAction = null;
}
}
});
// Add a MouseListener on this CustomTrayIcon which will
// fire when the user interacts with this CustomTrayIcon
// using their mouse.
this.addMouseListener(new MouseListener()
{
#Override
public void mouseReleased(MouseEvent arg0)
{
// NOT SUPPORTED BY TrayIcon
}
#Override
public void mousePressed(MouseEvent arg0)
{
// NOT SUPPORTED BY TrayIcon
}
#Override
public void mouseExited(MouseEvent arg0)
{
}
#Override
public void mouseEntered(MouseEvent arg0)
{
}
#Override
public void mouseClicked(MouseEvent arg0)
{
// Clear the bubble notification action as any open
// bubbles should be popped/closed when this interaction
// occurs.
CustomTrayIcon.this.newBubbleAction = null;
// If this MouseEvent was triggered by mouse button 1
// being clicked... then continue.
if (arg0.getButton() == MouseEvent.BUTTON1)
{
long tmp_clickOwnerIndex = 0L;
// Ensure that only one thread can modify the click owner index
// at any given time.
synchronized (CustomTrayIcon.clickLock)
{
// Increase the click owner index so that only the very
// last click can fire an event.
CustomTrayIcon.this.clickOwnerIndex++;
// Store the current value of the click owner index
tmp_clickOwnerIndex = CustomTrayIcon.this.clickOwnerIndex;
}
// Extract the click owner index, which was obtained in a
// thread-safe way, into a final variable so we can access it
// below.
final long tmp_clickOwnerIndex_final = tmp_clickOwnerIndex;
// Launch a new Thread which will sleep for a pre-determined
// period of time to catch the delay of a double/triple click.
(new Thread()
{
int tmp_clickCounter = 0;
public void run()
{
// Ensure that only one thread can modify the click
// counter at any given time.
synchronized (CustomTrayIcon.clickLock)
{
// Increase the number of active clicks that occurred
// during the current detection cycle.
CustomTrayIcon.this.clickCounter++;
// Extract the total number of active clicks that occurred
// during the current detection cycle up to this point.
tmp_clickCounter = CustomTrayIcon.this.clickCounter;
}
try
{
// Sleep for the pre-determined multiple click detection
// time.
// This ensures that we give the use enough time to
// actually perform multiple clicks.
Thread.sleep(CustomTrayIcon.this.getClickDetectionDelay());
}
catch (InterruptedException e)
{
// ignore
}
// Ensure that only one thread can modify the click owner index
// at any given time.
synchronized (CustomTrayIcon.clickLock)
{
// If this click was the last click since the appropriate
// delay, then it's clear that this click should be
// processed.
if (CustomTrayIcon.this.clickOwnerIndex == tmp_clickOwnerIndex_final)
{
// If only one click occurred during the allocated delayed
// time, then fire a button 1 click.
if (tmp_clickCounter == 1)
{
CustomTrayIcon.this.button1_click();
}
// If two clicks occurred during the allocated delayed
// time, then fire a button 2 click.
else if (tmp_clickCounter == 2)
{
CustomTrayIcon.this.button1_doubleClick();
}
// If three clicks occurred during the allocated delayed
// time, then fire a button 3 click.
else if (tmp_clickCounter == 3)
{
CustomTrayIcon.this.button1_tripleClick();
}
}
// Decrease the click owner index which will reset the
// click stack.
CustomTrayIcon.this.clickCounter--;
}
}
}).start();
}
// If this MouseEvent was triggered by mouse button 2
// being clicked... then continue.
else if (arg0.getButton() == MouseEvent.BUTTON2)
{
// Ensure that only one thread can modify the click owner index
// at any given time.
synchronized (CustomTrayIcon.clickLock)
{
// Increase the click owner index so that only the very
// last click can fire an event.
CustomTrayIcon.this.clickOwnerIndex++;
}
CustomTrayIcon.this.button2_click();
}
// If this MouseEvent was triggered by mouse button 3
// being clicked... then continue.
else if (arg0.getButton() == MouseEvent.BUTTON3)
{
// Ensure that only one thread can modify the click owner index
// at any given time.
synchronized (CustomTrayIcon.clickLock)
{
// Increase the click owner index so that only the very
// last click can fire an event.
CustomTrayIcon.this.clickOwnerIndex++;
}
CustomTrayIcon.this.button3_click();
}
}
});
}
}
CustomTrayIconMouseInterface
This interface is used to externalize various user interactions via the mouse.
package com.samples;
import javax.swing.Popup;
/**
* This interface contains several methods which can be fired when a user
* interacts with the {#link CustomTrayIcon} using their mouse.
*
* #author Matthew Weiler
* */
public interface CustomTrayIconMouseInterface
{
/**
* This method will be fired when the user clicks, with mouse button 1, this
* {#link CustomTrayIcon}.
* */
public void button1_click();
/**
* This method will be fired when the user double-clicks, with mouse button
* 1, this {#link CustomTrayIcon}.
* */
public void button1_doubleClick();
/**
* This method will be fired when the user triple-clicks, with mouse button
* 1, this {#link CustomTrayIcon}.
* */
public void button1_tripleClick();
/**
* This method will be fired when the user clicks, with mouse button 2, this
* {#link CustomTrayIcon}.
* */
public void button2_click();
/**
* This method will be fired when the user clicks, with mouse button 3, this
* {#link CustomTrayIcon}. <br />
* <br />
* <i>if a {#link Popup} menu is assigned to this {#link CustomTrayIcon}, it
* will still popup regardless of any actions taken by this method</i>
* */
public void button3_click();
}
CustomTrayIconNotificationBubbleClickEvent
This interface is used to externalize the interaction with the notification bubble.
package com.samples;
/**
* This interface contains several methods which can be fired when a user
* interacts with a bubble notification using their mouse.
*
* #author Matthew Weiler
* */
public interface CustomTrayIconNotificationBubbleClickEvent
{
/**
* This method will be fired when the user clicks, with mouse button 1, this
* bubble notification.
* */
public void button1_click();
}
When changing tabs, JTabbedPane always focuses the first focusable Component inside the tab.
How can I change its behaviour so that it will either focus the last focused component or none at all?
Invoking requestFocus afterwards is not an option because JTabbedPane must not set the focus in the wrong field at all.
Take a look at: Remembering last focused component.
You need to keep track of which component has focus in each tab. Then when a tab is selected, you need to change focus to the appropriate component. Here is the code taken from the link above:
class TabbedPaneFocus extends JTabbedPane implements ChangeListener, PropertyChangeListener {
private Hashtable tabFocus;
public TabbedPaneFocus() {
tabFocus = new Hashtable();
addChangeListener(this);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(this);
}
/*
* (non-Javadoc)
*
* #see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
#Override
public void propertyChange(PropertyChangeEvent e) {
if ("permanentFocusOwner".equals(e.getPropertyName())) {
final Object value = e.getNewValue();
if (value != null) {
tabFocus.put(getTitleAt(getSelectedIndex()), value);
}
}
}
/*
* (non-Javadoc)
*
* #see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
#Override
public void stateChanged(ChangeEvent e) {
Object value = tabFocus.get(getTitleAt(getSelectedIndex()));
if (value != null) {
((Component) value).requestFocusInWindow();
}
}
}
basically this works inside one Top-Level Container correctly
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
someComponent.grabFocus();
someComponent.requestFocus();//or inWindow
}
});