Forum Stats

  • 3,783,609 Users
  • 2,254,809 Discussions
  • 7,880,483 Comments

Discussions

JDesktop, JMenu, JInternalFrame

843807
843807 Member Posts: 46,582
edited Feb 27, 2003 8:55PM in Java Desktop Applications
Hi, I'm totaly new to Java and application development, and I need a bit of help, or pointing the right direction,
I have created an application that uses jdesktop to show internalframes when a user clicks on the menu.

What I am trying to do is, say when internalframe_one is open the user cannot open another internalframe_one frame from the menubar, I have tried the following

void jMenuItem2_actionPerformed(ActionEvent e) {
internalframe_one dlg = new internalframe_one();
desktop.add(dlg);
dlg.pack();
dlg.show();
jMenuItem1.setEnabled(false);
}

which is fine but when internalframe_one is closed i cant get the jMenuItem1.setEnabled(false); re-set to jMenuItem1.setEnabled(true);
I have tried various ways that i can think of, but i seem to be getting lost or chasing my tail in circles.
Unless i am being totally dumb and there is a proper way to do it that I cannot find in the help documentation or forums etc.
Appriciate some help,
Thanks

Comments

  • 843807
    843807 Member Posts: 46,582
    Make a class variable for JInternalFrame, then simply show (setVisible(true)) it when the user click on the menu. Don't create a new window everytime.
  • 843807
    843807 Member Posts: 46,582
    Hi I sort of got that but cant figure out where to put it, Ive included the code for the main frame and then the code for the internal frame ( Catagories.java),

    /////MainFrame.java/////////
    package twscart;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JInternalFrame;
    import javax.swing.JDesktopPane;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.JFrame;


    public class MainFrame extends JFrame {
    JDesktopPane desktop;
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileExit = new JMenuItem();
    JMenu jMenuHelp = new JMenu();
    JMenuItem jMenuHelpAbout = new JMenuItem();
    BorderLayout borderLayout1 = new BorderLayout();
    BorderLayout borderLayout2 = new BorderLayout();
    JMenuItem Settings = new JMenuItem();
    JMenuItem Manual = new JMenuItem();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem1 = new JMenuItem();

    //Construct the frame
    public MainFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
    jbInit();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    }
    //Component initialization
    private void jbInit() throws Exception {
    desktop = new JDesktopPane();
    setContentPane(desktop);
    this.setSize(new Dimension(700, 550));
    this.setTitle("Welcome");
    jMenuFile.setText("File");
    jMenuFileExit.setText("Exit");
    jMenuFileExit.addActionListener(new MainFrame_jMenuFileExit_ActionAdapter(this));
    jMenuHelp.setText("Help");
    jMenuHelpAbout.setText("About");
    jMenuHelpAbout.addActionListener(new MainFrame_jMenuHelpAbout_ActionAdapter(this));
    desktop.setPreferredSize(new Dimension(700, 550));
    Settings.setToolTipText("Configure Database settings");
    Settings.addActionListener(new MainFrame_Settings_actionAdapter(this));
    Manual.setText("Manual");
    jMenu1.setText("Screens");
    jMenuItem1.setToolTipText("Product Catagories");
    jMenuItem1.setText("Catagories");
    jMenuItem1.addActionListener(new MainFrame_jMenuItem1_actionAdapter(this));
    jMenuFile.add(Settings);
    jMenuFile.addSeparator();
    Settings.setText("Settings");
    jMenuFile.add(jMenuFileExit);
    jMenuHelp.addSeparator();
    jMenuHelp.add(jMenuHelpAbout);
    jMenuBar1.add(jMenuFile);
    jMenuBar1.add(jMenu1);
    jMenuBar1.add(jMenuHelp);
    jMenu1.add(jMenuItem1);
    this.setJMenuBar(jMenuBar1);
    desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    }
    //File | Exit action performed
    public void jMenuFileExit_actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    //Help | About action performed
    public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
    MainFrame_AboutBox dlg = new MainFrame_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.pack();
    dlg.show();
    }
    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    jMenuFileExit_actionPerformed(null);
    }
    }

    void Settings_actionPerformed(ActionEvent e) {
    DBaseConn dlg = new DBaseConn();
    desktop.add(dlg);
    dlg.pack();
    dlg.show();
    Settings.setEnabled(false);
    }

    //this opens the internal fram catagories and disables the menu button
    void jMenuItem1_actionPerformed(ActionEvent e) {
    Catagories dlg = new Catagories();
    desktop.add(dlg);
    dlg.pack();
    dlg.show();
    jMenuItem1.setEnabled(false);
    }

    }

    class MainFrame_jMenuFileExit_ActionAdapter implements ActionListener {
    MainFrame adaptee;

    MainFrame_jMenuFileExit_ActionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
    adaptee.jMenuFileExit_actionPerformed(e);
    }
    }

    class MainFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
    MainFrame adaptee;

    MainFrame_jMenuHelpAbout_ActionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
    adaptee.jMenuHelpAbout_actionPerformed(e);
    }
    }

    class MainFrame_Settings_actionAdapter implements java.awt.event.ActionListener {
    MainFrame adaptee;

    MainFrame_Settings_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
    adaptee.Settings_actionPerformed(e);
    }
    }

    class MainFrame_jMenuItem1_actionAdapter implements java.awt.event.ActionListener {
    MainFrame adaptee;

    MainFrame_jMenuItem1_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
    adaptee.jMenuItem1_actionPerformed(e);
    }
    }
    //////////end MainFrame.java///////////

    //////////start Catagories.java///////////
    package twscart;

    import javax.swing.*;
    import com.borland.dbswing.*;
    import java.awt.*;
    import javax.swing.event.*;

    public class Catagories extends JInternalFrame {
    JdbNavToolBar jdbNavToolBar1 = new JdbNavToolBar();
    JdbStatusLabel jdbStatusLabel1 = new JdbStatusLabel();
    JdbToggleButton jdbToggleButton1 = new JdbToggleButton();

    public Catagories() {
    try {
    jbInit();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    Catagories catagories = new Catagories();
    }
    private void jbInit() throws Exception {
    setLocation(20, 20);
    this.setClosable(true);
    this.setIconifiable(true);
    this.setNormalBounds(new Rectangle(10, 10, 400, 350));
    this.setTitle("Catagories");
    this.setEnabled(true);
    this.setPreferredSize(new Dimension(400, 350));
    this.addInternalFrameListener(new Catagories_this_internalFrameAdapter(this));
    jdbStatusLabel1.setText("jdbStatusLabel1");
    jdbToggleButton1.setText("Connect");
    jdbNavToolBar1.setButtonStateSave(JdbNavToolBar.DISABLED);
    jdbNavToolBar1.setButtonStateRefresh(JdbNavToolBar.DISABLED);
    this.getContentPane().add(jdbNavToolBar1, BorderLayout.NORTH);
    jdbNavToolBar1.add(jdbToggleButton1, null);
    this.getContentPane().add(jdbStatusLabel1, BorderLayout.SOUTH);
    }

    class Catagories_this_internalFrameAdapter extends javax.swing.event.InternalFrameAdapter {
    Catagories adaptee;

    Catagories_this_internalFrameAdapter(Catagories adaptee) {
    this.adaptee = adaptee;
    }
    }}
    ////////end Catagories.java///////////////////////

    ok so when I click on Settings or Catagories in the menu the internal frame opens and the menu item is disabled but visible, which is cool
    now when i close the frame Catagories I need to re-enable the Catagories menu item, jMenuItem1.setEnabled(true);

    Sorry if i seem a bit stupid on this but the Catagories.java would connect to a remote sql database and if they can open more than 1 frame it could cause a stack overflow or dbase problem, which is not what anybody wants.

    Thanks for any help
  • 843807
    843807 Member Posts: 46,582
    Hi Looking through your code I can see what you are trying to do. I suggest using a listener to listen for the change in visibility of your Catagories component. As you are listening for visbility a Component Listener (would be your best bet.

    try something like this
    ....
    Catagories dlg = new Catagories();
    dlg.addComponentListener((new ComponentListener()
        {
          public void componentHidden(java.awt.event.ComponentEvent e)
          {
            jMenuItem1.setEnabled(true);
          }
          public void componentShown(java.awt.event.ComponentEvent e){}
          public void componentMoved(java.awt.event.ComponentEvent e){}
          public void componentResized(java.awt.event.ComponentEvent e){}
        });
    That sould do the trick, but if it aint working just right, then have a dig through the API documentation regarding listeners till you hit apon the right one for you.

    - Tony
  • 843807
    843807 Member Posts: 46,582
    You are a life saver...
    It worked thanks very much for your help,
    I did start to look into listners but got lost part the way through now seeing some code, I get the idea.
    I have sent the Duke dollars to you so you should have them by now,
    Once again thanks
This discussion has been closed.