I'm trying to make a hello world form in Intellij. I've created the form, but the question now is what code to make in main() to make the form run and show up?
PS: all the tutorials around seem to only focus on "how to do forms on intellij" not in "how to actually make it run, then".
Thanks
Go to the class with the same name as the form.
Press the keyboard shortcut for "Generate". It's Ctrl+N on Mac OS X, Alt+Ins on Windows. Alternatively, from the menu, select menu Code → Generate.
Select "Form main()".
Now the main method is written and inserted for you. It will look something like this:
public static void main(String[] args) {
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
I just did my first Intellij Swing App.
Steve McLeod has the right instructions, however, when I tried to generate the main method using Alt+Insert => Generate main, I received an error message about one of my panels not being bound. So I clicked on the gui designer page (.form), selected my top panel, and gave it a name.
Everything else was named for me, but for some reason, the panel name was blank. Once I did that, I was able to switch over to the form .java class, press "Alt+Insert" and generate a constructor (not required, but needed).
From there, I followed Steve's advice to generate a main method. One thing that threw me off was the expectation that my Intellij generated .java class would extend or implement something swing related - it didn't. Swing only shows up in the Intellij generated main method (besides the private variables).
Check this tut while it is realy step-by-step:
JetBrains JavaFX HelloWorld
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.
My problem looks like:
I have an external application written in java which I start.
Next is to start my application. It should find the JTextFields of external application and set proper text values for them. At the end, fires the JButton which is placed somewhere on this window.
I have already tried the solution Java search for on-screen text field, but somehow it cannot find the specific sub-windows
the output generated by this solution is:
...
Window found: EnumWindows - NetBeans IDE 7.3 Beta 2
Window found: ToolkitEventListener2
Window found: theAwtToolkitWindow
Window found:
...
The title of JFrame is ToolkitEventListener2 and it creates JMenu and JButton.
I hope I explained the problem well and you will be able to help me.
java.awt.Window class has a static method
public static Window[] getWindows()
Use this to get list of all existing windows. Find proper JFrame (or JDialog) by title or by focus and get all the child components (recoursively). Filter out all JTextFields and use their values.
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
Create a project in NetBeans and create a new JFrame.
Use the GUI Builder to drag some components like a button or label onto the frame and look and the source. You'll see by default that the member variables are private in the frame class.
Now go to Tools -> Options -> Misc -> GUI Builder and change something like the variables modifier to protected instead of private.
Now how do you apply those changes to the already generated code? I've tried several things like format code, fix code, etc. I've even tried cutting all the components off of the frame and then repasting them hoping to fix the issue, but it still uses the old settings.
When I create a new JFrame in the project and perform step 2 again, the changes have taken effect. Any new code generated on a new frame or file works as expected, but not the original.
This is very strange behavior, and I have to imagine there's an easy straight forward way to regenerate this code. Am I missing something?
I'm using NetBeans 7.1 and Java 7u2. Thanks in advance!
As you have already alluded to, the GUI Builder options are defaults only, for the creation of the form.
You can change most things about already-generated GUI elements.
To change the GUI components 'access' from private to protected, right-click the component in the GUI designer and select "Customize Code". At the bottom of the "Code Customizer" dialog you can change just about any aspect of the declaration of the GUI element. That dialog also lets you customise things like the constructor used for the element.
I would recommend you leave the access default at private, and only change the elements that you really need to be protected or even public.
And don't listen to the doom-sayers. We have over 600 GUI-designed forms in our application, we use the GUI designer every day, with multiple developers, and we very rarely have any issues at all.
By the way, we are using version 6.9.1 of NetBeans with Java6, so YMMV.
When i make a GUI on netbeans, and i hit run on the top, nothing happens. it just says "build successful". i dont know how to make it do this. any help would be great! thanks
ps: i've googled it and I cant find anything on the subject.
pss: i just remembered. i saw onetime when i was at a friends house, he has the same problem. all he had to do was add a code somewhere to make it pop up. idk what or how, but thats what i remember. idk if it will help though.
Alternatively, you can create an instance of your GUI (let's say it's called, GUIDemo) in your main class and then you set it visible. You'd do this like this:
GUIDemo gui = new GUIDemo();
gui.setVisible(true);
Note that not all versions of Netbeans automatically create that constructor, you would have to write it yourself. In the source code for GUIDemo, you'd add this constructor:
public GUIDemo()
{
initComponents();
}
It may be happened that your main project setup was not correct.Right click your project from left side project explorer pane and select set as main project.
I figured it out. all i needed to do was delete the current main class which was a useless .java file and set my main class as the jframe file! thanks for the help though