0

I want to Reset the state of checkboxMenuItems.For example I create More than one Document then add some text to recently opened document and select LineNumbers then I go to previos opened Document at that time the linenumbers checkbox is reset to original state that is unselect state whether I select it in "check state" in another Dcoument.please help me.Thank you.

Here is code:

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class ResetItems extends javax.swing.JFrame {

    JTextField status;
    int i=0;
    JTextPane currentTextPane,lnNum;
    JScrollPane scrollPane;

    public ResetItems() {
        initComponents();
        lineNumbers.setSelected(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        create = new javax.swing.JMenuItem();
        lineNumbers = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        file.setText("File");

        create.setText("Create");
        create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createActionPerformed(evt);
            }
        });
        file.add(create);

        lineNumbers.setSelected(true);
        lineNumbers.setText("LineNumbers");
        lineNumbers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lineNumbersActionPerformed(evt);
            }
        });
        file.add(lineNumbers);

        menuBar.add(file);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void createActionPerformed(java.awt.event.ActionEvent evt) {                                       
        final JInternalFrame internalFrame = new JInternalFrame("");
        i++;
        internalFrame.setName("Doc "+i);
        internalFrame.setClosable(true);
        internalFrame.setAutoscrolls(true);
        currentTextPane=new JTextPane();
        currentTextPane.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
        tabbedPane.add(internalFrame);
        tabbedPane.add(internalFrame);
        tabbedPane.setSelectedIndex(i-1);
        currentTextPane.setCaretPosition(0);
        scrollPane=new JScrollPane(currentTextPane);
        internalFrame.add(scrollPane);
        internalFrame.setSize(internalFrame.getMaximumSize());
        internalFrame.pack();
        internalFrame.setVisible(true);
    }                                      

    private void lineNumbersActionPerformed(java.awt.event.ActionEvent evt) {                                            

    }                                           

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ResetItems.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ResetItems().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenuItem create;
    private javax.swing.JMenu file;
    private javax.swing.JCheckBoxMenuItem lineNumbers;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration                   
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • You going to need to set up a state model which is keyed by the `Document`. When you change documents, you're going to reinstate the given state model to the application. This should then change the states of the various objects you want. There are a number of ways to achieve, you could use a singleton, which provided notification to interested parties that the state has changed and they should update themselves for example – MadProgrammer Jul 30 '14 at 06:31
  • Okay,Please provide an example. – user3792689 Jul 30 '14 at 08:55
  • You first. Do some research into the ideas present, try a few different designs, see what you can come up with, post your efforts when you get stuck and need guidance. You question is already to broad to be answerable within the context of SO – MadProgrammer Jul 30 '14 at 09:04
  • I have no idea about Swing MVC.Please provide an example or try my application once. – user3792689 Aug 01 '14 at 08:09
  • Consider this an opportunity to learn ;) – MadProgrammer Aug 01 '14 at 08:57
  • Please help me,At least Provide an Example. – user3792689 Aug 06 '14 at 10:14
  • internalFrame.setJMenuBar(...) – MadProgrammer Aug 06 '14 at 10:23
  • I add that one.But,nothing is happened.can you try it once. – user3792689 Aug 06 '14 at 10:40
  • See how you built your original `JMenuBar`, you need to do the same thing with for the internal frame, but limit to those options which a relevant to the internal frame... – MadProgrammer Aug 06 '14 at 10:41
  • I can built the JMenuBar with menuItems.Here what can I take as menuitems. – user3792689 Aug 06 '14 at 10:54

0 Answers0