Short and sweet: I'd like to be able to run another .java file, from the click of a button in a GUI in Netbeans for Mac.
A little more explanation:
I am recreating a project I created when I started with java, to make it cleaner. I have done a lot of improvement, though one problem I had with it is that I had too many panels in one JFrame, and I ended up having large amounts of code setting visibilities whenever I change panel.
Eg:
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
I know I could just live with it, but I'd like to see if there is a way to separate the code/panels a bit more, as described in the following.
My first thought was to have multiple files; one for each page. Similar to how you would in html.
Therefore, I have LoginPage.java, and GuestMainPage.java.
If it is possible, I'd like to be able to link them, like: close LoginPage.java and open GuestMainPage.java, on the click of a button.
Basically, I want to be able to make it compile or open another .java file, when I click a button on my GUI.
(In case it helps, the file would be in the same project.)
Example If there is a way to open another file. Possibly by accessing terminal commands?
public class LoginPage extends javax.swing.JFrame {
//Excluded a bunch of generated code from Netbeans
private void Login(java.awt.event.MouseEvent evt) {
//Code executed on Mouse Click of a Button labeled Login.
if(username.equalsIgnoreCase("guest")){
//A bit of pseudocode
open(GuestMainPage.java);
close(LoginPage.java);
}
if(username.equalsIgnoreCase("admin")){
open(AdminMainPage.java);
close(LoginPage.java);
}
}
}
From research and experience, I know you can access other files, or other classes within the same project - thus the use of objects and such - but I don't know if you can physically run the other file. I'd be able to live with the possibility of being able to access terminal through java. (And then have the power to do basically anything, I just cannot find how to do that either.)
I have looked through a lot of stackoverflow topics to try and find an answer, as well as a lot of googling, but I cannot find one that really answers my question. However, if there really is no way to do this, then I am perfectly happy with an answer confirming that, and if that is the case, I will just have to cope with multiple panels.
Thank you
-Ewan
No, you don't "open" other files like that, nor do you want to. Instead you compile all Java files to class files and use other classes. You can load classes on command using a custom class-loader, but I sincerely doubt that your application really needs that. Instead it just needs some MVC (model-view-controller) structuring (as noted by ilea -- 1+ to them) and forethought.
One decent solution to consider is to use a CardLayout, since this can greatly simplify changing GUI "views" or components shown in a top-level container such as an applet or a JFrame. You can read up on how to use these guys here: How to use CardLayout
You should try implementing an MVC pattern. Take a look at spring: http://www.springsource.org/
As I see that you are going to do as same as me. Please start this from the little. Let's suppose that I have a ".java" file and I want to run it with the JFRAME button, so can I create this ".java" file into a JAR file and then with the JBUTTON I can run that JAR file with this code.
try {
Desktop.getDesktop().open(new File("Path of the *.jar File"));
} catch(IOException ex) {
System.out.println(ex.getMessage());
}
Try this.
Related
I've made a code which displays an array which shows traffic lights flashing, should I save externally as script files or embed it into the HTML.
I also need to explain why I've done this.
Please I've got until over Christmas to finish this, I've looked everywhere and can't find much, so I'm turning to this where apparently experienced programmers are and who have done all this before.
Please don't close the question, I really need help. Thanks
What is the ultimate goal?
Code maintainability - use external script
Reuseability - use external script
Downloadtime/network i/o - a bit better if embedded.
Want the HTML/CSS redone by designer later - use external script
The more I think of it, the more reasons I see to put it extern.
And tge one I listed pro embed - that's nullified at second load (script from cache)
Revering to the MVC-Pattern I'd put it in an extra file. It's just better structured and better to read.
Another advantage is that you can reuse your code. Like if you'd have a second page which uses this code you would just need the include it there, too.
I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.
I have a .jar of a game that I would like to embed inside a JFrame of my own program. So when I run my program, it will also launch that .jar and it will be wrapped in my JFrame. I'm not sure if this is even possible, haven't found a way yet, so any guidance is great :)
I can't offer much more context because even after an hour or so of googling I'm still completely lost on where to start, if you need any other information feel free to ask and I'll get it asap.
A .jar file is not a swing widget; it's Java archive and can contain class files. What you want to do is add the jar to the classpath, and then load a JWidget class from there.
The only way I know how to do this by hacking the jar. Create a new project, add it to your class path. Look at the manifest and what class the main method is in.
Then create your own main method which calls MainClass.main(args);
Then do a sneaky Frames.getFrames() as described here Get Any/All Active JFrames in Java Application?. Once you have a handle the jframe you can 'steal' the components and add it to your own and presto!
I am trying to open notepad++ in Jpanel but am not able to do it. Can anyone please help me on how to do it?
I read about an API JDIC but didnt find anything about opening text editors in JPanel. Is there any other API which can help me accomplish the task.
Notepad++ is a standalone application. It is not distributed as a dll from which you can reuse certain elements, nor is it a system control.
You can only run it as a separate process from java, but you will be otherwise unable to integrate with it.
You can, however, take advantage of syntax highlighting of JSyntaxPane component - perhaps that will be enough.
What you are trying to do is to embed notepad++ in JPanel which is not possible. A JPanel can only contain JComponents
If all you need is syntax highlighting and indenting facilities that notepad++ provides then you need to use JSyntaxPane.
Download: http://code.google.com/p/jsyntaxpane/
Use Runtime.getRuntime().exec("your_executable_file") for run your application in separated process. Read it.
I'm making a program that needs to be able to let Clients change a setting, and using what I'm calling a "Builder", create a .jar that replaces some constants in a class with their settings.
In other words, I have a GUI that has a few textfields so that when they press the JButton labeled Build, it creates a new Runnable Jar that in a Constants class whose settings are changed with what was in the textfields.
Is this possible? I've heard about ANT Scripts, but I'm not really sure if that's what I'm looking for here.
Thanks
have you considered using a .properties files or something similar instead? You can use ant scripts for what you are describing (check out http://ant.apache.org/manual/Tasks/replaceregexp.html, you could use this task in your build.xml to dynamically change the .java files but it seems a little kludgy) but it might not be the best solution.
Check this page: http://www.mkyong.com/java/java-properties-file-examples/ which has some detail about saving to/loading from a properties file. You could set up your constants class to load it's state variables from this file, and set up the Build JButton to create that properties file.
I'm trying to think of a use case where you would want to modify the class source itself rather than use a properties file, but to be honest I can't. So I suppose you may have some special circumstance where this is not a tenable solution for you, but 99% of the time this is how I would suggest you go about it.