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.
Related
I'm trying to show a window for file choosing as it follows:
public class XMLElementCounter() {
public static void main(String[] args) {
elementCounter();
}
static void elementCounter() {
try {
final FileChooser fc = new JFileChooser(defaultDirectory);
int returnVal = fc.showOpenDialog(parent);
...
}
}
First i declared the elementCounter directly in the main function and i called as parent fc, which showed the window as expected, but as i modularized it, it stopped showing anything. When i use null, it shows the screen behind every other window, which is annoying.
How can i know which is the parent i'm looking for, and where can i learn about it?
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 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);
}
I have made a .jar file in Eclipse to call a paint class. When I finish it gives me an error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: Graphics/src/G1.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
Here is my code to call the paint method:
public class G1Starter {
public void main(String[] args)
{
Graphics1 g1 = new Graphics1();
g1.repaint();
}
}
I tried making a main method in the Graphics1 class but it did not work.
Add the static keyword so that the application has a valid entry point
public static void main(String[] args)
Method 'main' should be 'static'
public static void main(String[] args)
I got an error on the line a JButton is on. Can someone help? It's really short code, by the way:
public class Main {
public JButton[] grid = new JButton[9];
public void init_components() {}
public void init_icons() {}
public static void main(String[] args) {}
}
You need an appropriate 'import' statement in your source code to tell Java what 'JButton' means.
import javax.swing.JButton;
An IDE, like Eclipse, will make it much easier for you to get started here.