Method in child class not updating JTextField - java

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?

Related

How do i switch between screens in swing

This has been asked before but I would like clarification, I'm new to java coding (sort of, started coding last month) and would like to know simply how can I switch between UIs in one JFrame, Picture this, a settings menu, How do I make it in one JFrame window instead of just make a new window with all the settings, If you don't get it, ask for clarification.
You can implement a frame (JFrame) and, for example, two panels (JPanel). Initially you embed panel A inside frame, when you want to show panel B then call the method showPanelB()
public class MyFrame extends JFrame {
PanelA panelA = new PanelA();
PanelB panelB = new PanelB();
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
public MyFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
showPanelA();
}
public void showPanelA() {
getContentPane().add(panelA, BorderLayout.CENTER);
}
public void showPanelB() {
getContentPane().add(panelB, BorderLayout.CENTER);
}
}
class PanelA extends JPanel {
// Panel implementation
}
class PanelB extends JPanel {
// Panel implementation
}

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'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();
}

How to use CardLayout, with Menu as action listener?

I want to change the JPanel of a JFrame, using the CardLayout class.
I have already run this example and it works.
Now I want to use as action listener, the JMenuItem; so If I press that JMenuItem, I want to change it, with a specific panel. So this is the JFrame:
public class FantaFrame extends JFrame implements Observer {
private static final long serialVersionUID = 1L;
private JPanel cardPanel = new JPanel();
private CardLayout cardLayout = new CardLayout();
public FantaFrame(HashMap<String, JPanel> fantaPanels) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("FantaCalcio App");
setSize(500, 500);
cardPanel.setLayout(cardLayout);
setPanels(fantaPanels);
}
public void update(Observable o, Object arg) {
cardLayout.show(cardPanel, arg.toString());
}
private void setPanels(HashMap<String, JPanel> fantaPanels) {
for (String name : fantaPanels.keySet()) {
cardPanel.add(fantaPanels.get(name), name);
}
}
}
Those are the Menu, the Controller and the Main:
private void pressed(){
home.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
controller.changePanel(home.getText());
}
});
}
public class Controller extends Observable {
public void changePanel(String panel){
setChanged();
notifyObservers(panel);
}
}
public static void main(String[] args) {
fantaPanels.put("Login", new LoginPanel());
Controller controller = new Controller();
MenuBarApp menuApp = new MenuBarApp(controller);
FantaFrame frame = new FantaFrame(fantaPanels);
frame.setJMenuBar(menuApp);
controller.addObserver(frame);
frame.setVisible(true);
}
The problem is that the JPanel doesn't change. What do you think is the problem ?
I've already debugged it, and in the update() method, the correct String value arrives.
You never add the cardPanel JPanel, the one using the CardLayout and displaying the "cards" to anything. You need to add it to your JFrame's contentPane for it to display anything. i.e.,
public FantaFrame(HashMap<String, JPanel> fantaPanels) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("FantaCalcio App");
setSize(500, 500);
cardPanel.setLayout(cardLayout);
add(cardPanel, BorderLayout.CENTER); // ****** add this line ******
setPanels(fantaPanels);
}

use actionlistener to call void function in same class

I have problem to use action listener to call function void in same class.
example..
code:
public class Product extends JPanel {
JButton add;
JPanel pAdd;
JLabel test;
JFrame frame;
public Product() {
add = new JButton("Add Product");
add.addActionListener(new ButtonListener());
add(add);
}
public void panelAdd(){
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
panelAdd();
}
}
}
How to make call the panelAdd void method?
When you add components to visible JFrame/JPanel/other components, you neet to call revalidate() andrepaint() methods after adding. Change your panelAdd() like next:
public void panelAdd(){
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
revalidate();
repaint();
}
If you put
System.out.println("hi");
to
public void panelAdd(){
System.out.println("hi");
pAdd = new JPanel();
pAdd.add(new JLabel("try"));
add(pAdd);
}
you will see hi printed to your console , your code are working, but you have problem in Layout .

Categories