I'm creating a simple GUI form with one button in IntelliJ IDEA 9. The class which was created with the form is not JFrame or any other swing class. How I can call my form in my source code?
I was stumped when IntelliJ consistently errored out when choosing Form main() from the insert menu, with an error message saying something about an invalid root component.
It turns out it means that the JPanel that is the main (root) component of your Form needs to have a field bound to it, and by default it doesn't do this.
Simply select the JPanel in the form designer, and give it a name (field name) in the property pane.
After this it will generate the main() just fine.
Simply go into the class associated with your form, press alt + insert and choose "Form main()".
Resources :
GUI Designer Basics
First add private JPanel Main; inside the class. Then, in the form's panel properties, add Main to the field name. Then, go to your class and from the Alt + Insert menu, select Main.
Now, the error wont come.
Related
How to modify/add code to the initComponents() method in Java on NetBeans? When I try to add any line of code this area seems to be like readonly and it's highlighted in gray! It's for security probably, but I suppose there is a way to disable that.
Yes the initComponents method is read only to keep full control for the IDE. You may add yours in the constructor right after initComponents.
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
myInitComponents();
}
public void myInitComponents() {
}
The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor. The method is 'guarded' to prevent this regeneration from overwriting user written code.
There are a couple of ways to add code to this method, indirectly:
Drop a new component onto the design
editor 'canvas' for the window.
Enter code as part of one of the
following code properties:
Pre-Creation Code, Post-Creation
Code, Pre-Init Code, Post-Init Code,
Post-Listener Code, Pre-Population
Code, Post-Population Code and
After-All-Set Code.
There are a couple other code properties that do not alter the initComponents() method... but can be very useful: Pre-Declaration Code and Post-Declaration Code.
alt text http://blogs.sun.com/vkraemer/resource/code-properties.png
Note: the editor for these properties is not 'rich', so I would recommend creating methods in the "regular editor" that you just call in the initComponents().
You can modify the code in the initComponents() method by positioning or changing the 'regular' properties of the 'base panel' or controls.
Presumably what you are doing is writing an application using the Matisse GUI tool.
Matisse generates non editable code blocks. This is required by Matisse so that the tool remains synchronized with the code base.
There are a number of options provided by Matisse to allow insertion of custom code before, after or within code blocks such as initComponents().
See the image below:
This shows the properties tab for a jPanel and some of the code insertion options.
If you right-click on the component in the Design View, and then click on "Customize Code" selection, you can modify the code in the InitComponent code. Several lines of explicit code can be customized.
To allow changes in both the source and the Matisse GUI editor, NetBeans prevents editing in what it calls "guarded blocks".
While you could imagine the IDE being able to interpret almost any kind of GUI code you write, in practice, it is much easier for the IDE developers to encapsulate the automatically generated GUI code in a single method (initComponents()) and leave the rest for you to edit.
If you are certain you know what you're doing, you can easily edit the .java file from an external editor, but:
be sure to save the current version somewhere
check that your changes didn't break something by opening the class in NetBeans visual editor once your done
I discovered by trial and error, that initialization which needs to be done before the user sees the panel should be added as "Pre-Init Code". In my case, I needed to populate a drop down box (called "Choice" in AWT). There seems to be very little detailed documentation available on using Matisse. So, I hope this will help others.
select all of the code and copy in an external editor like gedit or notepad.
Then delete your jframe file.
Create a new java class in netbeans in the same package with the same name.
Copy all the contents from the editor file and paste it in your newly created java class.
Close Netbeans
Go to folder path where there is your form file
Backup the 2 files with extensions ".form" and ".java"
Edit the 2 files with extensions ".form" and ".java" in notepad editor. For example if your form name is "myForm" you must have the files "myForm.form" and "myForm.java" in the folder.
The first file ".form" is xml file and the second ".java" is a code java file
Edit carefully your code in both files save changes and open NETBEANS.
Thats it!
When I try to add a non-palette component in IntelliJ 14.0.3 I get this error:
Forms added to palette must have a binding for the top-level component
The component that I try to add to a tab of a scrollpane, is a class that extends a JPanel.
I can not understand why this error. With other classes does not give me problems.
The error remains even if I select the two options.
You may add the form to a pallet and then place it as a component on another form.
To do so open in a designer a form which you want to be custom component. Click right on any group (e.g. swing) and select "Add Component to Palette". After this you can open another form and place the newly added component on it.
I solved this by changing the "field name" of the JPanel of the form that is being inserted in another form.
It seems this error appears when no field name is specified which means the error is a bit misleading.
I´m new in creating GUI in Java, I would like to implement JComboBox (which uses GlazedLists), in my JFrame, which is in another class. Is it possible to call this JComboBox in my JFrame? I´m asking because I use NetBeans (some parts of code are forbidden to edit). Thanks
You can access the reference to the JComboBox like any other variable. The name of the combo boxes variable will depend if your reamed the object or not (you can do this from the form inspector, usually found under the projects window when in design mode)
While you can't modify the code in the protected blocks, you still effect he combo box (or any other component) within your code normally, just make sure initComponents has been called first
I have to create an xml file based on the value of attributes set by user in dialog of Java Swing.There are five attributes name,age,sex,date of birth and place. when the user clicks on OK button of the Dialog box an xml file should be created in the temp directory of the user.
Kindly help me as I am new to Java Swing
Regards,
Kumar
Create a GUI with controls to collect the data you specified. Create a class to generate the XML output. Call this class.
To build a Swing GUI, you start with a JFrame.
Within this JFrame, you have a master JPanel.
The attributes will each have JLabel components and JTextField components. You will use a layout manager to arrange the label and text field components.
You have a JButton so that the user can tell your program that she's entered all of the attributes.
You should be able to look up all these Swing components and build a GUI.
Then, as jzd said, you create a class to generate the XML output and call this class from the GUI.
How to modify/add code to the initComponents() method in Java on NetBeans? When I try to add any line of code this area seems to be like readonly and it's highlighted in gray! It's for security probably, but I suppose there is a way to disable that.
Yes the initComponents method is read only to keep full control for the IDE. You may add yours in the constructor right after initComponents.
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
myInitComponents();
}
public void myInitComponents() {
}
The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor. The method is 'guarded' to prevent this regeneration from overwriting user written code.
There are a couple of ways to add code to this method, indirectly:
Drop a new component onto the design
editor 'canvas' for the window.
Enter code as part of one of the
following code properties:
Pre-Creation Code, Post-Creation
Code, Pre-Init Code, Post-Init Code,
Post-Listener Code, Pre-Population
Code, Post-Population Code and
After-All-Set Code.
There are a couple other code properties that do not alter the initComponents() method... but can be very useful: Pre-Declaration Code and Post-Declaration Code.
alt text http://blogs.sun.com/vkraemer/resource/code-properties.png
Note: the editor for these properties is not 'rich', so I would recommend creating methods in the "regular editor" that you just call in the initComponents().
You can modify the code in the initComponents() method by positioning or changing the 'regular' properties of the 'base panel' or controls.
Presumably what you are doing is writing an application using the Matisse GUI tool.
Matisse generates non editable code blocks. This is required by Matisse so that the tool remains synchronized with the code base.
There are a number of options provided by Matisse to allow insertion of custom code before, after or within code blocks such as initComponents().
See the image below:
This shows the properties tab for a jPanel and some of the code insertion options.
If you right-click on the component in the Design View, and then click on "Customize Code" selection, you can modify the code in the InitComponent code. Several lines of explicit code can be customized.
To allow changes in both the source and the Matisse GUI editor, NetBeans prevents editing in what it calls "guarded blocks".
While you could imagine the IDE being able to interpret almost any kind of GUI code you write, in practice, it is much easier for the IDE developers to encapsulate the automatically generated GUI code in a single method (initComponents()) and leave the rest for you to edit.
If you are certain you know what you're doing, you can easily edit the .java file from an external editor, but:
be sure to save the current version somewhere
check that your changes didn't break something by opening the class in NetBeans visual editor once your done
I discovered by trial and error, that initialization which needs to be done before the user sees the panel should be added as "Pre-Init Code". In my case, I needed to populate a drop down box (called "Choice" in AWT). There seems to be very little detailed documentation available on using Matisse. So, I hope this will help others.
select all of the code and copy in an external editor like gedit or notepad.
Then delete your jframe file.
Create a new java class in netbeans in the same package with the same name.
Copy all the contents from the editor file and paste it in your newly created java class.
Close Netbeans
Go to folder path where there is your form file
Backup the 2 files with extensions ".form" and ".java"
Edit the 2 files with extensions ".form" and ".java" in notepad editor. For example if your form name is "myForm" you must have the files "myForm.form" and "myForm.java" in the folder.
The first file ".form" is xml file and the second ".java" is a code java file
Edit carefully your code in both files save changes and open NETBEANS.
Thats it!