0

I have a project in school to build an application. Since I am quite new to the Java world I am struggling.

I decided to work in NetBeans and try to somehow dynamically create the app. I created a JFrame class dynamically in Source Packages and added couple of buttons there (dynamically).

Then I created another JPanel class which I want to link to the JFrame class using a Jbutton in JFrame class. However I do not know how the JFrame is called in the JFrame class which means I can't add or remove anything from it, only dynamically.

I tried creating a new instance called JFrame but it would just write that it can not find the symbol.

I also tried to just call the JFrame (Frame.add(nr)) but it only wrote that

non-static method add cannot be referenced from a static context 

public class Frame extends javax.swing.JFrame {

    public Frame()  {
        initComponents();
    }

    private void createRecipeActionPerformed(java.awt.event.ActionEvent evt) {  

        intro.show(false);
        NewRecipe nr = new NewRecipe(); 
        Frame.add(nr); 
        nr.show(true);
    } 

My expected result is: When the JButton is clicked in the JFrame the JPanel would appear.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1. Don't extend from `JFrame`, see [this](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program). Instead build your app based on `JPanel`s. 2. For showing different `JPanel`s use [`CardLayout`], [for example](https://stackoverflow.com/questions/49517392/how-to-view-another-jpanel-containing-many-sub-jpanels-from-same-jframe-in-netbe/49519023#49519023) 3. For better help sooner provide a proper [mcve] – Frakcool Dec 20 '18 at 13:59
  • Btw `Frame` can confuse you with `java.awt.Frame`, instead of calling `Frame.add(...)` call it as `this.add(...)` to get rid of `non-static...` error but again see point #1 – Frakcool Dec 20 '18 at 14:04
  • Thanks but when I created the Frame and not extended their apperared errors in the generated code. For example: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);getContentPane(); getAccessibleContext(); pack(); - the message reads everywhere that cannot find symbol – SusuTheSeeker Dec 20 '18 at 14:36
  • Because those methods are part of `JFrame`. Again, for better help sooner post a [mcve] – Frakcool Dec 20 '18 at 15:30

1 Answers1

0

It seems you are new to java and swing. So, I'm giving below code as a sample for you to start with. I think it does what you want. So, play with it a bit and try to understand what is going on.

You may have to play with few more sample UIs to understand the "pattern" of java and swing.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame extends JFrame {

  private JButton button;

  public Frame() {
    initComponents();
  }

  private void initComponents() {
    button = new JButton("Add New Recipe Panel");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Frame.this.getContentPane().add(new NewRecipe(), BorderLayout.CENTER);
        Frame.this.getContentPane().revalidate();
      }
    });
    this.getContentPane().add(button, BorderLayout.NORTH);
  }

  public static void main(String[] args) {
    Frame frame = new Frame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(200, 100, 400, 300);
    frame.setVisible(true);
  }

  class NewRecipe extends JPanel {

    NewRecipe() {
      this.add(new JLabel("New Recipe"));
    }
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • Thanks a lot! Indeed I am new to Java and my teacher already expects me to write an application... it will be a hard struggle... anyways thanks a lot! – SusuTheSeeker Dec 20 '18 at 16:14