best /proper way to

J

James_Gale

Hi,

I'm a newbie and was wondering if you could help.

I'd like to know the best /proper way to do the following, I think I
could hack it to do something like what I want it to do, but I'd rather
know the best /proper way to do it.


I have a two forms the first contains a selectable list of items, an
add, edit, delete and close button, the second contains various
textboxs to add/ edit fields and a ok and cancel button.

if the first form's selectable list of items is populated by a data
model.
On selection of an item and the edit button is clicked.

What would I do next ? to get the


Do I add the second form to the edit button as a Listener.

How would I get the information as to what was selected to the second
form, does the second form use the same one as the first form, or does
it have its own data model.


Any help or even a link would be greatly appreciated

many thanks

James

e-mail : (e-mail address removed)
 
Z

zero

Hi,

I'm a newbie and was wondering if you could help.

I'd like to know the best /proper way to do the following, I think I
could hack it to do something like what I want it to do, but I'd rather
know the best /proper way to do it.


I have a two forms the first contains a selectable list of items, an
add, edit, delete and close button, the second contains various
textboxs to add/ edit fields and a ok and cancel button.

if the first form's selectable list of items is populated by a data
model.
On selection of an item and the edit button is clicked.

What would I do next ? to get the


Do I add the second form to the edit button as a Listener.

How would I get the information as to what was selected to the second
form, does the second form use the same one as the first form, or does
it have its own data model.


Any help or even a link would be greatly appreciated

many thanks

James

e-mail : (e-mail address removed)

Please proofread your post before sending it - sentences like "to get
the" don't make much sense to me.

Are you coming from a VS environment? There's no such thing as a "form"
in Java - you probably mean a window or frame?

If I understand your question correctly, the best thing you can do is
have an ActionListener on the edit button that will create the second
frame if it doesn't exist yet, and show it if it does exist, after
filling it with the correct data.
 
R

Roedy Green

I have a two forms the first contains a selectable list of items, an
add, edit, delete and close button, the second contains various
textboxs to add/ edit fields and a ok and cancel button.

Is this an HTML form you are generating with a Servlet or an Applet,
or an application? Do you use AWT or Swing?

I presume an ordinary application.

You want the second frame to pop up when a button is pressed?

Than you write an ActionListener in the first frame for the button
than does

FrameType2 frame2 = new FrameType2( ..... );
frame2.setVisible();
and possibly
frame2 = null; if frame2 is not local.

You can pass frame2 some information, e.g. an interface to frame1 in
the constructor.
 
J

James_Gale

Sorry for my misuse of English.

I will try to explain myself better.

I have created two classes (which I called forms)

The first class "listView" is a jframe containing
a couple of jPanels, a jscrollpanes containing a jtable. Four jbuttons
"add", "edit", "delete" & "close".

The second class "itemView" is again a jframe containing a jtable. Two
jbuttons "Ok" & "Cancel"

I have a datamodel for populating the jtable in the first class
"listView".

I also have a main class.

The listView class is passed the datamodel on creation, the information
is shown in the jtable which has row selection only set, and is
non-editable.

The user select a row and then clicks the action to be perform
"add","edit" or "delete".
On selecting an action the second class is shown, detailing the
information to be edited.
On completion of the any editing etc.. the user can select either "ok"
to keep the changes or "cancel".

How would it be best to proceed.

Would it be better to create the itemView class and passing it the
datamodel used by listView, from each buttons ActionListener.

OR

Could I have the itemView class as an ActionListener itself and
listView would notify it when an action was selected. ie. add, edit,
delete or which row in the list was selected.
What way the itemView class is not "hardcoded" into the listView class.

many thanks

James

e-mail : (e-mail address removed)
 
Z

zero

Sorry for my misuse of English.

I will try to explain myself better.

I have created two classes (which I called forms)

The first class "listView" is a jframe containing
a couple of jPanels, a jscrollpanes containing a jtable. Four jbuttons
"add", "edit", "delete" & "close".

The second class "itemView" is again a jframe containing a jtable. Two
jbuttons "Ok" & "Cancel"

I have a datamodel for populating the jtable in the first class
"listView".

I also have a main class.

The listView class is passed the datamodel on creation, the information
is shown in the jtable which has row selection only set, and is
non-editable.

The user select a row and then clicks the action to be perform
"add","edit" or "delete".
On selecting an action the second class is shown, detailing the
information to be edited.
On completion of the any editing etc.. the user can select either "ok"
to keep the changes or "cancel".

How would it be best to proceed.

Would it be better to create the itemView class and passing it the
datamodel used by listView, from each buttons ActionListener.

OR

Could I have the itemView class as an ActionListener itself and
listView would notify it when an action was selected. ie. add, edit,
delete or which row in the list was selected.
What way the itemView class is not "hardcoded" into the listView class.

many thanks

James

e-mail : (e-mail address removed)


I would use neither. Instead, give each of the buttons an ActionListener
of its own (you could use 4 anonymous classes for this, or one class that
handles them all). In those listeners, populate and show the ItemView
class. For example:

public class ListView
{
private ItemView itemView;
// ...

public ListView(...)
{
// ...
JButton addButton = new JButton("add");
addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
itemView.setData(getSelectedData());
itemView.show();
}
});
// ...
}

// ...
}

getSelectedData would give a list/array/vector/... of the selected row.
ItemView:setData would use this data to fill out the text fields.

If you don't like the close relationship between ItemView and ListView,
you could implement a Controller which passes information between the
two.
 
J

James_Gale

Many thanks zero,

You mentioned a Controller, is this using Model-View-Controller, do you
know of any examples of how to use this.

James

e-mail : (e-mail address removed)
 
R

Roedy Green

You mentioned a Controller, is this using Model-View-Controller, do you
know of any examples of how to use this.

The controller is the most custom part. It is the application logic.
It decides what changes based on various inputs. Typically it calls
methods of the model, and the model is turn fires change events that
cause parts of the View to be repainted.
 
Z

zero

Many thanks zero,

You mentioned a Controller, is this using Model-View-Controller, do
you know of any examples of how to use this.

James

well in your case it's not really the model-view-controller pattern since
it would be relaying messages between two "view" classes. It's based on
the same pattern though. It more fits the OO idea of low coupling
(uncoupling the link between ListView and ItemView, so that they can be
changed independently).
Whether or not to use a controller in your case is a pure design decision.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top