Re: question about keepin
To: comp.lang.java.gui
On Apr 2, 4:00 pm, "Adam Sandler" <cor...@excite.com> wrote:
Hello,
I have a question about keeping logic/processing code off of the GUI.
In a couple of other languages I'm familiar with, there is usually a
method called FindComponent which will iterate through a form and get
the component matching the name in the argument.
For example, in VS.NET lets say I have a grid (table) on a form, the
form represents one class. I then put the code for getting and
processing data in another class. After any processing occurs (and
the results are placed in a dataset), I need to bind the dataset to
the grid. But the grid doesn't have knowledge of where the dataset
came from... so how does the data get shoveled in there?
In the data processing class, there's a local variable of grid type
and it's gets bound to the grid in the form, like so (sorry about the
VB):
Class SomeClass
Sub doSomething(ByVal c As Component)
Dim localGridView as GridView
Dim localDataSet as DataSet
' MyGrid lives on Form1
localGridView = c.FindComponent("MyGrid")
localGridView.DataSource = localDataSet
' So even though the GUI hasn't a clue as to what is going on, the
current data is now displayed to the user.
localGridView.DataBind()
End Sub
End Class
Does Java have an equivalent of FindComponent, or a something similar
to the code snip above?
I'm using Java 6 here. I did try looking at c.getComponent() but
there's only one valid index... 0 and it represents the
javax.swing.JRootPane. Anything after 0 throws an index out of bounds
error. There are 3 JButtons and 1 JList on the panel. I want to add
some strings to the JList...
public class MyGUI() extends JFrame
{
GUIUtils u = new GUIUtils();
public MyGUI()
{
//
// snip
//
myButton.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
try
{
u.doSomething(MyGUI.this);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
and...
public class GUIUtils
{
public void doSometing(Container c) throws IOException
{
listModel = new DefaultListModel();
listModel.addElement(value);
// I want to put the listmodel in the JList now but how do I get
this code to know about the JList in MyGUI?
}
}
Suggestions are greatly appreciated.
Thanks!
Search on "java MVC pattern". MVC is short for Model View
Controller. You should also look at "Java observable pattern".
First I would create the Model class like so.
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.*;
public class Model {
transient ArrayList changeListeners = new ArrayList();
public void loadData(/* variables */) {
/** @todo stuff here */
fireStateChanged(new ChangeEvent(this));
}
public void addChangeListener(ChangeListener listener) {
if (changeListeners != null && !
changeListeners.contains(listener)) {
changeListeners.add(listener);
}
}
public void removeChangeListener(ChangeListener listener) {
if (changeListeners != null) {
changeListeners.remove(listener);
}
}
protected void fireStateChanged(ChangeEvent e) {
if (changeListeners != null) {
for (Iterator iter = changeListeners.iterator();
iter.hasNext(); ) {
ChangeListener listener = (ChangeListener)
iter.next();
listener.stateChanged(e);
}
}
}
}
Then a controller class
public class Controller {
Model model = new Model();
public void doSomeDataAccess() {
/** @todo some data access */
model.loadData(/* data parameters */);
}
public Model getModel() {
return model;
}
}
Then the meat of the application, the frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class View extends JFrame {
JPanel contentPane = null;
JPanel pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT, 5,
2));
JButton cmdButton = new JButton("Do Some Data Access");
Controller controller = new Controller();
Model model = null;
public View() {
createUI();
}
private void createUI() {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(cmdButton);
cmdButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleDoSomeDataAccess();
}
});
/** @todo create the rest of your UI here */
}
private void handleDoSomeDataAccess() {
model = controller.getModel();
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
handleModelChanged();
}
});
controller.doSomeDataAccess();
}
private void handleModelChanged() {
/** @todo update your table or whatever here */
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24