I have two package:
java1 package with main class.
java2 package with end class.
I want to access function from end class to main class
java1 package - main class source code:
package java1;
import java2.end;
public class main extends javax.swing.JFrame {
public main() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
end.jTPanel();
}
}
java1 package - main class source code:
package java2;
public class end extends javax.swing.JPanel {
public end() {
initComponents();
}
public static void jTPanel(){
jTabbedPane1.setSelectedIndex(1);//always error, jComponent in jPanel
}
}
can you help me?
To access the method jTPanel from main you need an instance of end in main.
In the main class before your constructor, create a new instance:
public class Main extends javax.swing.JFrame {
End myend = new End();
Then you can access the jTPanel method by calling:
myend.jTPanel();
Side note: Java convention is to use capitalised class names. e.g. Main and End as apposed to main and end.
Make an instance of end class. Then using that instance call the methods of end class.
end x=new end();
x.jButton1ActionPerformed(event);
Related
I have problem with class Main.I tried import final boolean from UHCManager.java to Main.java , and get message "'me.kczor.managers.UHCManager' is not an enclosing class"
Class Main:
import me.kczor.managers.UHCManager;
public class Main extends JavaPlugin {
public void onEnable(){
UHCManager.this.statusGame = false;
}
}
Class UHCManager:
package me.kczor.managers;
public class UHCManager implements Listener, CommandExecutor {
public final boolean statusGame = false;
When you write Class.this.xxx <-- You are requesting the enclosing class. That is why the error you are receiving is
[['me.kczor.managers.UHCManager' is not an enclosing class]]
When you write this.xxx <-- You are requesting (this) class, where you are in.
In order to access a variable from a different class, whether it is private/public/protected. Create a new instance of the class you want, and create getters/setters for all the fields you want to access/modify from other classes.
Very simple example of how to use Class.this.
What is the difference between Class.this and this in Java
I have two classes, Main and SPBHomeHelp. Here is the code for Main:
public class Main{
SPBHomeHelp homeHelp;
public Main(){
homeHelp = new SPBHomeHelp();
Home home = new Home();
}
}
Here is the code for SPBHomeHelp:
public class SPBHomeHelp{
public SPBHomeHelp(){
}
public class Home{
public Home(){
System.out.println("Entered Home Constructor");
}
}
}
Main and SPBHomeHelp are two different .java files. I can easily declare and instantiate an instance for SPBHomeHelp. But I want to have an instance of Home, which is a class nested inside SPBHomeHelp, in Main too. I tried:
Home home = new Home();
because Home is a public, but that doesn't work. How can I create an instance of Home in Main?
There are two ways to implement this:
First,, just like the answer you accepted:
SPBHomeHelp.Home home = new SPBHomeHelp().new Home();
Second, make your Home static, then, in your Main.main() method,
SPBHomeHelp.Home home = new SPBHomeHelp.Home();
will be OK.
PS: what is the difference between static inner class and non-static inner class is: in a non-static inner class, there is a this which refer to the outer class, which means you can use fields and methods of the outer class in a non-static inner class. But you can't use fields and methods of the outer class in a static inner class except they are also static.
home is an instance of the inner-class Home type (from the class SPBHomeHelp). You need an instance of that class (that ishomeHelp), in order to construct a Home instance. I think you're looking for something like
SPBHomeHelp.Home home = homeHelp.new Home();
Change the class Home to static, and static import your Home class.
public class SPBHomeHelp{
public SPBHomeHelp(){
}
public static class Home{
public Home(){
System.out.println("Entered Home Constructor");
}
}
}
Main:
static import SPBHomeHelp.Home;
public class Main{
SPBHomeHelp homeHelp;
public Main(){
homeHelp = new SPBHomeHelp();
Home home = new Home();
}
}
I have two .java files in the same package. I am planning on making the first .java file the underlying code and the second .java file the GUI swing interface.
My problem I encountered was when working on the GUI part of the project, I needed to access several methods from the .java file with the code. My .java file with the code is a like this:
package same;
public class HFSim extends ApplicationTemplate
{
private static class AppFrame extends ApplicationTemplate.AppFrame
{
public myMethodIWanttoUse()
{
//code
}
And in my GUI .java:
package same;
public class GUI extends JFrame
{
public GUI()
{
public void actionPerformed(ActionEvent e)
{
//this is where I want to use the method from above
Is there a way to get that method to be used in the GUI portion? Or is there a better way to approach this problem? thanks in advance.
You have multiple solutions to your problem. The first question you should answer is how these methods you need to call qualify themselves.
Are they utility methods? (They don't require an instance of an object to work on)
Are they attached to a single instance of an object?
Do you need to call methods of a specific object more than just methods?
You can either:
Declare them static and call them, eg HFSim.AppFrame.myMethoIWanttoUse();
Declare a static instance of the object containing them, eg
public class HFSim extends ApplicationTemplate {
public static final AppFrame appFrame = new AppFrame();
...
}
public class GUI extends JFrame {
public GUI() {
public void actionPerformed(ActionEvent e) {
HFSim.appFrame.myMethodIWanttoUse();
}
}
}
Pass the instance of the object to the other one:
public class GUI extends JFrame {
private final HFSim.AppFrame appFrame;
public GUI(HFSim.AppFrame appFrame) { this.appFrame = appFrame; }
public void actionPerformed(ActionEvent e) {
appFrame.myMethodIWanttoUse();
}
}
Make methodIWantToUse() static by replacing
public myMethodIWanttoUse()
with
public static myMethodIWanttoUse()
Secondly, make AppFrame marked as public instead of private.
Then just call you method like this HFSim.AppFrame.myMethodIWantToUse().
Edit:
Alternatively, you don't have to make your method static. Just add this in your GUI code:
HFSim.AppFrame frame = new HFSim.AppFrame();
frame.myMethodIWantToUse();
Still, no matter what, you have to make AppFrame be public.
I have a class Employee
import javax.swing.*;
public abstract class Employee {
public static void searchEmp(int id) {
JOptionPane.showMessageDialog(null, "done");
}
}
Now I have another class test:
public class `test` {
public static void main(String args[]) {
searchEmp(2);// here my programme give error
}
}
I want to call the searchEmp() which is part of Employee from a class test but it gives an error. Please suggest any solution without inheritance.
You have to call Employee.searchEmp().
The static method searchEmp() is still a member of the class Employee and you must make a static call via its class.
Also the class Employee must be visible to the class test, otherwise you have to import it. I assume the two classes reside in the same package so this will not be a problem in your case.
Static methods and properties are bound to class. So you need to use ClassName.methodName or ClassName.propertyName.
Employee.searchEmp();
Your Test class doesnt have a static searchEmp(int) method, thus the error:
searchEmp(2);// here my programme give error
should be
Employee.searchEmp(2);
static methods are called using ClassName.staticMethod()
Given two classes, creating an object of each class in one another results in StackOverflow Exception. It is a JAVA project btw.
There are multiple classes in my projects and for using the other classes, I thought i would create objects of the other class and use it.
Say i have class Main and class GUI. I have created object of GUI in MAIN and initialized it. Similarly i have created an object of MAIN in GUI and initialized it.
Now this gives me a Stack Overflow Exception as the the constructor calls are going deep into recursion.
How do i go about it?
One possible solution i can think of is making variables and methods of one class STATIC.
Any other solution? Please suggest.
You should be passing an instance of one of you classes into the constructor of the other class.
public class Main {
private final GUI gui;
Main() {
gui = new GUI(this);
}
}
public class GUI {
private final Main main;
public GUI(Main main) {
this.main = main;
}
}
You could also use setters instead of constructors. I don't like this option as much, because you lose the ability to make your variables final.
public class Main {
private GUI gui;
Main() {
}
public void setGui(GUI gui) {
this.gui = gui;
}
}
public class GUI {
private Main main;
public GUI() {
}
public void setMain(Main main) {
this.main = main;
}
}
public static void main(String[] args) {
Main main = new Main();
GUI gui = new GUI();
main.setGui(gui);
gui.setMain(main);
}
Singleton ? (if it works for your app )