Welcome to Dream.In.Code
Become a Java Expert!

Join 150,406 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 935 people online right now. Registration is fast and FREE... Join Now!




Closing a JFrame with a button click ...

 
Reply to this topicStart new topic

Closing a JFrame with a button click ..., Getting this strange static error, nothing declared static.

hallizh
3 Apr, 2008 - 02:12 PM
Post #1

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 37


My Contributions
Okay this is jobDoor.java ..
java

import javax.swing.*;
import java.awt.*;
public class jobDoor {

JFrame frame;
JPanel primary, mainPanel;

public void windowPopper (String args) {
String argument = args;

sidePanel side = new sidePanel();
frame = new JFrame();
primary = new JPanel();
mainPanel = new JPanel();
if (argument.equals("starfsmenn")) {
starfsmenn starfsm = new starfsmenn();
mainPanel.add(starfsm.smenn());
}
primary.add(side.sPane());
primary.add(mainPanel);
mainPanel.setPreferredSize(new Dimension(500, 500));
frame.getContentPane().add(primary);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public void closeWindow() {
frame.setVisible(false);
frame.dispose();
}
}


And here i call to another method from "sidePanel.java" ...

java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class sidePanel {
JButton starfsmenn, vaktatafla, laun, veikindi;

public Component sPane() {
JPanel sidebar = new JPanel();
JLabel empty = new JLabel(" ");
JLabel empty2 = new JLabel(" ");
JLabel empty3 = new JLabel(" ");

starfsmenn = new JButton("Starfsmenn");
vaktatafla = new JButton("Vaktatafla");
laun = new JButton("Launakerfi");
veikindi = new JButton("Veikindi/frí");

sidebar.setLayout(new BoxLayout(sidebar, BoxLayout.Y_AXIS));
buttonListener listen = new buttonListener();

starfsmenn.addActionListener(listen);
vaktatafla.addActionListener(listen);
laun.addActionListener(listen);
veikindi.addActionListener(listen);

sidebar.add(starfsmenn);
sidebar.add(empty);
sidebar.add(vaktatafla);
sidebar.add(empty2);
sidebar.add(laun);
sidebar.add(empty3);
sidebar.add(veikindi);

return sidebar;
}
public class buttonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
if(e.getSource() == starfsmenn) {
//
// HERE IS MY PROBLEM
//
jobDoor.closeWindow();
jobDoor.windowPopper("starfsmenn");
//
// AND ENDS HERE(hopefully)
//
}

}
}
}


And last but not least ... the compile error ...

QUOTE
.\sidePanel.java:39: non-static method closeWindow() cannot be referenced from a static context
jobDoor.closeWindow();
^
.\sidePanel.java:40: non-static method windowPopper(java.lang.String) cannot be referenced from a static context
jobDoor.windowPopper("starfsmenn");
^
2 errors

Tool completed with exit code 1


Thanks for your time.
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Closing A JFrame With A Button Click ...
3 Apr, 2008 - 02:23 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
static methods/data remain the same for all classes and cannot access non-static methods/data

you should either declare each method to be static, or create an object of jobdoor.
User is offlineProfile CardPM
+Quote Post

hallizh
RE: Closing A JFrame With A Button Click ...
3 Apr, 2008 - 02:35 PM
Post #3

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 37


My Contributions
I dont really get it why, but this fixed the program .. Nothing is declared static in it however, except the main method, which is in loginPanel.java.
I declared the windowPopper and closeWindow methods static, that did it.
Explanation would be super wink2.gif

Thanks for your quick response mate, really appreciated smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Closing A JFrame With A Button Click ...
3 Apr, 2008 - 02:38 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(GWatt @ 3 Apr, 2008 - 03:23 PM) *

static methods/data remain the same for all classes and cannot access non-static methods/data

you should either declare each method to be static, or create an object of jobdoor.


When you enter the main() method which is static no instance of jobDoor exists

So you have to create a new object jobDoor to access its methods
or made all methods static (can be accesses even if no object jobDoor exists)
User is offlineProfile CardPM
+Quote Post

hallizh
RE: Closing A JFrame With A Button Click ...
3 Apr, 2008 - 02:51 PM
Post #5

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 37


My Contributions
Okay i understand. Its just that the frame is created in jobDoor, somehow i thought it didnt need to be static. But i get it now, thanks alot smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Closing A JFrame With A Button Click ...
3 Apr, 2008 - 03:40 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(hallizh @ 3 Apr, 2008 - 03:51 PM) *

Okay i understand. Its just that the frame is created in jobDoor, somehow i thought it didnt need to be static. But i get it now, thanks alot smile.gif

Yes your JFrame was a "kind of static" JFrame inside your main() method and it will disappear at the end of main().
Was not part of any instance of jobDoor

This post has been edited by pbl: 3 Apr, 2008 - 03:50 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month