Re: [NetBeans]changing menuitems
- From: RedGrittyBrick <RedGrittyBrick@xxxxxxxxxxxxx>
- Date: Sun, 27 Jul 2008 23:46:14 +0100
Daniel Moyne wrote:
Mark Space wrote:
Daniel Moyne wrote:No but both you and me basically have the same approach once you have access
Thanks very interesting idea but I still need the instance of GenListViewYes you do need an instance of GenListView. Is that a problem? I
to use method setEnabledMenu1.
Thanks
didn't see any request to change that in the original problem statement.
You needed an instance of GenListView in your code as well....
to the instance of GenListView ; the other interesting approach if possible
would be to add a special listner in GenListView to some particular events
generated by other classes (like request to change menus status) but I do
not know how to design this.
Thanks.
Here's one way.
----------------------------------- 8< ------------------------------
package org.redgrittybrick.test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Gen {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Gen();
}
});
}
private static final String PRIOR = "Prior", NEXT = "Next";
// APPLICATION
Gen() {
GenModel model = new GenModel();
GenControl controller = new GenControl(model);
JFrame view = new GenListView(model, controller);
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.pack();
view.setLocationRelativeTo(null);
view.setVisible(true);
}
// A VIEW
class GenListView extends JFrame implements Observer {
private JLabel label = new JLabel();
private JMenuItem priorItem, nextItem;
private JButton priorButton, nextButton;
private GenModel model;
GenListView(GenModel model, GenControl controller) {
this.model = model;
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Go");
priorItem = new JMenuItem(PRIOR);
priorItem.addActionListener(controller);
menu.add(priorItem);
nextItem = new JMenuItem(NEXT);
nextItem.addActionListener(controller);
menu.add(nextItem);
bar.add(menu);
setJMenuBar(bar);
JPanel panel = new JPanel(new GridLayout(1,3));
priorButton = new JButton(PRIOR);
priorButton.addActionListener(controller);
panel.add(priorButton);
nextButton = new JButton(NEXT);
nextButton.addActionListener(controller);
panel.add(nextButton);
update(null,null);
panel.add(label);
add(panel);
model.addObserver(this);
}
@Override
public void update(Observable observable, Object object) {
label.setText(model.get());
priorButton.setEnabled(!model.isFirst());
priorItem.setEnabled(!model.isFirst());
nextButton.setEnabled(!model.isLast());
nextItem.setEnabled(!model.isLast());
}
}
// A CONTROLLER
class GenControl implements ActionListener {
GenModel model;
GenControl(GenModel model) {
this.model = model;
}
@Override
public void actionPerformed(ActionEvent e) {
String actionName = e.getActionCommand();
if (actionName.equals(PRIOR)) {
model.prior();
} else if (actionName.equals(NEXT)) {
model.next();
} else {
System.err.println("Bug: Unexpected action: "
+ actionName);
}
}
}
// A MODEL
class GenModel extends Observable {
private final String[] data = { "One", "Two", "Three", "Four" };
private int current = 0;
public String get() {
return data[current];
}
public void next() {
if (current + 1 < data.length)
current++;
else
current = 0;
setChanged();
notifyObservers();
}
public void prior() {
if (current > 0)
current--;
else
current = data.length - 1;
setChanged();
notifyObservers();
}
public boolean isFirst() {
return current == 0;
}
public boolean isLast() {
return current == data.length - 1;
}
}
}
----------------------------------- 8< ------------------------------
Hope that helps.
--
RGB
.
- Follow-Ups:
- Re: [NetBeans]changing menuitems
- From: thufir
- Re: [NetBeans]changing menuitems
- References:
- [NetBeans]changing menuitems
- From: Daniel Moyne
- Re: [NetBeans]changing menuitems
- From: Mark Space
- Re: [NetBeans]changing menuitems
- From: Daniel Moyne
- Re: [NetBeans]changing menuitems
- From: Mark Space
- Re: [NetBeans]changing menuitems
- From: Daniel Moyne
- [NetBeans]changing menuitems
- Prev by Date: Re: [General]acces of members of subclass
- Next by Date: Re: adding a List of JTextField to the GUI
- Previous by thread: Re: [NetBeans]changing menuitems
- Next by thread: Re: [NetBeans]changing menuitems
- Index(es):