I've been trying to run gui in netbeans using java swing but it won't run at all. All the projects that I execcute that uses jframe does not run. Any tips on how to fix this thing? This is how it looks like:
I got this to run with no problems using NetBeans IDE 8.2. Let's start from the beginning.
Delete your current OfficeManagementSystem project from your IDE and local drive system. Select the OfficeManagementSystem project located within your IDE Project pane located on the left side of the IDE. Right-click on it and select the Delete popup menu item. A Delete Project dialog is displayed. Check-mark the Also delete sources under ..... checkbox then select the Yes button.
Close NetBeans!
To make sure all is gone, open your file browser (in Windows10 this is named File Explorer [win key + E]) and navigate to your NetBeans Projects folder. Default location in Windows is C:\Users\{yourAccountName}\Documents\NetBeansProjects\. Make sure that OfficeManagementSystem project folder is deleted.
With your web browser, go to the Source Code Web Page and select the Code button:
Then from the dropdown menu select Download ZIP:
A Save File dialog will appear possibly similar to this:
Select the Save File option then select the OK button. Choose a location to save the ZIP file and remember it.
Navigate to you downloaded file named HospitalManagementSystemusingJAVASwing-master.zip and Un-Zip it with your favorite decompression software like WinRar or 7Zip. Have it unzip into its own folder.
Navigate to and open the the folder created by your decompression software, it should have been named HospitalManagementSystemusingJAVASwing-master. If done properly you should have yet another folder (a sub-folder) also named HospitalManagementSystemusingJAVASwing-master. Rename this folder to HospitalManagementSystem by right-clicking on the folder and selecting Rename from the popup menu. Delete the unnecessary text within the name then hit your ENTER key.
After Renaming the folder, once again right-click on it and select Copy from the popup menu.
As described earlier, navigate to you NetBeansProjects directory (folder) and double-click on it. Paste the new HospitalManagementSystem folder into the NetBeansProjects folder. This project will now be added to the NetBeans Projects List.
Start NetBeans!
Once NetBeans has completely started, from the menu bar select the File ➞ Open Project... menu item. The Open Project dialog will be displayed. Scroll down the left pane of this dialog until you see HospitalManagementSystem and select it. Now hit the Open Project button. This project will load into the NetBeans IDE Projects pane located on the left side of your IDE under the name of: OfficeManagementSystem. Why this person changed the project name at this point is beyond me but to each their own.
Expand the project and its active package (officemanagementsystem) so as to expose all the classes involved. You should see the following listed:
LoginPage.java // The first Form we want displayed.
MenuPage.java // The Menu page (once login is achieved).
Module1.java // Should really be named PatientPage
Module2.java // Should really be named DoctorAvailabilityPage.
OfficeManagementSystem.java // The application start-up class.
This application still will not run since it contains nothing within the main() method of the start-up class (OfficeManagementSystem). Open this class into the IDE Editor by double-clicking on it.
Expand the main() method code block and add this code line into it:
new LoginPage().setVisible(true);
Run the application! The first Window displayed will be the Login Page. Username is: admin and the password is: admin. (change in code if you like). And away you go. :)
All java applications need a static main method as a bootstrap.
It should look something like the following:
public static void main(String[] args) { YourJFrameClass frame = new YourJFrameClass(); }
That should get you started.
The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.
It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.
Check if your JFrame Class has this method:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new YOURFORMNAME().setVisible(true);
}
});
}
The setVisibile(true) allows the form to be visible to the user. The main method is used to define the entry point for the application therefore this method is needed to run the JFrame
Related
I am currently doing a free Java course online called MOOC made by the University of Helenski. One challenge requires us to create new packages and such. The main issue I was having was that my code would not compile because it would refuse to acknowledge the Main class I created for it, even after I manually selected it (see one of the screenshots below).
I have found solutions of this challenge online and all the packages are created in a folder called "Source Packages" . The problem I am having is that every-time I create a new package, it automatically shows up in the "Test packages" folder.
I tried looking online to see how I can create a "Source Packages" folder on Netbean but I have not been successful in finding out how to go about this because this challenge is not letting me create a "Source Packages" folder. I am fairly new with Java and any help is appreciated. Thank you.
It seems that you have just created the wrong project type. Is it possible that this is a TMC project and not a normal Java-Application? Then it would make sense, that there are only test-packages available.
If this folder is missing, you can create it like this:
1.) Create a new folder:
Right-klick on your project in projectexplorer -> new -> other:
New-File-Window opens: on the left list klick "other", on the right list klick "folder", then klick next.
Now you can choose Foldername and Location. Click finish and the new folder is created.
2.) Set the new created folder as new source:
Right-klick on your project, select "properties", then on the opening "Project-Properties" Window in the list on the left side klick "sources", then on the right lower list you can add the created folder (klick add...)
Finally click ok and the folder should appear in your Menu.
I made a java application.and bundled all classes in jar file..wen i run the project from netbeans my app is running successfully..but wen i place my .jar file at another place and run from there..i am not getting the icons used by my application..In the code i get my icons from images directory present in project folder.
Now,i wanted to know how can we present these image files to the end user (like we present .jar file).Thanks in advance
It seems like there are two questions here:
How do I get NetBeans to include an image file in the jar produced when I build my project?
How do I access an image file from a jar?
This answer applies to NetBeans 6.8 and addresses both of the subquestions.
Assume that you have an ant based Java Application Project.
Here is the 'Files' view of the project
JP
+ images
+ test.jpg
+ nbproject
+ src
+ jp
+ Main.java
+ test
+ build.xml
+ manifest.mf
Inside your Main.java you have code like this:
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
When you run this project from inside NB you get this output:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
When you run the code packed into the jar, you get something like this:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.txt false
To get something better when the jar is executed, you need to do the following:
Add the images directory as a source root for you project.
Right click on the project and select the Properties item. A dialog will appear.
Select 'Sources' in the list that is on the left side of the dialog. This will change the content of the panel on the right side of the dialog.
Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.
Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.
Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.
Change your code to use Class.getResource().
public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
URL url = Main.class.getResource("/test.jpg");
System.out.println(url);
When you run the project from inside the IDE, you should see something like this:
/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg
When you run the code packed into the jar, you will get something like this:
bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.jpg false
jar:file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar!/test.jpg
After you get the URL for the test.jpg file, you can use ImageIcon(URL) to create the icon
For a Maven Project in NetBeans 6.9, put your filen into Resources in the "Other Sources" folder of the Project.
And be sure you dont forget the leading "/"
I develop plugin to netbeans IDE and i would like to determinate user to select a open projects in netbeans IDE in some action:
How it is possible to call this window and get some reason from user action on it?:
I am not really sure what you want to achieve, but you may take a look on the following code:
Lookup gLookup = Utilities.actionsGlobalContext();
Project currentlySelectedProject = gLookup.lookup(Project.class);
// do something with the project, e.g., print out its name
System.out.println(ProjectUtils.getInformation(currentlySelectedProject).getDisplayName());
This code gives you currently selected project in the projects window of NetBeans IDE (therefore the project has to be already open to get it). If you need to follow changes to it, you can add a listener for it:
Lookup.Result<Project> globalResultOBJ = gLookup.lookup(new Lookup.Template(Project.class));
LookupListener globalListenerOBJ = new LookupListener() {
#Override
public void resultChanged(LookupEvent le) {
currentlySelectedProject = genlokup.lookup(Project.class);
// again you can do something with newly selected project
System.out.println(ProjectUtils.getInformation(currentlySelectedProject).getDisplayName());
}
};
globalResultOBJ.addLookupListener(globalListenerOBJ);
This codes will give you access to currently selected project. Is that what you need or dou you want explicitly ask the user to open a new project? Meaning, that you want to programmatically open the run the Open project action?
This question already has answers here:
Error: Selection does not contain a main type
(24 answers)
Closed 4 years ago.
I downloaded eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz. This eclipse is built-in with java and my Lubuntu is 64-bit. Whenever I compile and run a simple code in java like this one below:
public class Sample{
public static void main(String[] args){
System.out.println("YOLO");
}
}
I always get an Editor does not contain a main type.
I put the file in a project folder called Sample. This eclipse should compile java code because its an IDE distribution specialized for java.
How can i resolve this error?
Any help would be much appreciated.
Here's my project structure:
Problem is that your folder is not identified as a Source folder.
Right click on the project folder -> Properties
Choose 'Java Build Path'
Click on 'Sources' tab on top
Click on 'Add Folder' on the right panel
Select your folders and apply
I just had this exact same problem. This will sound crazy but if someone sees this try this before drastic measures. delete method signature:
public static void main(String args[])
(Not the body of your main just method declaration)
Save your project then re-write the method's header back onto its respective body. Save again and re-run. That worked for me but if it doesn't work try again but clean project right before re-running.
I don't know how this fixed it but it did. Worth a shot before recreating your whole project right?
I suspect the problem is that Sample.java should be in a package inside the src folder.
I guess that eclipse will not automatically look outside of there.
Right click your project > Run As > Run Configuration... > Java Application (in left side panel) - double click on it. That will create new configuration. click on search button under Main Class section and select your main class from it.
First look for the main method is there or not.If it is there, do restart your eclipse and right click on the page which having main method, Go to run as Java application.
Make sure you do Run As > Java Application.
If not you could try a Project > Clean
Some more questions that deals with this that could be helpful,
Refer this
Create a source folder under Java Resources
For me, classpath entry in .classpath file isn't pointing to the right location. After modifying it to <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> fixed the issue
Ideally, the source code file should go within the src/default package even if you haven't provided any package name. For some reason, the source file might be outside src folder. Create within the scr folder it will work!
Right click on Sample.java file and delete it. Now go to File -> New -> Class , enter name of program (i.e. hello) , click on finish . It will create file hello.java. Enter source code of program and finallly press ctrl + F11
Right click on your project, select New -> Source Folder
Enter src as Folder name, then click finish.
Eclipse will then recognize the src folder as containing Java code, and you should be able to set up a run configuration
I had the same issue. I had accidentally deleted the .classpath and .project file in my workspace. Luckily it was in Recycle bin, as soon as it was restored, there were no issues.
Just change "String[] args" to "String args[]".
I use Netbeans 7 on Kubuntu 12.04. I start Netbeans like this:
netbeans --cp:a /usr/share/java/xercesImpl.jar &
cause of known issue with Netbeans and Kubuntu 12.04. In the middle of my project my palette with Swing components just .. disappeared!
I see only sth like this:
How to restore default Netbeans palette?
I checked Ctrl+Shift+8 shortcut but with no results - the problem is still the same.
**EDIT:
I dont know what I did but now I see ONLY EMPTY palette :( : **
I also tried this:
1) I cd'ed to /home/mazix/.netbeans/7.0/config/Windows2Local/Groups/commonpalette
, ls'ed and saw only this: CommonPalette.wstcgrp
2) cd'ed to /home/mazix/.netbeans/7.0/config/Windows2Local/Components and saw this:
AnalyzerTopComponent.settings output.settings
AnalyzeStackTopComponent.settings projectTabLogical_tc.settings
breakpointsView.settings projectTab_tc.settings
CallHierarchyTopComponent.settings properties_1.settings
callstackView.settings properties.settings
CommonPalette.settings refactoring-preview.settings
ComponentInspector.settings
ReporterResultTopComponent.settings CssPreviewTC.settings
resultsView.settings debugging.settings
search-results.settings DeclarationTopComponent.settings
services.settings evaluator.settings
sessionsView.settings favorites.settings
sources.settings find-usages.settings
StyleBuilderTC.settings gsf-testrunner-results.settings
svnversioning.settings hgversioning.settings
synchronize.settings JavadocTopComponent.settings
TaskListTopComponent.settings localsView.settings
TerminalContainerTopComponent.settings
MultiView-java#007Cform#007C_1.settings threadsView.settings
MultiView-java#007Cform#007C_2.settings versioning_output.settings
MultiView-java#007Cform#007C_4.settings watchesView.settings
MultiView-java#007Cform#007C.settings Welcome.settings
navigatorTC.settings
and didnt see any _hidden files :(
Open NetBeans and choose Window -> Palette
It will appear but it will be empty because you didn't choose a GUI class yet.
To fill the palette with the GUI components like labels, follow these steps:
Create a new project .
Right click in the package name or project name, and choose New -> Other...
In the New File dialog box, choose the "Swing GUI Forms" from category and the "JFrame"
Form file type sub window. Click Next.
Enter name lets say "MyGUI" for the new the class which you created now.
The palette window is now full with components like labels, text,checkboxes, ...
if not appear yet make sure to press "Design" button in the upper of the GUI class bar "MyGUI"class
I got the same issue and this work for me window -> reset window. hope it work for you :)
I think the same problem occur here.Check this solution.it may helpful to you.
This steps for restore the default palette?
Exit the IDE.
Go to your user directory and go down to config/Palette subdir. In this directory, the files or directories deleted from the default palette content are presented as empty files with _hidden suffix in their names.
Delete these _hidden files to let the palette categories and items appear again in the IDE.
I think this will help you
Do one thing, simply right click on your netbeans project and then open it. It resolves the problem. You will observe if u open a new project the palette is there, so just copy the project with a different name, its will solve your problem.
In my case after creating a new project without a default constructor, the search box at the palette holds a single character i.e. '='. this results in an empty palette too.
In that case the solution is obvious: just delete the content of the search input.
It puzzled me for a moment, though, not noticing the search input field content.
I had the same situation with my netbeans. The only solution I did was to backup my project folder (saving it to other drive). Then re-install the netbeans. Afterwards I re-paste the project folder at the directory and re-open it.
Right-click mouse button at palette box. Choose command "reset palette".
I encountered the same problem. Just check the bottom of the palette to see if there is a search box open. If there is, close it. Then the palette will be available once again.
So, nothing suggested here worked for me, I am leaving my solution.
You create the project, name it.
Create a package.
Click on the package with the right mouse button
select New filme
select Other
select AWT GUI Forms
select Frame form
Now you should see the menu at the right part of your screen with the option to create buttons, labels, etc.
As of 2021 for mac users;
Open your NetBeans app and click on "windows" located at the top menu of the application. A drop down will appear.
Scroll down and click "IDE Tools". You'll see another dropdown containing list of items including palette.
Click on the "palette". This will make the palette visible.
That's all!