Controlling JComponents from other classes - java

Is it possible to control instances like variables, jcomponents, timer in other external classes?
For example this is my Class1
public class Class1 extends JFrame {
JLabel lbl = new JLabel("Hello");
public Class1() {
super("Class1");
Container c = getContentPane();
setLayout(null);
c.add(lbl);
lbl.setBounds(0,0,100,20);
Class2.process();
setSize(200,100);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String var[]) {
new Class1();
}
}
You can see there's Class2.process();
Here's the other class externally but in the same folder
public class Class2 {
public static void process() {
// I want to control lbl from Class1 class inside this method like
// lbl.setVisible(false);
}
public static void main(String args[]) {
//
}
}
Is it possible? Sorry. I can't find answers on other website.

You have to pass the instance of JLabel e.g.;
JLabel lbl = new JLabel("Hello"););
Class2.process(lbl);
public class Class2 {
public static void process(JLabel lbl) {
// I want to control lbl from Class1 class inside this method like
lbl.setVisible(false); // this will change your Class1 Jlabel
}

Related

Add element to JFrame from other class

I have two classes in one JFrame creation:
public class Window extends JFrame {
public void createWindow() throws IOException {
setTitle(GeneralValues.PROGRAM_NAME);
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
And in the second I have a JLabel:
public class InitialConfiguration {
Window w = new Window();
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
My main method:
public class Main {
Window w = new Window();
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main();
}
}
I wanted to add a label from the InitialConfiguration class to the frame from the Window class but unfortunately this code creates a frame but does not add the label from the InitialConfiguration class. Can it be done at all?
Your issue is, that you went wrong with the instances of your Window. Your InitialConfiguration has a member variable of Window, to which the label is being added. But this is not the same instance of the Window that you have in your Main class.
So all in all, you create and show the window, but never show the second window you actually added your label to.
So altogether, if you really want to keep your structure this way (I would advise against that but you may have your reasons), passing the correct Window instance as a method parameter should fix it.
public class InitialConfiguration {
// Removed second Window here
public void main(Window w) { // rename this method please
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100); // use layout managers
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
}
}
And your "Main":
public class Main {
Window w = new Window(); // rename the class Window please
InitialConfiguration ic = new InitialConfiguration();
public static void main(String[] args) {
w.createWindow();
ic.main(w);
}
}
You didn't start the Jframe at window class and I don't know your main class so I made it for your understanding. Check this codes;
(Edited)
Main: Example code:
public class MainMenu{
public static void main(String[] args) {
Windows w = new Windows();
InitialConfiguration ini = new InitialConfiguration(w);
w.createWindow();
ini.main();
}
}
ICONFIG class example code:
public class InitialConfiguration {
Window w;
public InitialConfiguration(Windows w){
this.w = w;
}
public void main() {
JLabel blauncherIcon = new JLabel();
blauncherIcon.setBounds(100, 100, 100, 100);
blauncherIcon.setOpaque(true);
blauncherIcon.setText("Text");
w.add(blauncherIcon);
w.repaint();
}
}
Window class example code:
public class Window extends JFrame {
public void createWindow(){
setTitle("Your title");
setSize(1100, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}

Can I acces variables and/or methods of the class a JFrame was created in from the panel?

shortened code:
public class Test{
private JFrame frame;
private TestPanel panel;
Object obj;
public static void main(String args[]) {
Test test = new Test();
}
public Test() {
try {
// Setup GUI
frame = new JFrame("Test");
panel = new TestPanel();
frame.add(panel);
}
}
}
Is it possible to directly access obj from panel?
If yes: How?
Thank you in advance for your help. :)
No; Not without passing the reference to object to the panel.

Can't resolve an object initiated in a public method?

I'm just starting out with java and I was working on a new GUI. I made a method that makes my JFrame, sets its default location, close operation, background colour etc.
I made a separate method for this to keep it out of the main code for the sake of tidiness. I already know how to solve this method if I just put all of those things in the main method.
This is the code:
public class Main {
public static void makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
I get an error in my main method saying that "mainWindow" can't be resolved. Why is this? is it because I made the mainWindow object in the other method? Is there a way to resolve this issue without putting everything from the makeWindow() method into the main method?
That won't work because mainWindow only exists in the context of your makeWindow method. You can make it a static field instead:
public class Main {
private static JFrame mainWindow; // declare it here so it can be seen from your main method.
public static void makeWindow(){
mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
}
public static void main(String args[]){
makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
EDIT
As #Xing commented (credits shared), you can return the mainWindow from your makeWindow method:
public class Main {
public static JFrame makeWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setVisible(true);
mainWindow.setPreferredSize(new Dimension(400,400));
mainWindow.pack();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
return mainWindow;
}
public static void main(String args[]){
JFrame mainWindow = makeWindow();
JPanel mainPanel = new JPanel();
mainWindow.add(mainPanel);
}
}
create mainWindow instance/ reference globally
private static JFrame mainWindow;
public static void makeWindow(){
mainWindow = new JFrame();
}

Method in child class not updating JTextField

public class Calculator extends JComponent {
Result rez = new Result();
public void init() {
Frame frame = new Frame();
Panel panel = new Panel();
panel.add(rez);
frame.add(panel);
Test test = new Test(); <- I thing this is the problem, instancing a child inside a parent. Not sure.
test.test(); <- doing nothing
}
}
class Frame extends JFrame {
public Frame() {
// .. setVisible, setTitle....
}
}
class Panel extends JPanel {
public Panel() {
// setBounds, setLayout
}
}
class Test extends Calculator {
public void test() {
rez.setText("Test"); <- This does nothing
}
}
Trying to make a calculator and I'm having trouble updating the results box. The method test() in the Test class is not updating the JTextBox.
However, if i create a method in the main Calculator() class, called update() and put the rez.setText() inside, it works every time.
This works tho:
public class Calculator extends JComponent {
Result rez = new Result();
public void init() {
Frame frame = new Frame();
Panel panel = new Panel();
panel.add(rez);
frame.add(panel);
this.update(); <- works
}
public void update() {
rez.setText("Test"); <- works
}
}
Why is that? Extended classes should share variables. Should they?

How do I replace these static JFrame variables?

I'm a freshman with all the GUI stuff and I want to know why do I have to declare all the JFrames with "static" in my code(otherwise it wont compile) and how can I modify my code to compile without declaring them "static"?
public class Ventana extends JFrame {
private static JFrame frameInicio = new JFrame("Ingresar Datos");
private static JFrame frameCrearUsuario = new JFrame("Crear Usuario");
private static JFrame frameMenu = new JFrame("Menu Del Juego");
private static JFrame frameJuego = new JFrame("El Ahorcado");
public static void main(String[] args) {
Inicio();
}
public static void Inicio(){
frameCrearUsuario.dispose();
frameMenu.dispose();
frameInicio.setSize(450,200);
frameInicio.setLocationRelativeTo(null);
frameInicio.add(new FrameInicio());
frameInicio.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameInicio.setVisible(true);
}
public void CrearUsuario(){
frameInicio.dispose();
frameCrearUsuario.setSize(450,300);
frameCrearUsuario.setLocationRelativeTo(null);
frameCrearUsuario.add(new FrameCrearUsuario());
frameCrearUsuario.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameCrearUsuario.setVisible(true);
}
public void Menu(){
frameInicio.dispose();
frameJuego.dispose();
frameMenu.setSize(660,290);
frameMenu.setLocationRelativeTo(null);
frameMenu.add(new FrameMenu());
frameMenu.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameMenu.setVisible(true);
}
public void Juego(){
frameMenu.dispose();
frameJuego.setSize(1100,800);
frameJuego.setLocationRelativeTo(null);
frameJuego.add(new FrameJuego());
frameJuego.setDefaultCloseOperation(EXIT_ON_CLOSE);
frameJuego.validate();
frameJuego.setVisible(true);
}
It's becuase your public static void Inicio() is static. You can't call a non static property from a static method
Here's a common way to use a JFrame. Create a constructor for the JFrame and add all the components inside of it. Then just Create the JFrame object in the main method
public class Ventana extends JFrame {
JPanel panel = new JPanel();
JButton button = new JButton("button");
JTextArea jta = new JTextArea();
public Vantan(){
add(panel);
add(button);
add(jta);
}
public static void main(String[] args){
JFrame frame = new Ventana();
frame.setTitle("Frame);
frame.setSize(500, 500);
frame.pack();
frame.setDefaulCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This way you don't need the static properties as in your case.

Categories