Problem with actionlistener/drop-down-menu (for a queue)
Hello there,
I really don't get it. I'm sitting over this programs since months and I just can't get through.
Here's the problem:
I have created a wonderful working program (queue) implying 3 classes (i guess you guys know this kind of "lists"):
-content
-element
-list
List refers to element, refers to content, you know that.
In "list" I have the following methods:
- give out the starting time
- add a new person
- take out the person on the beginning of the queue
- give out all information about the list
- get minimal waiting time
- get average waiting time
- get maximal waiting time
- get length of the queue
Everything is working totally fine and i get the right results over the console.
But now I JUST need to realize a GUI with a drop-down menu where I can choose the methods listed above.
It just doesn't work!
Of course, I can realize the drop-down menu,
but it doesn't work to apply the methods I got so wonderfully working over the console working at the GUI at all!
How do I attach the methods from class "list" with the buttons of my new class "GUI"?
How can I realize the TextField I need for the method "add a new person"?
Can I even combine the methods from the other class with this GUI at all?
I tried using the ActionListener but it always gives me the answer that the methods wouldn't exist, even if I just wrote them in the class GUI.
Was it a bad choice to program things to work over the standard console first?
Do I have to program everything anew for the GUI?
Please please please help me!
Respectfully,
Lara
Re: Problem with actionlistener/drop-down-menu (for a queue)
Quote:
Do I have to program everything anew for the GUI?
If the methods are generic enough, you won't have to rewrite them, but rather create more JFrames or JDialog to house the information they return in a format you find desirable.
So what I'm getting from this is that you're having trouble with an ActionListener? Could you post the relevant code or create an SSCCE that describes the problem? Also, are your Content, Element, and List classes static, or are they meant to be instantiated?
When you talk about "combining" methods, I'm not sure what you mean. Methods can call other methods (granted that static/non-static limitations are respected).
Re: Problem with actionlistener/drop-down-menu (for a queue)
Thanks for your answer. No it's not just the actionlistener, it's the whole GUI (but yeah, mainly the actionlistener).
I tried writing an example here but it's just to complex because of the 3 classes so I don't know how to make my point shortly :(
I will think about it, but previously one question:
If I have an actionlistener in class A, does he have to call methods of the same class (A) (for example methods who call methods from the other class B), or can an actionlistener also directly be linked to methods of class B? And if yes, how do I do it?
Oh man, to be quite honest I don't even get the textfield visible :(
Re: Problem with actionlistener/drop-down-menu (for a queue)
Quote:
Originally Posted by
blueroselara
Thanks for your answer. No it's not just the actionlistener, it's the whole GUI (but yeah, mainly the actionlistener).
I tried writing an example here but it's just to complex because of the 3 classes so I don't know how to make my point shortly :(
I will think about it, but previously one question:
If I have an actionlistener in class A, does he have to call methods of the same class (A) (for example methods who call methods from the other class B), or can an actionlistener also directly be linked to methods of class B? And if yes, how do I do it?
Oh man, to be quite honest I don't even get the textfield visible :(
You want to access Class B methods in Class A? If it's so, you must look into if Class B is visible to Class A, if it's visible, create an instance and use it's methods wherever you want(but only if this is needed).
Re: Problem with actionlistener/drop-down-menu (for a queue)
Okay thanks, it's now saying
Quote:
"non-static method fromclassA() cannot be referenced from a static content"
But it's really not static!
Oh please would anybody check my codes, I'm really going crazy with this :(
Re: Problem with actionlistener/drop-down-menu (for a queue)
Quote:
Originally Posted by
blueroselara
Okay thanks, it's now saying
But it's really not static!
Oh please would anybody check my codes, I'm really going crazy with this :(
Where is your code?
Re: Problem with actionlistener/drop-down-menu (for a queue)
So this is my actionlistener in class GUI
Code :
class PersonEinfuegenActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Liste.PersonEinfuegen();
}}
And this is my class "Liste" or rather the part with the method I want to use above.
Code :
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
public class Liste
{
// instance variables
private static Element kopf;
private static Element ende;
private static Element aktuell;
private static Uhrzeit beginn;
private static Date start;
private Uhrzeit current;
private long differenz;
private static long startmillis;
/**
* Constructor for objects of class Liste
* Initialisierung der Liste
*/
public Liste()
{
kopf = null;
ende = null;
start = new Date();
startmillis = start.getTime();
System.out.println("Sie haben eine neue Liste erstellt." +start);
System.out.println ("");
}
/**
* Method put
*/
public void PersonEinfuegen()
{ Element akt;
akt = new Element(); //Element erzeugen
if (kopf == null)
{ kopf = akt;
akt.next = null;
ende = akt; }
else
{
ende.next = akt;
ende = akt; }
}
I can send you the whole code if you want to but it's 4 classes long :/
//edit: I just checked out that it doesn't make sense, even if it would work. Calling the method from the other class doesn't make it work in GUI. I would have to build a textfield or anything...I don't know. Omg
Re: Problem with actionlistener/drop-down-menu (for a queue)
Quote:
Liste.PersonEinfuegen();
You must look how to call the methods of other class. Well, PersonEinfuegen() is not a static method so you must call it from some object which is actually referencing Liste.
Re: Problem with actionlistener/drop-down-menu (for a queue)
So, that answers my previous question: your List class is meant to be instantiated. This means it is like the String class. Think about this: when you want to get the length of a String, can you use...
String.length()
...? No, because length() is NOT a static method, and you must therefore create an instance of String. This example is correct:
String example = "yay!";
int len = example.length();
Now that I've created an instance of String, I can use the String class's non-static methods in respect to the INSTANCE, not the CLASS.
Re: Problem with actionlistener/drop-down-menu (for a queue)
Ah okay I get the point thanks for that it was stupid and not really what I meant.
It's more like, how can I simply make a button which calls the method from the other class up? Can you give me an example?
My GUI looks like this
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ausgabefenster
{
private JFrame fenster;
private Container inhalt;
private JLabel label1;
private Liste list;
private JMenuBar menuzeile;
private JMenu menu1;
private JMenuItem minute22;
private JTextField eingabeFeld;
private JLabel anzeige;
/**
* Constructor for objects of class Ausgabefenster
*/
public Ausgabefenster()
{
fenster = new JFrame("Uhrenanzeige");
fenster.setSize(800,800);
fenster.setLocation(300, 200);
inhalt = fenster.getContentPane();
menuzeile = new JMenuBar();
fenster.setJMenuBar(menuzeile);
menu1 = new JMenu ("Datei");
menuzeile.add(menu1);
minute22 = new JMenuItem("Person einfuegen");
menu1.add(minute22);
// minute22.addActionListener(new PersonEinfuegenActionListener());
list = new Liste();
JLabel bitteLabel = new JLabel("Bitte Text eingeben:");
JLabel eingLabel = new JLabel("Eingegeben wurde:");
anzeige = new JLabel("");
eingabeFeld = new JTextField("");
label1 = new JLabel ("Bitte Person einfuegen"); // Label mit Text erzeugen
eingabeFeld = new JTextField("");
label1.setVerticalAlignment(SwingConstants.CENTER);
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font("Arial", Font.BOLD, 50));
inhalt.add(label1);
inhalt.add(eingabeFeld); // Label in Inhaltsbereich einfügen
inhalt.setBackground(Color.YELLOW);
fenster.setVisible(true); // Fenster darstellen *//
}}
From point //minute22.addActionListener... everything goes bullshit and I just don't get an idea how to simply solve it, that I can click the button "Person einfuegen" and then there appears a Textfield that does exactly the thing that the console does.
//okay thanks to snowguy13 I know what you mean and that the stuff shown above is total crap, I just have to show you anything from my code so you can get an idea. I have to work on it later but maybe you can give me an easier example or way how i could realize that
Re: Problem with actionlistener/drop-down-menu (for a queue)
Well, we don't know what is your application's design, what do you want to do....
But if you want to call a function from other class, we've just told you. We don't know the very logic of your program unless you tell us. And that is why SSCCE encouraged..
Re: Problem with actionlistener/drop-down-menu (for a queue)
Because if it's too complicated to turn the console stuff into the GUI I consider breaking it down and start the whole stuff anew with a total new GUI refering program but it would be very sad because of the months of hard work I put into the queue to get it working over the console.
It's really not that I'm dumb, I just can't work with the GUI, I have never learned it and I don't understand the technique behind it. I guess Java is not really meant for GUI-programming so it's like learning swimming in a big ocean
Re: Problem with actionlistener/drop-down-menu (for a queue)
Quote:
I guess Java is not really meant for GUI-programming
Totally wrong. It's actually that you didn't properly learn it or use it.
Re: Problem with actionlistener/drop-down-menu (for a queue)
Okay then it's my fault for not understanding it, I'm sorry.
But to get to the point, I have this program which works very properly over the console: I have a queue where I can input informations about new people, it can show me the people who are already in the queue, and so on.
All I want to do is to have a GUI instead of the console!
So if I for example choose the button "insert person" there will occur a textfield where I can put the information over the person like I can already do it over the console, and if I choose the button "show me the list" there appears just the content of the list, like it's already working when I do it over the console.
I tried to give you some SSCCE but it seemed to be insufficient. Should I give you the whole program? I just don't get along with the GUI and everything I'm trying to say about it turns out to be so stupid because I can't really describe what I mean.
Maybe I should better rework it and come back then. But if I would know it's more easier to write the whole program anew I would do this and not hold me up with trying to turn the console output/input in the GUI like I do for three months now :(
Re: Problem with actionlistener/drop-down-menu (for a queue)
Hm... Well, it's kinda messy to try and rearrange the Components of a GUI after it has been rendered (not saying that it's wrong, just that it has a few caveats). You could create a few more GUI's that pop up when you press certain buttons, or maybe make use of the JOptionPane class. If you want to make all of the GUI's yourself, I suggest looking at this tutorial.
As Mr.777 said, we still don't know enough to accurately help you... I'm sorry.
Re: Problem with actionlistener/drop-down-menu (for a queue)
That was a nice idea, I didn't thought of simply letting some new windows pop up! I still don't know how to realize it at all but I will start trying it. Thanks for this hint, but I guess I have to come back at you guys in a few days (:
Re: Problem with actionlistener/drop-down-menu (for a queue)
Okay, no problem! Happy to help! Good luck with your program! :)
Re: Problem with actionlistener/drop-down-menu (for a queue)
Well, morning guys.... Happy to see that you atleast got some point to see Java's strength :) Good Luck and if you got any problem, let us know.