I have 2 classes in the class Grafics. I would like to put all the grafic stuff, like create frame, buttons, etc., in the inhalt class. I would like to create all the functions with the grafics.
my question:
is it possible to have the Main in the inhalt class and the constructor in the grafics class?
package uebung2;
import javax.swing.JFrame;
public class Grafics extends JFrame {
private static final long serialVersionUID = 1L;
public Grafics(){
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Class with the main method:
package uebung2;
public class Inhalt {
public void main(String[]args){
Grafics fenster = new Grafics();
}
}
Of course it is - main is just the program entry point and it is generally preferable not to mix application glueing (like starting the program, setting up the environment) and the "business logic" of the program.
Related
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);
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'm trying to use and learn the composition's relation between classes respecting the flexibility of OOP programming, a small sample code that explains what I'm trying to do:
Main class:
public class MainFrame extends JFrame {
private Test test;
public static void main(String[] args) {
new MainFrame();
}
public MainFrame() {
initFrame();
}
private void initFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(100,100);
setResizable(false);
setUndecorated(true);
setVisible(true);
setLocationRelativeTo(null);
test.update();
}
public void setNumber(int i) {
System.out.println(i);
}
}
and this class:
public class Test {
MainFrame mainFrame;
public void update(){
mainFrame.setNumber(10);
}
}
if i run this i will get :
Exception in thread "main" java.lang.NullPointerException
how can I change the code so that:
The Test class refers only to the MainFrame instance created in the main, not to create a new instance of the MainFrame inside the Test class
It is possible to avoid the use of static classes and static functions to do this?
The Test class refers only to the MainFrame instance created in the
main, not to create a new instance of the MainFrame inside the Test
class
Sorry, this makes no sense. That's not how Java works, and the runtime is educating you.
This code is wrong from top to bottom.
The instance in the Test class is null because you never initialized it.
The Test class knows nothing about the main method in the MainFrame class, nor should it.
The instance in that main method goes out of scope as soon as the method exits and is garbage collected.
Why would the MainFrame class have a reference to a Test instance inside it? Bad design. It's called a circular dependency. Once you add it, you can never separate the two classes. Sometimes they can't be helped, but I see no reason for it at all here, especially for a class named Test. Test can know about MainFrame, but MainFrame need never know about Test. You've done it wrong.
You don't need to extend JFrame. You aren't adding anything by extending. I'll bet this should be a JPanel that you'll add to a JFrame.
In stead of advising about design, I'll just put a way to achieve it.
public class MainFrame extends JFrame {
private Test test;
public void setTest(Test test) {
this.test = test;
}
}
and
public class Test {
MainFrame mainFrame;
public Test(MainFrame mainFrame) {
this.mainFrame = mainFrame;
this.mainFrame.setTest(this);
}
}
Good luck.
Quick fix for your NullPointerExceptions:
public class Test {
MainFrame mainFrame;
public void update(){
mainFrame.setNumber(10);
}
public void setMainFrame(MainFrame mainFrame) {
this.mainFrame = maninFrame;
}
In initFrame():
test = new Test();
test.setMainFrame(this);
test.update();
Otherwise I agree, the concept you're trying has a lot of misunderstanding. Try to separate the Test and MainFrame logic.
I am newbie in java
package assigment;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class view extends JFrame {
public static void main(String[] args) {
new view();
}
public view(){
JFrame f = new JFrame("WELCOME");
f.setSize(400, 300);
f.setVisible(true);
f.setLocationRelativeTo(null);
controller cl = new controller();
JButton btnCompany = new JButton ("COMPANY");
f.add(btnCompany);
f.setLayout(null);
btnCompany.setBounds(50, 50, 100, 50);
btnCompany.addActionListener (cl);
}
}
contoller class
package assigment;
import java.awt.event.*;
public class controller {
public static void actioncompany(ActionEvent a,view view) {
if (a.getSource() == view.btnCompany) {
System.out.print ("test");
}
}
}
Problem:
Cannot use controller class
Cannot access btnCompany in controller class
That code shouldn't even compile since there isn't a field, view.btnCompany. The btnCompany variable is local to the constructor and thus invisible everywhere else. Also, as MadProgrammer notes, your controller class (re-name it Controller) doesn't implement ActionListener and so cannot be used as an ActionListener.
I have other issues with your code:
Don't use null layout and absolute positioning.
Do abide by Java naming rules including starting class and interface names with an upper case letter so that others can more easily understand your code.
Yes, separate out your control from your view.
Most all fields should be private, and so view.BtnCompany shouldn't be visible, even if the field existed.
ActionListeners must implement the ActionListener interface or extend a class that implements the interface such as AbstractAction.
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 )