I made a simple program input and data display connected to sql server 2008. Only one java class for connections (KoneksiDatabase.java) and one JFrame form for form (Form.Java). Previously I did not specify the main class for the project . after JFrame form completed I made why I could not make out the form as the main class ?
when I create a new form JFrame (Form2.java) form they will be made in the main class in the Project Properties
Form2.Java only seen in pictures that can be made in the main class , while Form.Java can not.
http://s.kaskus.id/images/2015/08/11/912158_20150811050000.jpg
Did you try something like this in your Form class?
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form().setVisible(true);
}
});
}
or even more simpler
public static void main(String args[]) {
new Form().setVisible(true);
}
Related
I'm trying to make CRUD application which can work 2 different ways:
Console application (store "Customer" data to text file)
GUI application (store "Customer" data to Database)
I want to use command line argument as a variable to choose between these 2 interfaces.
For example:
if(args[0] == 0){
startAppWithGUI();
}
else{
startAppWithConsole();
}
but I'm so confused that where my main method is using that arguments. All I can see is this:
public static void main(String[] args) {
launch(args);
}
You create a Launcher
import javafx.application.Application;
public class Launcher{
public static void main(String[] args){
if(args[0] != 0){
Application.launch(YourFXMainClass.class,args);
}else{
YourConsoleApplication.main(args);
}
}
}
And then you use the Launcher as Main Class
I want to include one java file into another. Both have the main functions in it. One file looks similar to the following:
public class FileShow
{
public static void main(String args[])
{
JFrame guiFrame = new JFrame();
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("RTL Parser GUI");
guiFrame.setSize(500,500);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
JPanel comboPanel = new JPanel();
JTextField handle = new JTextField(30);
comboPanel.add(handle);
guiFrame.add(comboPanel);
guiFrame.setVisible(true);
}
}
whereas my other java file is:
public class AnotherFile{
public static void main(String[] args) {
new AnotherFile();
}
public AnotherFile()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Assertion-Based GUI");
guiFrame.setSize(500,500);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
JPanel comboPanel = new JPanel();
JTextField handle = new JTextField(30);
comboPanel.add(handle);
guiFrame.add(comboPanel);
guiFrame.setVisible(true);
}
}
Is there any way to combine both the files and run together, since both have the main functions in it?
How do i combine both the files in same java file and run both of them together?
You just can't do that. Each Java file should have only one main method.
But you can better organize your files to do what you want:
public class FileShow{
public void doSomething(){
//...
}
}
public class AnotherFile{
public void doSomething(){
//...
}
}
public class mainClass(){
public static void main(String args[])
new FileShow().doFileShow();
new AnotherFile().doAnotherFile();
}
}
I would just add 'AnotherFile' object in the main method of Fileshow. You can only have one main method.
So in Fileshow.java, in the main method add
Anotherfile a = new Anotherfile()
From what you wrote, it is completely unclear, what you want to achieve. Include two Java classes into one .java file? Into one .jar file?
What do you mean by "run together"?
Combining two top-level Java classes in one source file is possible (according to JLS), while only one of them may be public. I believe though, it is not a best practice, just because you get quite a messy lifecycle of your classes. But if you still want to do it, you must make one of them either package private or nested.
Getting both to one jar is trivial. Just call jar cf jarname <classes>. It would also be possible to call the main methods separately by explicit mentioning them in java command line, like java -cp jarname <package>.FileShow.
Still, I'm not sure I understood your question right.
In Java, each Java file can contain one public class and by default JDK will call it's main method. If you have two classes both having a main method and you want to keep it in one Java file, the two classes can not be public, one must be an inner/nested class. I have given an example below.
public class FileShow
{
public static void main(String args[])
{
AnotherFile.main(args);;
// Your code
}
static class AnotherFile
{ // as it contains a static method
public static void main(String[] args) //or any static class
{
new AnotherFile();
}
public AnotherFile(){
// Your code
}
}
}
Logically it will work. But I highly discourage to go with this. It is not standard.
I have an interface design in netbeans and another java class file in same pacage. I want to call the class file when the button clicks. How can i do this? Please remember that i am new to java. here is the button action perform field
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
and i want to call the
public class comments {
public static void main(String[] argv) throws Exception {
class file.
Here while I run java project in netbeans all things are working okay. But after they are built there is not any item added in combobox as it works during netbeans run. The sample code is given below.
First Login JFrame
public class Login_Frame extends javax.swing.JFrame {
welcome w = new welcome();
public Login_Frame() {
initComponents();
}
//button action perform event for dispose this window and open new welcome window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
.
.
w.setVisible(true);
this.dispose();
.
.
}
}
Second JFrame
public final class welcome extends javax.swing.JFrame {
// comboitem is class in which method for adding item in combobox from sqlite
db is declared
comboitem c = new comboitem();
// textclass is class in which method for changing lowercase text entered in
text to uppercase is declared
textclass tc = new textclass();
public welcome() {
// while I try to run the project using netbeans run project option
// logincall() method initialized and work fine.
Problem
After project built when I try to run the jar file from the cmd. it runs without any
error but logincall() method doesn't work or may be not initialized.
initComponents();
logincall();
.
.
}
public void logincall(){
//Remarks
//tc.uppercase() method is working fine after built. But other c.but_stn() like
// doen't.while during running project through netbeans all thing working fine.
c.bus_stn();
c.bus_trl();
c.inq_stn();
c.editframe();
c.userlist();
c.editTrainStation();
c.editFlightStation();
c.flightFlight();
c.pickupstand();
tc.uppercase();
}
I didn't know what is wrong with it. I searched on google but didn't find any proper answer.
There also any error showing up in netbeans. Please fill free to ask any questions if more information is needed. I appreciate all your replies.
This is my Welcome Main class's main method.
public static void main(String args[]) {
...
look and feel auto generated code
....
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new welcome().setVisible(true);
}
});
}
A couple of things that might be an issue here:
1) Are you running your GUI on the EventDispatchThread? this is mandatory for java swing GUI to be able to work properly. The main reason for this is concurrency. Details here.
2) Are you re-rendering your combobox? it is important that you do this because changes to GUI elements may not be immediately shown.
3) What Database are you using? in order to determine if the fault lies in the code or the DB you could write a test using static data, if it loads in the IDE and outside of it chances are that your code is correct but the DB isn't
1- what is the difference between JFrame and FramView in Java ??
2- when I create New Desktop Application with NetBeans , FrameView is created and when I put any component on it ( by design mode) it appears beautiful like this ( see picture 1 in bottom link )
but when Application run , it appears like this !!(see picture 2 in bottom link )
and when I create JFram and put a button on it , whenever Application run the button appears like this ... !! (see picture 3 in bottom link )
why this happend ..?? I want to create a beautiful interface and component Like component on FramView but without any problem in design?
How I do that ??
Notice : I use NetBeans 6.8
see pictures http://www.freeimagehosting.net/uploads/fc54702761.png
I am sorry because I put the Images in this way , but your web site prevent me from putting image and multi hyperlinks because I am a new user
If memory serves me right, the FrameView is a custom component by Netbeans. To make your JFrame components look "better", add the following code just before calling the init() method:
import javax.swing.UIManager;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) { }
initComponents();
}
.
.
.
.
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}