I've created a new Java project without main class in NetBeans and then I've added a jApplet Form (let's call it MainWindow.java) to my project package. After that, I've added few other jApplet Forms that represent different "pages" of my applet application to my package using the GUI builder of NetBeans.
When I run my applet, I can see the MainWindow form with some buttons, label, etc. on the AppletViewer just fine.
Now, I want to make an event when one of my buttons on my MainWindow is pressed, to show another jApplet Form that I've created earlier and show that form instead of the MainWindow form. I'm trying to create a simple navigation system in my jApplet, and I don't know how to make it efficiently!
Can anyone help me with the code that I should write in the MouseClicked event of my button to make it show another form?
Basically, you can't (or shouldn't) design applets like this. There is no effective means to switch from one applet to another.
Instead, you should create one master applet and using something like CardLayout, design separate forms using one thing like JPanel
You'd then able to switch the forms using the CardLayout manager as needed
Related
I'm working on a GUI in Window Builder and I want to import some of the objects I have declared on my main. The GUI is in the same package as my main and 5 classes, 1 parent and 4 child classes. Is this possible to do? As of right now, I'm trying to call my characters for the game I created into the GUI to be displayed when a certain button is pushed.
So far, I tried importing each class into my GUI. That didn't work went into my main and added my GUI by declaring it as a new object followed up by adding EventQueue.invokeLater and making that visible, but that didn't really work. Any thoughts on how to do something like this?
On Window builder Palette, click choose component and search for your class for example
package.class. This is the image Refer to this.
I'm currently learning how to create a program with Java and jFrame. One problem I have is that I cant create new "forms" (how they are called in visualbasic) or windows. I'm using "Java-Editor", a usually very simple editor for things like that. Can anyone help me create a new form?
Thanks for your help in advance,
Till
From JFrame I assume that you are using Swing, and that you want multiple windows for your application. Your app should only use one JFrame object, if you need more windows (usually these are popup messages) then you can use dialogs. The class for this is JOptionPane.
Here is an example:
JOptionPane.showMessageDialog(null,"title","content",JOptionPane.INFORMATION_DIALOG);
As you can see you can control the title and the content displayed. Note that for the content parameter you can pass any JPanel object, so you dialog can display a lot of things.
The first parameter is the owner of the dialog, now I just set it to null, which means the dialog will have no owner. If you store a reference to your JFrame object then you can pass this for example and the dialog will always appear above your main window.
The last parameter is just for the general styling of the dialog. You can set it to other message types like ERROR MESSAGE too.
More info about displaying simple dialogs: https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
I have a Java application which displays results on a JPanel. The results are displayed using HTML by using a JLabel.
Now I want to let the user interact with the local application by allowing local methods to be called when the user clicks on a link in that HTML document.
Is this possible?
To answer you question, then, it is possible, however you cannot use a JLabel, you need to insert a JavaFX component, and then you can set your class as a window variable on the DOM, and thus your methods can be called from JavaScript.
Have a look at this answer on this question. It looks like they are doing exactly what you want.
I've spent the last few days putting together a game that runs as an Applet, but I would also like to have it available as an Application.
I've tried placing the applet in a frame, but I only get a black screen (and the occasional menu screen) when doing so. The applet still runs and the sound still plays, but there is nothing else.
I would like to not have to break down the code and rewrite it for a standalone application, as it's time consuming, but if there's no other way, then I'll do what I have to do. The program is a little complex in that the "main" class (containing all the media information, game related events, etc.) is an extension of an Applet class (containing the KeyListener, init() event, run() event, etc.) so maybe I am just trying to place the wrong thing in a frame?
Here is a link to the src folder for the game. I have tried putting both the AWPASG class and the Game class in a frame and seen the same results for both.
Any help would be greatly appreciated.
Source Code/Media
http://www.mediafire.com/?4eslqqr4aoh33j1
That's because your class design is not very good. You've bunched everything together in 2 classses, isntead of separating at least the UI Widgets (panels, etc, etc) from the UI Container.
Also you're using the old style AWT Applet and Panel. You should use the new Swing JApplet and JPanel (and all opthers JXxx components)
In short, try isolating your GUI building (painting) into a class that extends JPanel (or contains a JPanel). Then you can have 2 separate startup classes, one that puts your game JPanel inside a JApplet and one that puts it into a JFrame (and thus gives you the possibility to start it as a Desktop application).
Also you might want to separate your GAME init logic from the Applet init lifecycle, your game should be able to initialize without caring about the underlieing GUI technology.
I am currently writing an application where the user has, at some point, to click a button which have been generated at run time. I know how to do it when writing all my swing code from scratch, but I'd like to take advantage of Netbeans' visual editor.
The generated UI code goes into an initComponents() method I can't modify since it is regenerated automatically from the visual form.
I'd like to have a panel I place at design time using the visual editor in which I could add the buttons at run time so that they fit nicely in the layout, but I don't know how to access the panel in a convenient way. Besides, there may be another method than using a panel.
So basically :
How do I locate a Swing component at run time ?
Is there a better way of integrating components created at run time in a generated Swing UI ?
Thanks for your help.
NetBeans-generated GUI classes store all the components in private variables. You can add a method into the generated class that returns the panel, and it will remain even as you do additional design.
If you're going to use a generated UI, then it's probably best to use a JPanel within that UI to "carve out" space for your own components. Otherwise, you'll have to worry about how your components affect the layout of the components placed by the UI.
Just because you are using NetBeans generated GUI classes doesn't mean that you have to use the Group layout for the panels. I find that switching it to a BorderLayout helps especially in cases where I want to add some dynamic user interface code.
It is possible to change private to protected/public by either right clicking on a component in the GUI-Designer, choosing properties and hitting the Source-tab or right clicking on a component and choosing "Modify Source" (or something like that) and setting the appropriate access modifier.
Or just export them via a getXYZComponent() method.
Locating the component should provide as being too difficult, as you built it with the designer and thus know each component.
For example, if you had a JTabbedPane and wanted to add tabs to it when the user hits a button or something like that, you would simply issue myJTabbedPane.add(myCustomComponent); et voila, a new tab appears.
It is also possible to modify the auto-generated code and/or the code used for auto-generation by using the "Modify source" dialog mentioned above, which can be really useful.