It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to create a timer using as3 timer class or some other class. And every 10 second i want to do some alert or trace something. Timer wont stop at any time. And every 10 second we can do some stuff.
The class of course would be the Timer class.
Here is a simple example to get you started.
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class TimerExample extends Sprite
{
public function TimerExample()
{
var timer:Timer = new Timer(10000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
public function timerHandler(event:TimerEvent):void
{
trace("timerHandler: " + event);
}
}
}
Couldn't be simpler:
var t:Timer = new Timer(10000);
t.addEventListener("timer", doSomething);
t.start();
function doSomething(event:*):void {
trace("something");
}
Related
This question already has answers here:
Passing Parameters JavaFX FXML
(10 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am success to change the pane in stack pane with .setVisible() when the button is in the main scene, not in the stack pane.
But when I want to change my pane with clicking the button in one of the pane , I'll get the NullPointer error......
I tried to create the StackPane controller in every pane controller, and use the method .isPressed() to controll the pane visible , so how can I fix this problem?
chatController.java
public class chatController{
#FXML Pane pane_chat_list,pane_chat_room;
public void initialize() {
pane_chat_list.setVisible(false);
pane_chat_room.setVisible(true);
}
public void isPressed(int a) {
if(a == 0) {
pane_chat_list.setVisible(true);
pane_chat_room.setVisible(false);
}else {
pane_chat_list.setVisible(false);
pane_chat_room.setVisible(true);
}
}
}
chat_list.java
public class chat_list{
#FXML Button chat_list_button;
chatController controll = new chatController();
public void initialize() {
chat_list_button.setOnAction(e -> back());
}
public void back() {
controll.isPressed(1);
}
}
chat_room.java
public class chat_room{
#FXML Button chat_room_back;
chatController controll = new chatController();
public void initialize() {
chat_room_back.setOnAction(e -> back());
}
public void back() {
controll.isPressed(0);
}
}
My first suggestion is to read some good Java book or tutorial, and there are many good resources for Java, also make sure to learn conventions.
Your problem is that you create new instance of chatController in both chat_room and chat_list, and they should share same instance in other to make it work, also you probably don't even initialize pane_chat_list and pane_chat_room inside chatController which leads to NullPointerException.
Your approach makes this really hard even though solution is much simpler, all you need is to have 1 parent class which holds both views, views don't know about each other, they just inform parent that there was a click on backButton, and parent manages what is shown and what is hidden.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How do I make communications go between Arduino Uno and a Java app?
I've found Arduino and Java, but that's not clear to me.
Ok, I'll modify the same code to help you understand. (I'll remove the listener and add a substitution, which is not good. You should use the listener)
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
private static final String PORT = "COM32";
private InputStream input;
private OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if (currPortId.getName().equals(PORT)) {
portId = currPortId;
break;
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.start();
}
public void start() throws IOException {
initialize();
System.out.println("Started");
byte[] readBuffer = new byte[400];
while (true) {
int availableBytes = input.available();
if (availableBytes > 0) {
// Read the serial port
input.read(readBuffer, 0, availableBytes);
// Print it out
System.out.print(new String(readBuffer, 0, availableBytes));
}
}
}
}
Run this and load a code to Arduino which write to the serial. Then those values will be displayed by the program. (you may need to change the PORT accordingly)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
//calling class
import javax.swing.JFrame;
class jcheckkbox {
public static void main(String args[]) {
jRadio roof = new jRadio();
roof.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
roof.setSize(300, 200);
roof.setVisible(true);
//secondary class
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jcheckbox extends JFrame {
private JCheckBox cd;
private JCheckBox md;
private JTextField vcd;
public jcheckbox() {
super("Beer bar");
setLayout(new FlowLayout());
vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);
cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);
handler dahandler = new handler();
cd.addItemListener(dahandler);
md.addItemListener(dahandler);
}
private class handler implements ItemListener {
public void itemStateChanged(ItemEvent event) {
Font cool = null;
if (md.isSelected() && cd.isSelected())
cool = new Font("Serif", Font.BOLD + Font.ITALIC, 25);
else if (md.isSelected())
cool = new Font("Serif", Font.BOLD, 30);
else if (md.isSelected())
cool = new Font("Sans_Serif", Font.ITALIC, 30);
vcd.setFont(cool);
}}}
how to write a program in just one class i mean no need calling class for setsize or defaultcloseoperation etc because two classes are harder to compile when making a .jar or .exe out of it,i know there is another way but i want to use this method as it is a lot more easier to make buttons,textfields comboboxes with this method
If your whole program is within a couple of hundred lines then you can create multiple classes within a file. A file is typically used to host one class, but you can have static classes withing the file
As per some of the comments it is bad practice to put everything in one class. A class should only do one thing and helps modularize your program.
As per your code sample above you are obviously a beginner. I would strongly recommend that you go to the Java Tutorial and take a look around.
If you have any further questions then Google for them, if they have not been answered, then feel free to post a question here.
I really didn't understand the questions but here is my answer as I understand first you can include the main method on your jcheckbox class.
Second you can add this functions you hinted in the constructor
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
public jcheckbox()() {
super("Beer bar");
setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
vcd = new JTextField("this is a code", 20);
vcd.setFont(new Font("Serif", Font.PLAIN, 22));
vcd.setToolTipText("yahoo");
add(vcd);
cd = new JCheckBox("bold");
md = new JCheckBox("italic");
add(md);
add(cd);
handler dahandler = new handler();
cd.addItemListener(dahandler);
md.addItemListener(dahandler);
}
Is there any workaround to create submenu in a flex context menu other than stopping right click from javascript.
Regards,
Hi Frank,
Yes, I want to create submenus in a context menu. Can you help me here.
Regards,
Hi Frank,
I need the context menu for the application not for datagrid.
In my initial question the phrase "other than stopping right click from javascript" means
"catch the right click in html, call a javascript function and over js call a as function."
The project that you have specified does the above procedure. I don't want to use this
procedure. Is there any other way for achieving submenus in a flex context menu. Could you
please tell me if so..
Regards,
Arvind
Yes, there is.
I don't know, what you exactly mean with this:
other than stopping right click from
javascript.
But, if you want to create a entry in submenu, do this:
//Instance of my own class
private var myContext:myContextMenu = new myContextMenu();
application.contextMenu = myContext.myContextMenu;
//Here is the Class:
package com.my.components
{
/* ////////////////////////////////////////////
///// My Context MenĂ¼ /////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//to use: //
// private var myContext:MyContextMenu = new MyContextMenu(); //
// init() in creationComplete //
// application.contextMenu = myContext.myContextMenu; //
////////////////////////////////////////////////////////////////////////////// */
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
public class MyContextMenu extends Sprite
{
public var myContextMenu:ContextMenu;
private var menuLabel:String = String.fromCharCode(169)+" My Company GmbH";
public function MyContextMenu()
{
myContextMenu = new ContextMenu;
removeDefaultItems();
addCustomItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
super();
}
private function removeDefaultItems():void
{
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomItems():void
{
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
}
private function menuItemSelectHandler(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest('http://www.my-company.de'));
}
private function createLabel():TextField
{
var txtField:TextField = new TextField();
//txtField.text = textLabel;
txtField.text = "RightClickHere";
return txtField;
}
}
}
Have fun
EDIT:
There is an interesting project here. They catch the right click in html, call a javascript function and over js call a as function.
Unfortunately, the limitation of FP or NativeMenu APi allowed just on level contextmenu. Read here
Frank
I've got a Flash UI that does a periodic server call to get some updated information. The call uses the flex sdk's rpc.soap.Operation class. It looks something like this:
var wsOperation:Operation = Operation(webService.getOperation(SomeOperation));
wsOperation.addEventListener("fault", wsError);
wsOperation.addEventListener("result", wsResult);
wsOperation.send(...some params);
This call gets some data from a SQL database. I have timed the call from right before the send to the start of the wsResult function at ~4 seconds. During this time, my UI is not updated. It is frozen/unresponsive.
Now, I know Flash is single-threaded/asynchronous, so I'm not sure why this is happening. I see that the send(..) function returns an AsyncToken which I am not using. Could this have something to do with it?
Any other ideas as to why this is happening are appreciated. Thanks.
I still haven't found an acceptable solution to this. It seems ridiculous that I would have to Pseudo thread to get flash to update the UI during a 4 second call. I'm wondering if maybe the parsing of the soap response could be taking up a lot of time. If there is a lot of processing to do, will Flash delay updating the UI indefinitely?
You will get a UI freeze in Flash because it is single threaded. However, you can do pseudo threading with something like this:
package
{
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.utils.getTimer;
import mx.core.UIComponent;
import mx.managers.ISystemManager;
public class PseudoThread extends EventDispatcher
{
public function PseudoThread(sm:ISystemManager, threadFunction:Function, threadObject:Object)
{
fn = threadFunction;
obj = threadObject;
// add high priority listener for ENTER_FRAME
sm.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 100);
sm.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
sm.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
thread = new UIComponent();
sm.addChild(thread);
thread.addEventListener(Event.RENDER, renderHandler);
}
// number of milliseconds we think it takes to render the screen
public var RENDER_DEDUCTION:int = 10;
private var fn:Function;
private var obj:Object;
private var thread:UIComponent;
private var start:Number;
private var due:Number;
private var mouseEvent:Boolean;
private var keyEvent:Boolean;
private function enterFrameHandler(event:Event):void
{
start = getTimer();
var fr:Number = Math.floor(1000 / thread.systemManager.stage.frameRate);
due = start + fr;
thread.systemManager.stage.invalidate();
thread.graphics.clear();
thread.graphics.moveTo(0, 0);
thread.graphics.lineTo(0, 0);
}
private function renderHandler(event:Event):void
{
if (mouseEvent || keyEvent)
due -= RENDER_DEDUCTION;
while (getTimer() < due)
{
if (!fn(obj))
{
if (!thread.parent)
return;
var sm:ISystemManager = thread.systemManager;
sm.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
sm.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
sm.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
sm.removeChild(thread);
thread.removeEventListener(Event.RENDER, renderHandler);
dispatchEvent(new Event("threadComplete"));
}
}
mouseEvent = false;
keyEvent = false;
}
private function mouseMoveHandler(event:Event):void
{
mouseEvent = true;
}
private function keyDownHandler(event:Event):void
{
keyEvent = true;
}
}
}
This will enable you to do a process without the UI Freeze. It basically uses the stage's RENDER event to defer processing. This code performs as much Actionscript computation as possible limited by the time needed to maintain the frame rate. For more information see: http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html