0

Right so my team mate has created a JApplet which contains a countdown

I have to take his JApplet and fit it inside a JPanel/JTab (Which its inside a JFrame), something from the NetBeans Palette.

Simply speaking i have a page with many components and I just want the countdown at the top right.

Using the GUI builder in netbeans, i created a JPanel and an event. In the event handler, i put:

private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {                                       
    // TODO: Countdown

    Package.Packagename.Countdown.init();
}                                      

The error message i get is:

non-static method init() cannot be referenced from a static context.

The Applet works fine on its own, i just cant put it in here.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2999509
  • 97
  • 2
  • 4
  • 13
  • 1
    _"I have to take his JApplet and fit it inside a JPanel"_ - Auto-Fail. `JApplet` is a top level container and should not be put inside of a `JPanel`. You _can_ however take the `JApplets` component contents and put _them_ inside the `JPanel` – Paul Samsotha Mar 25 '14 at 08:40
  • Do you want another frame to open with the timer or did you want the timer embedded into the panel? – Paul Samsotha Mar 25 '14 at 08:42

2 Answers2

3

"I have to take his JApplet and fit it inside a JPanel"

JApplet is a top level container and should not be put inside of a JPanel. You can however take the JApplets component contents and put them inside the JPanel

-OR-

Instead of trying to use your friend's applet code, just create your own. Implementing a countdown is really not that difficult. Just hand code it.

  1. Drag and drop a label to the corner of your frame (jLabel)

  2. Then use something like this

    public class MyFrame extends javax.swing.JFrame {
        int time = 100;
    
        public MyFrame () {
            initComponents();
    
            jLabel.setText(String.valueOf(count));
    
            javax.swing.Timer timer = new javax.swingTimer(1000, new java.awt.event.ActionListener(){
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (count == 0) {
                        ((javax.swing.Timer)e.getSource()).stop();
                    } else {
                        count--;
                        jLabel.setText(String.valueOf(count));
                    }
                }
            });
            timer.start();
        }
    }
    

If you want, just make a Timer a class member that you can call from another actionPerformed to start() or stop() it

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    +1 for the [`Timer`](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) alternative; some hybrid examples are cited [here](http://stackoverflow.com/a/12449949/230513). – trashgod Mar 25 '14 at 10:18
1

An Applet is a web component that has nothing to do inside a desktop app.

Create a widget (something wrapped in a JPanel or whatever) that you will both use in your applet and your desktop app.

Michael Técourt
  • 3,457
  • 1
  • 28
  • 45
  • Im sorry could you expand on how you will do this? Inside NetBeans or Eclipse or something? – user2999509 Mar 25 '14 at 09:04
  • Well you need to put your "countdown component" classes into a separate "project". You will import the "countdown component" project from your two other projects (applet, desktop app) so you can re use the countdown component classes in both. – Michael Técourt Mar 25 '14 at 09:34
  • The solution I'm providing here is better explained by @peeskillet in the upper part of his answer. – Michael Técourt Mar 25 '14 at 09:35