Question

Currently I am having a major issue when trying to update my JFrame with new information. I tried the usual invalidate, validate, repaint, ect.

Here is the main code that creates the JFrame:

package Calendar;

import javax.swing.*;
import java.awt.*;
import java.util.GregorianCalendar;
import java.awt.Frame;

public class Main extends JFrame
{


    public Main()
    {

    }

    public void makeFrame()
    {
        JFrame frame = new JFrame("Programming II Project: Calendar");

        setLayout(new BorderLayout(0, 5));

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(640, 384));
    }

    public void makeCenter(int year, int month)
    {
        // Add center
        //new JFrame();
        //this.setVisible(true);
        JPanel p = new JPanel(new BorderLayout());
        p.add(calendarPanel.makeCalendar(year, month));
        add(p, BorderLayout.CENTER);
        System.out.println("----- FRAME WINDOWS -----");
        Frame[] frames = Frame.getFrames();
        for (Frame frame : frames)
            System.out.println(frame.getName() + ": " + frame.getClass());

        //this.setMinimumSize(new Dimension(512, 384));
    }



    public void makeEast()
    {
        JPanel p = new JPanel(new BorderLayout());
        Clock myClock = new Clock();
        p.add(myClock);
        p.setForeground(Color.red);
        this.add(p, BorderLayout.EAST);
    }

    public void makeNorth()
    {
        // Add north
        northPanel np = new northPanel();
        this.add(np, BorderLayout.NORTH);
    }

    public static void main(String[] args)
    {

        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("ClassNotFoundException");
        }
        catch (InstantiationException e)
        {
            System.out.println("InstantiationException");
        }
        catch (IllegalAccessException e)
        {
            System.out.println("IllegalAccessException");
        }
        catch (UnsupportedLookAndFeelException e)
        {
            System.out.println("UnsupportedLookAndFeelException");
        }

        Main m = new Main();
        m.makeFrame();
        m.makeCenter(GregorianCalendar.YEAR, GregorianCalendar.MONTH);
        m.makeNorth();
        m.makeEast();
        m.setVisible(true);
    }
}

And this is the button code that is suppose to switch the JFrame: This is in a totally differnet class

static class bNextMonth_Method implements ActionListener{
    public void actionPerformed (ActionEvent e)
    {
        if (clickm == 11)
        {
            clicky = clicky + 1;
            clickm = 0;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            m.makeCenter(clicky, clickm);
            m.makeNorth();
            m.makeEast();
        }
        else
        {
            clickm = clickm + 1;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            m.makeCenter(clicky, clickm);
            m.makeNorth();
            m.makeEast();
        }
    }
}

Now you'll notice that in the first bit of code in the makeCenter method the commented out stuff. If I un-comment that it the information changes but it creates a new window every time.

Now even though dispose() is not in there currently, I put it in the makeCenter method since that was what was called from the button. I can tell that the button is working properly because

System.out.println("----- FRAME WINDOWS -----");

updates and lists a new frame every time I click.

There has to be something terribly simple.

Also I would like to say that there is another class I left out here called Clock.java.

This does have a timer but I'm not sure if that was interrupting anything.

EDIT: I guess this is what you mean?

 public class bNextMonth_Method implements ActionListener{
    private Main main;

    public bNextMonth_Method(Main main) {
        this.main = main;
    }
    public void actionPerformed (ActionEvent e)
    {
        if (clickm == 11)
        {
            clicky = clicky + 1;
            clickm = 0;
            System.out.println(clickm);
            System.out.println(clicky);
            main.makeCenter(clicky, clickm);
            main.makeNorth();
            main.makeEast();
        }
        else
        {
            clickm = clickm + 1;
            System.out.println(clickm);
            System.out.println(clicky);
            Main m = new Main();
            main.makeCenter(clicky, clickm);
            main.makeNorth();
            main.makeEast();
        }
    }
}

That was my best interpretation but that throws

bNextYear_Method(Main) in bNextYear_Method cannot be applied to ()

now i know what that means but how do I go about fixing it?

Was it helpful?

Solution

Look what you're doing with Main inside of the ActionListener -- you're creating a completely new and different Main inside of this method!

public void actionPerformed (ActionEvent e)
{
    if (clickm == 11)
    {
        clicky = clicky + 1;
        clickm = 0;
        System.out.println(clickm);
        System.out.println(clicky);
        Main m = new Main();   // ********* here **************
        m.makeCenter(clicky, clickm);
        m.makeNorth();
        m.makeEast();
    }

This Main has nothing to do with the one that is being displayed. Any changes made to the state of this newly created Main object will not be reflected in the displayed one because they are completely different unique objects.

Solution: don't do this. Pass in a valid reference to the real Main object into the listener and call methods on it.

public class MyListener implements ActionListener {
  private Main main;

  public MyListener(Main main) {
    this.main = main;
  }

  public void actionPerformed(ActionEvent e) {
     // now in here we can call methods on the main variable
     // and it will be called on the actual displayed main
  }
}

Edit

Now when you call the listener's constructor you must pass the correct parameter into it. Also pleas study and follow Java naming conventions. All class names should start with an upper-case letter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top