> try something like this:
>
[quoted text clipped - 14 lines]
>
> or did i misunderstand your problem?!
No you did not. I incorrectly expressed what I wanted. Sorry!
If I do it your way, then I have to implement my main method in the MyGui
class?
Actually I wanted to separate my code into the following classes: gui,
processing, main...
Isn´t it possible to do it like that? Then I would have a main class with
instances of gui
and processing, to which I pass an instance of the main class. And then I
install the
ActionListener for MyGui in MyMain...
[code]
class MyMain() implements ActionListener{
...
MyGUI window = new MyGUI(this);
MyProcessing pross = new MyProcessing(this);
...
public void actionPerformed(ActionEvent e) {
if ("button1".equals(e.getActionCommand())) {
// how can I receive the string in a textfield?
// this doesn´t work...
String temp = (Textfield)
(MyGUI-instance.getComponent(1)).getText();
MyProcessing-instance.DoSomething(temp);
}
}
class MyGUI{
public MyGUI(MyMain xxx){
MyMain instance = xxx;
}
...
Jbutton button1 = new JButton("button1");
button1.addActionListener(instance);
...
}
[/code]
Thanks!
pete
Mathias Lichtner - 20 Jul 2005 14:04 GMT
>>try something like this:
>>
[quoted text clipped - 18 lines]
> If I do it your way, then I have to implement my main method in the MyGui
> class?
No, no need to do that?!
> Actually I wanted to separate my code into the following classes: gui,
> processing, main...
[quoted text clipped - 3 lines]
> install the
> ActionListener for MyGui in MyMain...
[...]
The ActionListener for your GUI should not be inside the code of
your main as you wanted to seperated main from gui.
A common way to solve your problem is something like this: (nearly
pseudo-code)
class main {
main-method {
// create gui and start/show it
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui = new GUI();
gui.setVisible(true); }
});
}
}
imho it is not a good way to let your main gui class implement
ActionListener. Either you add anonymous classes for your listeners
or (when suitable) you create (a) new class file(s) for your listener(s)
class gui{
private JTextField myText;
private JButton doSomething;
// constructor
gui
{ // create gui elements
[...]
// register actionListeners
doSomething.addActionListener(new ActionListener() {
// you don't need that ugly if-clause (if "button1".equals....)
// this listeners
// actionPerformed-method is only run when the user
// pushes the button
public void actionPerformed(ActionEvent e) {
myProcessClass proc = new myProcessClass();
result = proc.processIt(myText.getText());
// display/doSomething with results
}
});
}
Now, none of your classes needs a reference to another and everything
should work fine. Probably this is not the best way to do something like
that, but it's one of the nicer ways ;-)
hth