graphical interface comunication with main application

Hi... Im new with Java programming, I'm used to C++ and without my beloved pointers this is driving me crazy. I'm developing a client-sever application with a graphical interface in the client side.

The point is that the client has to login on the server through the main JFrame, and depending on the response, the client will show a new Frame, depending on the type of user.

I have several questions:

- I suppose I cannot extend JFrame in the client class, because I will have several frames. How can I comunicate my client class with the other frame classes, that are in other package?

- I'd like to implement the graphical interface as a library, handle here the input events, and call the methods of the client class. Should I implement listeners in the client or something?

- Maybe I can create the frames in the client class, and pass the client instance to the constructors of the frames, so the frames can easily interact with the client... but from a engineering point of view this design is a mess.

Thanks in advance

[1073 byte] By [Byfrona] at [2007-11-27 4:43:23]
# 1

> Hi... Im new with Java programming, I'm used to C++

> and without my beloved pointers this is driving me

> crazy.

Java has references. If your C++ programs were based primarily on pointer arithmetic, I'd shudder at the thought of the code.

> I have several questions:

>

> - I suppose I cannot extend JFrame in the client

> class, because I will have several frames.

So? Extend JFrame and reuse the subclass. Or subclass JFrame several times. Though actually, IMHO, it's cleaner to use JFrame as-is and subclass panels or other components to put in the JFrame.

> How can I

> comunicate my client class with the other frame

> classes, that are in other package?

Invoke methods on them. The methods would have to be protected or public because they're in other packages.

> - I'd like to implement the graphical interface as a

> library, handle here the input events, and call the

> methods of the client class. Should I implement

> listeners in the client or something?

In this model, which is the client? The GUI? The main app?

Generally you write a GUI by creating the GUI widgets and adding listeners to them to handle the events they get.

> - Maybe I can create the frames in the client class,

> and pass the client instance to the constructors of

> the frames, so the frames can easily interact with

> the client... but from a engineering point of view

> this design is a mess.

Errr....I don't see why you need to pass anything to frames.

You can create a class that takes an instance of the main app, creates some frames, and then creates listeners for those frames or the components within those frames. The listeners could refer to the main app instance.

paulcwa at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 2

> Im new with Java programming,

> I'm developing a client-sever application with

> a graphical interface in the client side.

Those two sentences don't mesh well. That's pretty advanced java stuff you're trying to tackle, especially if you're still having c++ withdrawal symptoms. Have you gone through some of the java basics tutorials? If not, here are some good ones to start with:

http://java.sun.com/docs/books/tutorial/

http://java.about.com/od/beginningjava/a/beginjavatutor.htm

Do yourself a favor and start with the basic stuff before touching swing, you'll most likely confuse yourself otherwise. I'm not trying to be discouraging, I just remember some of the dumb **** I tried to do with swing before I knew the language well.

hunter9000a at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 3

> - I suppose I cannot extend JFrame in the client

> class, because I will have several frames. How can I

> comunicate my client class with the other frame

> classes, that are in other package?

Sure you can. As for communicating between windows, all you need is a reference to whatever object has the methods you need to call.

> - I'd like to implement the graphical interface as a

> library, handle here the input events, and call the

> methods of the client class. Should I implement

> listeners in the client or something?

Presumably you would have some UI that will handle action events and the like on the buttons and fields and will call some methods in some object you define to do the business logic (the server communication, validation, etc.). I would maybe start with defining a class that has the methods you need for the functional stuff. Then wrap a UI around it such that the UI triggers those methods (passing in data, etc).

> - Maybe I can create the frames in the client class,

> and pass the client instance to the constructors of

> the frames, so the frames can easily interact with

> the client... but from a engineering point of view

> this design is a mess.

Not sure it's a mess. I'd probably do just something similar. The idea being that the business logic is general to your app, the UI is just a layer on top which could be switched with something else (JSP/servlets to run on a web server, for example.

bsampieria at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Make a controller (parent) class that spawns the appropriate frame as needed by the response from the previous state. The controller will know all of it's children and as such can have getters and setters for what ever may be needed.
morgalra at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 5

import java.awt.*;

import java.awt.event.*;

public class TestGui {

public static void main(String[] argv) {

final Blah blah = new Blah(argv);

Frame f = new Frame("Blah");

Button but = new Button ("click me");

final Label lab = new Label();

but.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

blah.doit();

lab.setText(blah.getIt());

lab.repaint();

}

});

f.add(but, BorderLayout.NORTH);

f.add(lab, BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

}

}

class Blah {

private int i = 0;

private String[] words;

Blah(String[] argv) {

words = argv;

}

void doit() {

i = ++i % words.length;

}

String getIt() {

return words[i];

}

}

Stupid, yes.

paulcwa at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 6

Well yeah... that's pretty stupid. The problem is that my interfaces are quite complex, and Im using NetBeans to design them, so the listener implementation is in other class, and I cannot see my controller(blah) class from there. Im trying to do something like:

public class MainApp

{

public static void main(String[] argv) {

Client cli = new Client();

MainJFrame mainFrame = new MainJFrame(cli);

}

}

Now the problem is that Im supposed to destroy the mainFrame and create anotherFrame from the client class... so Ive to sort out something else.

Byfrona at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 7
So pass the client to the listener's constructor.
paulcwa at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 8
OK I'm stupid. I can just create the Frames in the Client constructor and manage everything from there.
Byfrona at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...
# 9

Here is your code using swing not awt and i also put it in one class:

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Gui extends JFrame{

JButton but = new JButton("click me");

JLabel lab = new JLabel();

Blah blah;

static String[] arguments;

public Gui(String title){

super(title);

blah=new Blah(arguments);

but.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

blah.doit();

lab.setText(blah.getIt());

lab.repaint();

}

});

add(but, BorderLayout.NORTH);

add(lab, BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

Gui.arguments=args;

new Gui("Blah").setVisible(true);

}

class Blah {

private int i = 0;

private String[] words;

Blah(String[] argv) {

words = argv;

}

void doit() {

i = ++i % words.length; // seems i will be 1 first time and throws java.lang.ArithmeticException: / by zero exception when no arguments are given

}

String getIt() {

return words[i]; // will give error when i use none or only one argument since arrays start from 0

}

}

}

Hope it helps!

Message was edited by:

pSal

pSala at 2007-7-12 9:55:09 > top of Java-index,Java Essentials,Java Programming...