Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #1376 > unrolled thread
| Started by | "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> |
|---|---|
| First post | 2011-04-27 15:32 +0000 |
| Last post | 2011-04-27 15:33 +0000 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.gui
question about keeping th "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
Re: question about keepin "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
Re: question about keepin "bcr666" <bcr666@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
Re: question about keepin "bcr666" <bcr666@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
Re: question about keepin "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
Re: question about keepin "Ed Tidwell" <ed.tidwell@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
| From | "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:32 +0000 |
| Subject | question about keeping th |
| Message-ID | <1175547610.827079.229990@p77g2000hsh.googlegroups.com> |
To: comp.lang.java.gui
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!
---
* 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
[toc] | [next] | [standalone]
| From | "Tom Hawtin" <tom.hawtin@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:32 +0000 |
| Subject | Re: question about keepin |
| Message-ID | <46118376$0$8711$ed2619ec@ptn-nntp-reader02.plus.net> |
| In reply to | #1376 |
To: comp.lang.java.gui
Adam Sandler wrote:
>
> Does Java have an equivalent of FindComponent, or a something similar
> to the code snip above?
There is a name property on Component, but it's rarely used.
> 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...
JRootPane is a Container containing other components, which a Containers
containing other components, ...
From 1.5, to "simplify" things, you can add components to JFrame and
set its layout. What you are actually doing (provided the JFrame is in
the write mode) is to add the component or set the layout on the content
pane. This "simplification" has made Swing's wacky ways more difficult
for the learner, IMO. However, it does make trivial demos shorter.
The general way of doing it is to configure the JList (or whatever)
before you add it to the Container. Then you don't have to worry about
getting it back out. So:
* set your logic up on the model
* (possibly elsewhere in code) create the component created using the
model and configure the styling
* (possibly elsewhere in code) add the component to its container with
the appropriate layout configuration
> public class MyGUI() extends JFrame
> {
It's usually a bad idea to extend classes needlessly. I know bad
tutorials do it to make the code shorter (but more complicated).
Tom Hawtin
---
* 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
[toc] | [prev] | [next] | [standalone]
| From | "bcr666" <bcr666@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:32 +0000 |
| Subject | Re: question about keepin |
| Message-ID | <1175553208.133444.115550@n76g2000hsh.googlegroups.com> |
| In reply to | #1376 |
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
[toc] | [prev] | [next] | [standalone]
| From | "bcr666" <bcr666@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:32 +0000 |
| Subject | Re: question about keepin |
| Message-ID | <1175553969.878510.305770@n59g2000hsh.googlegroups.com> |
| In reply to | #1378 |
To: comp.lang.java.gui
Oh, BTW if you are talking about using something like JTable on your
JFrame, you can use a DefaultTableModel (or subclass it) for the
model.
public class Controller {
DefaultTableModel model = new DefaultTableModel();
...
}
public class View extends JFrame() {
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class View extends JFrame {
...
Controller controller = new Controller();
JTable myTable = null; // changed line
...
private void createUI() {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(pnlNorth, BorderLayout.NORTH);
contentPane.add(new JScrollPane(myTable),
BorderLayout.CENTER); // Added Line
pnlNorth.add(cmdButton);
cmdButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleDoSomeDataAccess();
}
});
/** @todo create the rest of your UI here */
}
private void handleDoSomeDataAccess() {
DefaultTableModel model = controller.getModel(); // changed
line
myTable.setModel(model); // added line this will automatically
add the myTable as a listener to the model
controller.doSomeDataAccess();
}
private void handleModelChanged() { // you can remove this method
/** @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
[toc] | [prev] | [next] | [standalone]
| From | "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:32 +0000 |
| Subject | Re: question about keepin |
| Message-ID | <1175608876.824650.274700@e65g2000hsc.googlegroups.com> |
| In reply to | #1378 |
To: comp.lang.java.gui On Apr 2, 4:33 pm, "bcr666" <bcr...@gmail.com> wrote: > You should also look at "Java observable pattern". Thanks everyone for their help. BTW, I did look at observable in my Head First Design Patterns book. But their example for Swing (pg 72) didn't stimulate any creativity. All their example does is add multiple event listeners to a component. Thanks again! --- * 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
[toc] | [prev] | [next] | [standalone]
| From | "Ed Tidwell" <ed.tidwell@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | Re: question about keepin |
| Message-ID | <461bd27b$0$17188$4c368faf@roadrunner.com> |
| In reply to | #1376 |
To: comp.lang.java.gui Adam Sandler 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. > I actually wrote a Swing application framework to do just this. Basically, what I did is created Actions and hooked component listeners to delegate back to a mediator class. Anything that I wanted to 'FindComponent' on I just created had it implement a Mediatable interface. Then added it to a Map collection to easily register it with the applications Mediator object. The mediator class is where I keep all of my business logic. Happy to send you examples if your interested in learning more. You can find the OpenSource project here: https://tframe.dev.java.net/ JSR 296 will address a lot of this in Java 7 when it ships. Kind regards, Ed --- * 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.gui
csiph-web