Passing Object References and Concurrency / JavaFX - java

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.

Related

Java Intermec ITCScan failed to load

I'm trying to implement a Barcode Reader for a CK71 ATEX Intermec Scanner. The operating system is Windows Embedded Handheld 6.5 and as JVM I'm using phoneME Personal Profile. I did install the DC_Java_WM6_Armv4i.cab (see the picture)
When I run the code below I get the following error: ITCScan failed to load. java.lang.UnsatisfiedLinkError: no ITCScan.dll in java.library.path
How can I fix this error? I've tried everything.
Note that before, I was using CreME JVM and everything was working fine. I gave up CreME when my 30 days evaluation version expired.
The content of the .lnk file (instead of myProject.MainClass are the real names of course):
255#"\Program Files\pMEA PP\bin\cvm.exe" "-Xopt:stdioPrefix=\My Documents,useConsole=false" -cp "\My Documents\Trasabilitate.jar;\My Documents\DataCollection.jar" myProject.MainClass
Here is the complete code:
/*
* BarcodeSample.java
*
* COPYRIGHT (c) 2004 INTERMEC TECHNOLOGIES CORPORATION, ALL RIGHTS RESERVED
*/
import java.awt.*;
import com.intermec.datacollection.*;
/**
* This sample demonstrates using the BarcodeReader class to
* read barcode data into a text field.
*/
public class BarcodeSample extends Frame implements BarcodeReadListener
{
BarcodeReader bcRdr;
TextField txtFieldData;
Button btnClose;
Label labelStatus;
public BarcodeSample(String aTitle)
{
super(aTitle);
initComponents();
try
{
bcRdr = new BarcodeReader();
bcRdr.addBarcodeReadListener(this);
// Starts asynchronous barcode read
bcRdr.threadedRead(true);
}
catch (BarcodeReaderException e)
{
System.out.println(e);
labelStatus.setText(e.getMessage());
//*****
//* Since m_labelStatus was not initialized with text,
//* doLayout() is required on some platforms in order
//* to show the new label text for the first setText()
//* call.
//*****
doLayout();
}
}
private void initComponents()
{
setLayout(new FlowLayout());
txtFieldData = new TextField(20);
add(txtFieldData);
btnClose = new Button("Close");
add(btnClose);
labelStatus = new Label();
add(labelStatus);
btnClose.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
exitApp();
}
});
btnClose.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
{
exitApp();
}
}
public void keyReleased(java.awt.event.KeyEvent e) {}
public void keyTyped(java.awt.event.KeyEvent e) {}
});
}
/**
* This method is invoked when the BarcodeReadEvent occurs.
*/
public void barcodeRead(BarcodeReadEvent aBarcodeReadEvent)
{
/**
* Uses EventQueue.invokeLater to ensure the UI update
* executes on the AWT event dispatching thread.
*/
final String sNewData = aBarcodeReadEvent.strDataBuffer;
EventQueue.invokeLater(new Runnable() {
public void run() {
// Displays the scanned data in the text field
txtFieldData.setText(sNewData);
}
});
}
public void exitApp()
{
if (bcRdr != null)
bcRdr.dispose(); // Release system resources used by BarcodeReader
setVisible(false);
dispose(); // Dispose the frame
System.exit(0);
}
public static void main(String[] args)
{
final BarcodeSample asyncReader =
new BarcodeSample("Barcode Sample");
asyncReader.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
asyncReader.exitApp();
};
});
asyncReader.setVisible(true);
}
}
I finally got it to work. I found out what my java path was using the snippet here (I'll post it below in case something happens to the link):
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
System.out.println(key + ": " + value);
}
And then I added my ITCScan.dll to the folder where java.library.path was set (in my case, \ProgramFiles\pMEA PP\bin.
I don't know if this is the most elegant solution, but it works. Hope it'll help somebody someday.

Capture about, preferences and quit menu items

I'm using the current version of SWT to build my applications and I want to run it under Mac OS X (Yosemite).
My problem is now that I'm not be able to capture clicks on the "About", "Preferences" and "Quit" menu items which were automatically added to my application.
I already searched a lot and found the following class which seems very helpful to me http://www.transparentech.com/files/CocoaUIEnhancer.java.
And that's my code to initialize it:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class Test {
private Display display;
private Shell shell;
public Test(Display display) {
this.display = display;
initUI();
}
public void open() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
private void initUI() {
shell = new Shell(display);
shell.setSize(808, 599);
shell.setText("Test");
AboutHandler aboutHandler = new AboutHandler();
PreferencesHandler preferencesHandler = new PreferencesHandler();
QuitHandler quitHandler = new QuitHandler();
CocoaUIEnhancer uienhancer = new CocoaUIEnhancer("Test");
uienhancer.hookApplicationMenu(display, quitHandler, aboutHandler, preferencesHandler);
}
private class AboutHandler implements Listener {
public void handleEvent(Event e) {
}
}
private class PreferencesHandler implements Listener {
public void handleEvent(Event e) {
}
}
private class QuitHandler implements Listener {
public void handleEvent(Event e) {
}
}
}
I can compile it without any errors but if I start the program then I will get the following exception:
Exception in thread "main" java.lang.NoSuchMethodError: actionProc
at org.eclipse.swt.internal.Callback.bind(Native Method)
at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
at org.eclipse.swt.internal.Callback.<init>(Unknown Source)
at CocoaUIEnhancer.initialize(CocoaUIEnhancer.java:124)
at CocoaUIEnhancer.hookApplicationMenu(CocoaUIEnhancer.java:92)
at Test.initUI(Test.java:50)
at Test.<init>(Test.java:18)
It's probably an error in the native libraries but I can't figure it out!
I didn't use CocoaUIEnhancer at all, as it was causing issues as well.
So here's what I ended up doing in my applications:
/**
* Convenience method that takes care of special menu items (About, Preferences, Quit)
*
* #param name The name of the menu item
* #param parent The parent {#link Menu}
* #param listener The {#link Listener} to add to the item
* #param id The <code>SWT.ID_*</code> id
*/
private void addMenuItem(String name, Menu parent, Listener listener, int id)
{
if (OSUtils.isMac())
{
Menu systemMenu = Display.getDefault().getSystemMenu();
for (MenuItem systemItem : systemMenu.getItems())
{
if (systemItem.getID() == id)
{
systemItem.addListener(SWT.Selection, listener);
return;
}
}
}
/* We get here if we're not running on a Mac, or if we're running on a Mac, but the menu item with the given id hasn't been found */
MenuItem item = new MenuItem(parent, SWT.NONE);
item.setText(name);
item.addListener(SWT.Selection, listener);
}
Just call it with SWT.ID_PREFERENCES, SWT.ID_ABOUT and SWT.ID_QUIT respectively. Hand in a fallback menu item name, a fallback Menu and the actual Listener you want to add to the menu item.
So for example:
addMenuItem("Quit", myMenu, new Listener()
{
#Override
public void handleEvent(Event event)
{
// Close database connection for example
}
}, SWT.ID_QUIT);
It looks like this the actionProc
int actionProc( int id, int sel, int arg0 )
in CocoaUIEnhancer probably needs to use long rather than int for the arguments to work with 64 bit SWT.
You need to modify CocoaUIEnhancer.java, to make it work with pure SWT application as described in this tutorial:
Modify the getProductName() method to return a String when no product is found (instead of null)
Wrap the code in hookWorkbenchListener() in a try-catch (IllegalStateException e) block
Wrap the code in modifyShells() in a try-catch (IllegalStateException e) block
Add some code to the actionProc(...) method, to bring up an About-Dialog and Preferences-Dialog (since we aren’t using commands):
static long actionProc(long id, long sel, long arg0) throws Exception {
// ...
else if (sel == sel_preferencesMenuItemSelected_) {
showPreferences();
} else if (sel == sel_aboutMenuItemSelected_) {
showAbout();
}
return 0;
}
private static void showAbout() {
MessageDialog.openInformation(null, "About...",
"Replace with a proper about text / dialog");
}
private static void showPreferences() {
System.out.println("Preferences...");
PreferenceManager manager = new PreferenceManager();
PreferenceDialog dialog = new PreferenceDialog(null, manager);
dialog.open();
}
// ...
Finally, we add the following lines to our main() method:
public static final String APP_NAME = "MyApp";
public static void main(String[] args) {
//in your case change the Test constructor
Display.setAppName(APP_NAME);
Display display = Display.getDefault();
//insert in initUI method call the earlysetup
if (SWT.getPlatform().equals("cocoa")) {
new CocoaUIEnhancer().earlyStartup();
}
Shell shell = new Shell(display);
shell.setText(APP_NAME);
...
}
Quoted code.
Baz's solution works great! If you'd rather not import OSUtils just to test if you are on a Mac, use instead:
System.getProperty("os.name").contentEquals("Mac OS X")

