aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to enable/disable this item? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to enable/disable this item?" Watch "how to enable/disable this item?" New topic
Author

how to enable/disable this item?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
This seems stupid question,but I tried to solve it but in vain. Could you pl help me?
My problem is I was making one program in Swing. and I was making one MenuBar and ToolBar. The Objective is to have some of its component disabled intally, which I was able to achieve successfully.
Now in the menu I have change user/Login under File menu on clicin same i am calling a panel where I was asking the user to enter his/her id/pwd. I would like on successful login I want that Disconnect for file menu and compatibility of Help menu should be enabled.
Now problem is that I am using the single variable and could not able to understand how to call these components and make them enabled. could you please help me.
regards,
Arun Mahajan
Code:-
**********
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class test extends JFrame
implements ActionListener,ItemListener
{
JButton toolbutton;
JToolBar toolbar;

public test()
{
JMenuBar menubar;
JMenu menu;
JMenuItem menuItem;
Containercont = getContentPane();
cont.setBackground(new Color(140,199,222));
cont.setFont(new Font("arial",Font.BOLD,11));
cont.setLayout(null);
menubar = new JMenuBar();
setJMenuBar(menubar);
//Build File Menu
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menubar.add(menu);
//Items in File Menu
menuItem = new JMenuItem("Change User/Login",KeyEvent.VK_C);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
//adding separator
menu.addSeparator();
menuItem = new JMenuItem("Disconnect",KeyEvent.VK_D);
menuItem.addActionListener(this);
menuItem.setEnabled(false);
menu.add(menuItem);
menuItem = new JMenuItem("Clear Screen",KeyEvent.VK_S);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.addActionListener(this);
menu.add(menuItem);
//adding separator
menu.addSeparator();
menuItem = new JMenuItem("Exit",KeyEvent.VK_X);
menuItem.addActionListener(this);
menu.add(menuItem);
//Build Help Menu
menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_H);
menubar.add(menu);
//Items in Help Menu
menuItem = new JMenuItem("Compatibility",KeyEvent.VK_P);
menuItem.addActionListener(this);
menuItem.setEnabled(false);
menu.add(menuItem);
menuItem = new JMenuItem("About ",KeyEvent.VK_B);
menuItem.addActionListener(this);
menu.add(menuItem);
//setting Top Left Icon
Image img = Toolkit.getDefaultToolkit().getImage("images/picon.gif");
setIconImage(img);
//setting frame size
setSize(450,400);

//setting toolbar
toolbar = new JToolBar();
toolbar.setBackground(new Color(140,199,222));
addToolBarButtons(toolbutton);
toolbar.setBounds(-2,-2,290,30);
toolbar.setFloatable(false);
cont.add(toolbar);
//setting windows listeners
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
protected void addToolBarButtons(JButton jbutton)
{
jbutton = new JButton(new ImageIcon("images/buttons/change_user.gif"));
jbutton.setToolTipText("Change User/Login");
jbutton.addActionListener(this);
toolbar.add(jbutton);
jbutton = new JButton(new ImageIcon("images/buttons/disconnect.gif"));
jbutton.setToolTipText("Disconnect");
jbutton.addActionListener(this);
jbutton.setEnabled(false);
toolbar.add(jbutton);
jbutton = new JButton(new ImageIcon("images/buttons/clear.gif"));
jbutton.setToolTipText("Clear Screen");
jbutton.addActionListener(this);
toolbar.add(jbutton);
jbutton = new JButton(new ImageIcon("images/buttons/exit.gif"));
jbutton.setToolTipText("Exit");
jbutton.addActionListener(this);
toolbar.add(jbutton);

toolbar.addSeparator(new Dimension(4,20));
jbutton = new JButton(new ImageIcon("images/buttons/compatibility.gif"));
jbutton.setToolTipText("Compatibility");
jbutton.addActionListener(this);
toolbar.add(jbutton);
jbutton = new JButton(new ImageIcon("images/buttons/exit.gif"));
jbutton.setToolTipText("About ");
jbutton.addActionListener(this);
toolbar.add(jbutton);
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Entered into ActionListener");
Object o = evt.getSource();
String s= ""+o;
if(s.indexOf("JMenuItem")>0)
{
s="";
JMenuItem source = (JMenuItem)(evt.getSource());
s = ""+source.getText();
}
else if(s.indexOf("JButton")>0)
{
s="";
JButton source = (JButton)(evt.getSource());
s = ""+source.getToolTipText();
}
//for File Menus
if("Change User/Login".compareToIgnoreCase(s)==0)
{
System.out.println("From Login");
}
else if("Disconnect".equals(s))
{
System.out.println("Disconnect");
}
else if("Clear Screen".equals(s))
{
System.out.println("Clear Screen");
}
else if("Exit".equals(s))
{
System.exit(1);
}

//For Help Menus
else if("Compatibility".equals(s))
{
System.out.println("Compatibility");
}
else if("About ".equals(s))
{
System.out.println("About");
}
else if((evt.getActionCommand()=="Login"))
{
System.out.println("Login Submit Button");
}
}
public static void main(String args[])
{
test ls=new test();
ls.setTitle("test!!");
ls.setVisible(true);
}
}
**************************
[ March 02, 2002: Message edited by: arun mahajan ]
[ March 02, 2002: Message edited by: arun mahajan ]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arun,
You first need to move your JMenuBar out to a class variable in order to get a handle on it outside of your constructor. Once this is done, you can use the following snippets to get at the menu items:

Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to enable/disable this item?