Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello Sir, I wanted to set

Status
Not open for further replies.

VenkatRastapuram

Programmer
Oct 24, 2003
46
IN
Hello Sir,


I wanted to set Tab order to my application.i.e First I wanted to set the focus to Menu in my application and after that on every Tab event I have to change the focus.
How to do this. Please help me in this regard

R.Venkatesh
MakeLogic
venkat@makelogic.com
 
//See if the code suit you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class IntroExample extends JMenuBar {

String[ ] fileItems = new String[ ] { "New", "Open", "Save", "Exit" };
String[ ] editItems = new String[ ] { "Undo", "Cut", "Copy", "Paste" };
char[ ] fileShortcuts = { 'N','O','S','X' }; //_N
char[ ] editShortcuts = { 'Z','X','C','V' }; //control z
final JMenu fileMenu;
public IntroExample() {

fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu otherMenu = new JMenu("Other");
// Assemble the File menus with mnemonics.
ActionListener printListener = new ActionListener( ) {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand( ) +
"] was pressed.");
}
};
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems, fileShortcuts);
item.addActionListener(printListener);
fileMenu.add(item);
}

// Assemble the File menus with keyboard accelerators.
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts,
Toolkit.getDefaultToolkit( ).getMenuShortcutKeyMask( ), false));
item.addActionListener(printListener);
editMenu.add(item);
//if (i==3) item.setDisplayedMnemonicIndex(-1);
}

// Insert a separator in the Edit menu in Position 1 after &quot;Undo&quot;.
editMenu.insertSeparator(1);

// Assemble the submenus of the Other menu.
JMenuItem item;

// Assemble the Other menu itself.

otherMenu.add(item = new JCheckBoxMenuItem(&quot;Check Me&quot;));
item.addActionListener(printListener);
otherMenu.addSeparator( );
ButtonGroup buttonGroup = new ButtonGroup( );
otherMenu.add(item = new JRadioButtonMenuItem(&quot;Radio 1&quot;));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem(&quot;Radio 2&quot;));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator( );
otherMenu.add(item = new JMenuItem(&quot;Potted Plant&quot;,
new ImageIcon(&quot;image.gif&quot;)));
item.addActionListener(printListener);

// Finally, add all the menus to the menu bar.
add(fileMenu);
add(editMenu);
add(otherMenu);

}

public static void main(String s[ ]) {
JFrame frame = new JFrame(&quot;Simple Menu Example&quot;);
frame.getContentPane().setLayout(null);
frame.setBounds(new Rectangle(0, 0, 200, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final IntroExample myIntro = new IntroExample();
frame.setJMenuBar(myIntro);
//frame.getContentPane().setLayout(null);

final menuFocus menuFocusObj = new menuFocus(false);
JPanel jPan = new JPanel();
JPanel jPan2 = new JPanel();

jPan.setBounds(0,0,200,250);
//jPan.setBackground(Color.white);
frame.getContentPane().add(jPan);
jPan.setLayout(new GridLayout(4,1));
//jPan2.setLayout(new GridLayout(1,2));

JTextField jText1 = new JTextField(&quot;Text1&quot;);
JTextField jText2 = new JTextField(&quot;Text2&quot;);
JTextField jText3 = new JTextField(&quot;Text3&quot;);
//JLabel checkLabel = new JLabel(&quot;Tap to menu&quot;);
jPan.add(jText1);
jPan.add(jText2);
jPan.add(jText3);
jPan.add(jPan2);
//jPan2.add(checkLabel);

final JCheckBox chBox = new JCheckBox(&quot;Tap to menu&quot;,false);
chBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
if (source==chBox)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
menuFocusObj.setFlag(true);
}
else
{
menuFocusObj.setFlag(false);
}

}
}

}
);
chBox.addFocusListener(new FocusListener()
{

public void focusGained(FocusEvent e)
{
}

public void focusLost(FocusEvent e)
{
if (menuFocusObj.getFlag()==true)
(myIntro.fileMenu).doClick();
}

}
);
jPan2.add(chBox);
frame.setVisible(true);
(myIntro.fileMenu).doClick();
frame.repaint();
}

}
class menuFocus
{
private boolean enabled;
public menuFocus(boolean temp)
{
enabled = temp;
}
public void setFlag(boolean temp1)
{
enabled = temp1;
}
public boolean getFlag()
{
return enabled;
}
}
//Some object are final because I have to access it within Anonymous class
//Please Mark this post as helpful if the code suits you.
 
prosper, if you put your code between the tags :

Code:

then it does not appear as italic (this is because you access an array like so :



which tells the &quot;TGML&quot; processor to put the text in italics ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top