TinySound and multiple click in GUI (button)

I am struggling with a problem using TinySound (http://finnkuusisto.github.io/TinySound/). I've made a method to play a sound (i've implemented the Music class, since it allows to be played without a thread sleep limiter). My problem is that the "Play" button in my GUI can be spammed, resulting in the sound or music being played in a stack. I've checked out the setMultiClickThreshold in the Java API, but this do not solve my problem (You never know how long the sound or music-file is going to be).
Has anyone used TinySound, or know a workaround this challenge?
Here is the code for the method (I will provide more if necessary):
public void playSound(String filePath) {
soundFile = new File(filePath);
TinySound.init();
Music sound = TinySound.loadMusic(soundFile);
sound.play(false);
while(sound.done()) {
TinySound.shutdown();
}
}
Consider using a SwingWorker, disabling the JButton on button press, and re-enabling it when the SwingWorker has completed its actions. The re-enabling could be done within a PropertyChangeListener that has been added to your Swingworker and that responds to a PropertyChangeEvent.newValue() of SwingWorker.StateValue.DONE.
For example, your code could look something like,....
public class SwingworkerEg {
// .....
public void playSound(String filePath) {
soundFile = new File(filePath);
TinySound.init();
Music sound = TinySound.loadMusic(soundFile);
sound.play(false);
while (sound.done()) {
TinySound.shutdown();
}
}
// The JButton or menu item's Action or ActionListener class
private class PlayAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
// disable the button or menu item
setEnabled(false);
// create worker to play music in a background thread
// pass in the file path
PlayWorker playWorker = new PlayWorker(filePath);
// listen for when the worker thread is done
playWorker.addPropertyChangeListener(new PlayWorkerListener(this));
// execute the worker (in a background thread)
playWorker.execute();
}
}
// To listen for when the worker is done
class PlayWorkerListener implements PropertyChangeListener {
private PlayAction playAction;
// pass in the Action so we can re-enable it when done
public PlayWorkerListener(PlayAction playAction) {
this.playAction = playAction;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// if the worker is done
if (evt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
// re-enable the button
playAction.setEnabled(true);
}
}
}
// this is to call playSound in a background thread
class PlayWorker extends SwingWorker<Void, Void> {
private String filePath;
// pass in the file path String
public PlayWorker(String filePath) {
this.filePath = filePath;
}
#Override
protected Void doInBackground() throws Exception {
// this is called in a background thread
playSound(filePath);
return null;
}
}
}
Here's a trivial working example:
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
#SuppressWarnings("serial")
public class SwingWorkerEg2 extends JPanel {
private JSpinner spinner = new JSpinner(new SpinnerNumberModel(3, 3, 10, 1));
public SwingWorkerEg2() {
add(new JLabel("Seconds to wait:"));
add(spinner);
add(new JButton(new FooAction("Please Press Me!")));
}
// The JButton or menu item's Action or ActionListener class
private class FooAction extends AbstractAction {
public FooAction(String name) {
super(name); // set button name
int mnemonic = (int) name.charAt(0); // get first letter as int
putValue(MNEMONIC_KEY, mnemonic); // set button mnemonic for first letter
}
#Override
public void actionPerformed(ActionEvent e) {
// disable the button or menu item
setEnabled(false);
int spinnerValue = ((Integer) spinner.getValue()).intValue();
// create worker to play music in a background thread
FooWorker playWorker = new FooWorker(spinnerValue);
// listen for when the worker thread is done
playWorker.addPropertyChangeListener(new FooWorkerListener(this));
// execute the worker (in a background thread)
playWorker.execute();
}
}
// To listen for when the worker is done
class FooWorkerListener implements PropertyChangeListener {
private FooAction fooAction;
// pass in the Action so we can re-enable it when done
public FooWorkerListener(FooAction fooAction) {
this.fooAction = fooAction;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// if the worker is done
if (evt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
// re-enable the button
fooAction.setEnabled(true);
}
}
}
// this is to call count down in a background thread
class FooWorker extends SwingWorker<Void, Void> {
private int spinnerValue;
// pass in the file path String
public FooWorker(int spinnerValue) {
this.spinnerValue = spinnerValue;
}
#Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < spinnerValue; i++) {
System.out.println("count is: " + i);
Thread.sleep(1000);
}
System.out.println("count is: " + spinnerValue);
return null;
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SwingWorker Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SwingWorkerEg2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Make sure to read Concurrency in Swing for more on how to use SwingWorkers
Got it to work! Thank you very much! I will try to implement it in my ActionController class, it is a bit messy keeping everything in the same method in the SoundHandler ;)
here is the final working SoundHandler:
import java.io.File;
import javax.swing.SwingWorker;
import kuusisto.tinysound.Music;
import kuusisto.tinysound.TinySound;
import imports.ActionController;
import imports.GUI;
/**
* This class handles the playing of the sound and extends SwingWorker so that
* the JFrame do not freeze when the sound is played.
*
* #author Gaute Gjerlow Remen
* #version 1.0
*/
public class SoundHandler extends SwingWorker<Void, Void> {
private GUI gui;
private ActionController actionController;
private File soundFile;
public SoundHandler() {
actionController = new ActionController(this);
gui = new GUI(actionController);
}
/**
* Plays the sound file in another thread
* #param filePath
* #throws Exception if the thread is interrupted
* #return null when doInBackground is finished
*/
public void playSound(String filePath) {
soundFile = new File(filePath);
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
TinySound.init();
Music sound = TinySound.loadMusic(soundFile);
sound.play(false);
while (!sound.done()) {
gui.unablePlayButton();
}
gui.enablePlayButton();
TinySound.shutdown();
return null;
}
};
worker.execute();
}
/**
* #return file opened in the GUI
*/
public String openFile() {
return gui.openFile();
}
/**
* Calls the about window in GUI
*/
public void showAbout() {
gui.showAbout();
}
/**
* Calls the showNoSong window in GUI
*/
public void showNoSong() {
gui.showNoSong();
}
/**
* Calls the changeSongLabel window in GUI
*/
public void changeSongLabel() {
gui.changeSongLabel();
}
/**
* A empty method made only for the extending of the class
*/
#Override
protected Void doInBackground() throws Exception {
// TODO Auto-generated method stub
return null;
}
}

How to create a moveable SWT shell without title bar/ close button?

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;
}
}

Extending a Class by Adding a Button

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);
}
}

Categories