Hi guys I have written a class that uses a frame and Swing components. Unfortunately the traversal policy that i wrote does not seem to work for the jmenu. Can u guys help me fox it....much appreciated
public class MainFrame extends JFrame {
//<editor-fold defaultstate="collapsed" desc="Variables">
JMenuBar jmb = new JMenuBar();
JMenu group = new JMenu("Group");
JMenu cheque = new JMenu("Cheque");
JMenuItem zero = new JMenuItem("Zero");
JMenuItem one = new JMenuItem("One");
JMenuItem two = new JMenuItem("Two");
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Constructor">
/**
* constructor for MainFrame
*/
public MainFrame()
{
setJMenuBar(jmb);
jmb.add(group);
jmb.add(cheque);
zero.addKeyListener(new KeyEvents());
group.add(zero);
one.addKeyListener(new KeyEvents());
group.add(one);
two.addKeyListener(new KeyEvents());
group.add(two);
@SuppressWarnings("UseOfObsoleteCollectionType")
Vector<Component> order = new Vector<Component>();
order.add(group);
order.add(zero);
order.add(one);
order.add(two);
order.add(cheque);
//SingletonMain.getInstance().setFocusTraversalOrder(order);
MyOwnFocusTraveralPolicy f = new MyOwnFocusTraveralPolicy();
f.setMyOwnFocusTraveralPolicy(order);
setTitle("Cheque Program-- Group 0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setFocusTraversalPolicy(SingletonMain.getInstance().getFocusTraversalPolicy());
setFocusTraversalPolicy(f);
setBounds(0, 0, 1370, 730);
setVisible(true);
}
//</editor-fold>
private class MyOwnFocusTraveralPolicy extends FocusTraversalPolicy
{
Vector<Component> order = new Vector<Component>();
public void setMyOwnFocusTraveralPolicy(Vector<Component> order) {
this.order = new Vector<Component>(order.size());
this.order.addAll(order);
}
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent)
{
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent)
{
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
public Component getLastComponent(Container focusCycleRoot) {
return order.lastElement();
}
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}
}