I have written a programme in java for command line. Now to convert it to gui I used Netbeans GUI Builder. The problem is I do not know where to put my initialisation codes(from the old main class).
There is a main in gui but I do not think I can put there all those codes. Even then I do not think it would not be a good idea. So how can I run my initialisation codes from old main class?
I believe you would have the beginnings of this from Netbeans, correct?
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
... some stuff here automatically created by Netbeans (leave it).
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
//enter initialization code here
Main mainWindow = null;
try {
//enter more initialization code here
mainWindow = new Main();
} catch (IOException ex) {
System.exit(1);
}
//enter even more initialization code here
mainWindow.setVisible(true);
}
});
}
Of course, edit as you like. I would highly recommend that you DO use Netbeans automated features, especially if you're new at creating your own GUIs. Copy and paste your code from your command line app right into this automated main. Hope that helps.
Related
public class MedicalCenter {
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(() -> {
new Login().setVisible(true); <----------
new TimeAndDate().setVisible(true); <----------
});
}
}
Are these lines used just to start showing main windows of swing application?
new keyword is always used while creating objects
They do create objects but you didn't kept their reference.
So you cannot access those objects later on if you want to.
Currently teaching myself advanced object programming and I've hit a snag when trying to write a Javafx solution to a given Swing solution.
The problem was to develop an RMI program with Polling. I have the Swing version working perfectly, and I believe I've narrowed the problem down this section of code.
Swing
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new PollClient().setVisible(true);
}
});
}
JavaFx
public static void main(String args[])
{
Platform.runLater(new Runnable(){
#Override
public void run()
{
launch(args);
}
});
}
Any advice would be appreciated.
Platform.runLater specifies in its documentation that it executed on the JavaFX application thread at an unspecified time in the future. For this thread to exist you must first launch your application.
This means that since the application isnt launched the action to launch the application is never executed.
Regardless, JavaFX should be launched from within the main function, no need to insert a delay:
Application.launch(PollClient.class, args)
I am creating a small GUI java application that it will store some user credentials in a file.
If the file is missing or has the wrong properties then I want a pop to get brought up that will inform the user to register his credentials (so a new file can be created with the proper ones).
I have nailed down the logic of when the file is incorrect and/or missing but what I can't figure out (due to my inexperience with JFrame) is where exactly in the code to check if the user needs to enter his credentials so he can be prompted.
Let's say that the function showWarning() is the one that will check and display the popup if needed and this is my main JFrame function (this was generated from Netbeans mostly):
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
Do I put the showWarning() function inside the main function? If yes, do I put it right after new GUI().setVisible(true);? What is the proper way of doing this?
EDIT: I am stumbling to the same problem I did before. This is my showWarning() that I drafted quickly for testing purposes:
public void showWarning(){
File propertiesFile = new File("config.properties");
if (propertiesFile.exists() && propertiesExist(propertiesFile)) {
JOptionPane.showMessageDialog(rootPane, "Creds are ok");
} else {
JOptionPane.showMessageDialog(rootPane, "Creds are not ok");
}
}
The problem that I am having is that I can't make this method static in order to use it without an object because of the rootPane which is a non-static object. The problem that this caused is that I can't just write:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
I can't use showWarning() like that since it's a non-static method.
Do I need to have the GUI object in a variable properly or is there a way to make the showWarning() a static method?
If you want the check to run right when the program starts, you would want to put your function call after the main JFrame gui is set visible. See edited code below. Of course, I'm using the ambiguous showWarning() function here, but you should talor that line of code to your need. If calling a function, then right the function, but if wanting to call a new popup jframe you will need to do more lines of code there.
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
LoginForm login = new LoginForm();
login.setVisible(true);
}
});
}
Now here you would want to change the variables accordingly. The LoginForm is a Jframe already created.
You probably want your popup dialog to be modal, so the program does not continue until the user has handled and fixed the problem. To do this, do not use a JFrame but a JDialog for your popup dialog and make it modal. Then you can simply put the showWarning() call everywhere you want. I think I would put it inside the main.
Use JDialog for creating the pop up.
And either you add the showWarning() method call in main like this :
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
showWarning();
}
});
}
or better you can invoke the method showWarning() when the user credentials have to be enetered in the file. If checked just before it, it would be optimal.
I noticed when i compile the class which is implemented as Runnable or extends Thread class, then the java class called from inside the protected void run() gets compiled along with it.
this is my code:
public class Main extends Thread{
Main(){
super("Main Thread-Entry Point");
this.start();
}
public static void main(String[] args){
new Main();
}
public void run(){
try{
System.out.println("creating MDI and SQL threads");
SQL sql = new SQL();
sql.main(null);
Thread.sleep(10000);
MDI mdi = new MDI();
mdi.main(null);
System.out.println("thread created successfully");
}
catch(Exception ie){
ie.printStackTrace();
}
}
}
i don't know if this a common thing that a java programmer should know generally.
please explain why this happens??
Yes, javac automatically compiles source files within your source path your code depends on.
Other IDEs even resolve the reverse dependency, compiling source files that are part of your project(s) and depend on the class you are just compiling.
I just wanted to add space invaders as an easter egg to a program I made.
I found an open source code Here
The game runs fine on its own. However when I try to run it from a button in my program it makes my program and that game freeze. I just took his source code and added it to mine, so it is in the same jar file.
I tried 2 ways to start it.
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Game.main(null);
}
});
}
and
plain old Game.main(null);
Should I be starting this some other way?
public static void main() {
// Start the main game loop, note: this method will not
// return until the game has finished running. Hence we are
// using the actual main thread to run the game.
SpaceInvaders si = new SpaceInvaders();
Thread SpaceInvaders = new Thread(si);
SpaceInvaders.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
new SpaceInvaders().gameLoop();
}
}
Just had to create it's own thread for it.