My button can't open my class - java

Basically, the problem is I created an interface with Java Scene Builder. And from FXML button I wanted to open my class.
#FXML
public void pressButton(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try{
new SerialChart().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
#FXML
public void pressButton2(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try {
new Main().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
And my Main can be open, but my SerialChart can't be opened. It says "The constructor SerialChart() is undefined". So here is the problem I think
public SerialChart(String title) {
super(title);
I think this is the problem why I can't open. Please help me... I can show you the whole code if you need.

Like Jim Garrisson said, your constructor that you call takes no arguments, but your defined one does (String title). This means you need to pass in a String argument (Even a blank one like "" will work) when you call it in the Button function.
new SerialChart("Some Title").start(new Stage()); //should be your call in the Button function.

so this is the answer I figured out SO HAPPY
public void pressButton(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try{
SerialChart serialChartDemo = new SerialChart("Clean Energy Data Real time graph");
serialChartDemo.pack();
RefineryUtilities.centerFrameOnScreen(serialChartDemo);
serialChartDemo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}

Related

JavaFX freeze window

I encountered a problem when I filter a treeView (long process) my application freezes. I tried to do this in a separate thread (Thread), but then I got the error "Not on FX application thread; currentThread = Thread-5"
void InitBtnFind() {
//Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
Platform.runLater(() -> {
try {
// imitation of work
Thread.sleep(5000);
fileView.setRoot(treeView.filterChanged(finalSFilterExt, finalSearchW));
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.out.println("lol");
});
}
});
}
You can give a sample code to solve my problem.
P.s. Do not lower my reputation, I am really interested in this matter
P.s. my attempt to do this with thread
//Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
if (findThread != null && findThread.isAlive())
findThread.interrupt();
findThread = new Thread(() -> {
try {
fileView.setRoot(treeView.filterChanged(finalSFilterExt, finalSearchW));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("lol");
});
findThread.setName("findThread");
findThread.setDaemon(true);
findThread.start();
}
System.out.println("kek");
});
I fixed it this way, I don’t know how correct it is. But it worked for me. I hope it will be useful.
//Event Button Search
btnFind.setOnAction(event -> {
newFind();
if (Config.isRoot()) {
String finalSFilterExt = filterExt.getText();
String finalSearchW = searchWord.getText();
if (findThread != null && findThread.isAlive())
findThread.interrupt();
findThread = new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("++++++++++++++");
var q =treeView.filterChanged(finalSFilterExt, finalSearchW);
Platform.runLater(()->{fileView.setRoot(q);});
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});
findThread.setName("findThread");
findThread.setDaemon(true);
findThread.start();
}
});

Writing a character on another java application on button click?

I am a Java programmer and new to JavaFx.I want to create a virtual keyborad. I can make everything like buttons,layout,stage,scene everything.I also know using setText() method which can write text on the same java applicatton,but the question is that how do i make understand computer or program(in javafx or java not in swings) that on button click(ie on setOnAction()),it has to write a character on any 'another' java application (such as notepad,wordpad,etc). Is there is any class or interface that i have to extends or implements respectively or is there is any method which can help? I had explored the internet but was unable to find something helpful.
If you have set all the buttons in your controller you can do like this :
//I supposed you named you 'button_a' your
#FXML
Button button_a;
#Override
public void initialize(URL location, ResourceBundle resources) {
button_a.setOnAction(event->{
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("file.txt")); //or new File("c:/users/.../.../file.txt");
writer.write(button_a.getText()); //will give the letter you write on the button : the letter of the keyboard
} catch (IOException e) {
System.err.println("IOError on write");
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println("IOError on close");
e.printStackTrace();
}
}
}
});
}
An easier way could be that : You have surely put all your buttons in a container, a GridPane would be good because you can put all into ONE container (and put ONLY the buttons or you'll need to check it's a button each time in the loop), and then iterate over the children of the GridPane (the buttons) :
#FXML
GridPane grid;
#Override
public void initialize(URL location, ResourceBundle resources) {
String fileName = "test.txt";
for(Node button : grid.getChildren()){
((Button)button).setOnAction(event->{
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileName)); //or new File("c:/users/.../.../file.txt");
writer.write(((Button)button).getText()); //will give the letter you write on the button : the letter of the keyboard
} catch (IOException e) {
System.err.println("IOError on write");
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println("IOError on close");
e.printStackTrace();
}
}
}
});
}
}
Hope this helps

Simultaneous launch of two processes

I need to run two processes simultaneously.
I wrote the code:
public void starttwoprocessing () {
final Thread tworunprocessing = new Thread(new Runnable() {
public void run() {
FlashLight.onFlashResume();
handler.post(new Runnable() {
public void run() {
camera.takePicture(null, null, photoCallback);
}
});
}
});
tworunprocessing.start();
}
First start:
camera.takePicture(null, null, photoCallback);
The second:
FlashLight.onFlashResume();
After changing places with the same result.
In this case, I get the first shot and the flash is started later.
Thread.sleep(...); does not help
How to start simultaneously flash, and immediately take a picture?
Thanks
written like this:
public class Launcher
{
public void main(String args[]) throws IOException, InterruptedException
{
try {
Process[] proc = new Process[2];
proc[0] = new ProcessBuilder("FlashPreview.onFlashResumeStart()").start();
Thread.sleep(3000);
proc[1] = new ProcessBuilder("camera.takePicture(null, null, photoCallback)").start();
try {
Thread.sleep(3000);
}
catch (InterruptedException ex)
{
}
proc[0].destroy();
Thread.sleep(3000);
proc[1].destroy();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Called:
mk = new Launcher();
try {
mk.main(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Something I'm doing wrong.
Does not work at all, no crash, but wrote in the log:07-05 16:38:58.217: W/System.err(30934): java.io.IOException: Error running exec(). Command: [FlashPreview.onFlashResumeStart()] Working Directory: null Environment: [ANDROID_SOCKET_zygote=9, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/sdcard0, ANDROID_ASSETS=/system/app, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec, LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/framework_ext.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/sec_edm.jar:/system/framework/seccamera.jar, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=8,66560, VIBE_PIPE_PATH=/dev/pipes]
even using Threads your processes will runs after eche other. Using Threads means that second process no need to wait while first one is done. But easiest way how to fire two processes at the same time it is use timeout or ProcessBuilder
Also it can be good idea to run second process in first one. As for me it the best solution.
P.S. privet, ne chasto yvidiw zdes svoih s ykrainu)))
I have implemented the following:
class MyTask extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute() {
super.onPreExecute();
FlashLight.onFlashResume();
}
#Override
protected Integer doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
camera.takePicture(null, null, photoCallback);
}
}

Cannot use IOException in AbstractAction

I have run into a curious issue. I need to export some text to a file as a result of pressing a button in a GUI. I cannot, however, apply an IOException to the actionPerformed method of the AbstractAction that is called by the event. I am at a loss as to how to get around this.
Here is the export class:
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import java.io.*;
public class ExportRunner
{
public static void exportToFile(ArrayList<Locker> list) throws IOException
{
}
}
And the AbstractAction extension:
class Export extends AbstractAction
{
public Export()
{
super("Export");
}
public void actionPerformed(ActionEvent e)
{
ExportRunner.exportToFile(list);
}
}
First of all are you sure you want to re throw the exception or maybe is better to handle it and/or show a message to the user?
Option 1: re-throw the exception (ugly in my opinion):
public void actionPerformed(ActionEvent e) {
try{
ExportRunner.exportToFile(list);
} catch(IOException ioex) {
throw new RuntimeException(ioex);
}
}
Option 2: catch and handle it:
public void actionPerformed(ActionEvent e) {
try{
ExportRunner.exportToFile(list);
} catch(IOException ioex) {
handleItOrShowMessageToUser(ioex);
}
}
I would normally pass in a "error handler" to the action class that would allow the delegation of the responsibility of dealing with showing/reporting the error to another part of the application...
Something like...
public interface ErrorListener {
public void errorOccurred(String msg, Exception exp);
}
Then you could pass it to you action...
public class Export extends AbstractAction
{
private ErrorListener errorHandler;
public Export(ErrorListener errorHandler)
{
super("Export");
this.errorHandler = errorHandler;
}
public void actionPerformed(ActionEvent e)
{
try {
ExportRunner.exportToFile(list);
} catch (IOException exp) {
errorHandler.errorOccurred("Failed to export file", exp);
}
}
}
Obviously, somewhere, you need a implementation to handle the callback ;)
You might like to have a look at the Exception trail for more information

handle toolbar actions on eclipse plugin

i'm writing a plugin for eclipse. my problem is: i wrote a class MyEditor extends MultiPageEditorPart to edit my files, and a class MyContributor extends MultiPageEditorActionBarContributor to add actions to the toolbar.
so far i can see the buttons on the toolbar added by MyContributore.contributeToolbar() but they are always deactivated, even when i select some editparts in my editor.
i can get it working with "normal" editors (i.e. extending EditorPart), but i don't know why it doesn't work for multi page editor.
besides the usual implemented methods, here are the init and createPage overridden method I wrote in MyEditor (named XALDesignerMultiPage, in the snippet below), as required by the comment:
#Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException
{
super.init(site, input);
this.setPartName(input.getName());
XALDesignerMultiPage.site = this.getSite();
//this.model = new Program();
try {
this.model = XALInput.parseXALFile((FileEditorInput)input);
} catch (Exception e) {
// ... do something
}
}
...
#Override
protected void createPages() {
try
{
for (Automaton currAut : this.model.getAutomata())
{
createGraphicalEditor(currAut);
}
}
catch (Exception e)
{
// ... do something
}
// ... other stuff
}
...
private void createGraphicalEditor(Automaton currAut)
{
try
{
IEditorPart editor = new XALDesigner(); // XALDesigner is an instance of single page editor
int index = this.getPageCount();
addPage(index, editor, new AutomatonInput(((FileEditorInput)getEditorInput()).getFile(), currAut)); // AutomatonInput wraps the single page input
String autName = AutomatonInput.defaultName;
if (currAut != null)
{
autName = currAut.getName();
}
setPageText(index, autName);
}
catch (PartInitException e)
{
// ... do something
}
catch (Throwable t)
{
// ... do something
}
}
thanks in advance

Categories

Resources