I am quiet new to using NetBeans IDE. I have created a file menu. When the user selects an option from the menu, a window should open,or a form should be loaded. So as far as i have figured out, the event needs to be handled by a mouseClick method. Within this method i have to code in a way, so that, it opens the Window the option refers to. May be what i am trying to say is sounding extremely ambiguous but I'll try giving an example.

suppose the file menu has an option create new a/c... so when the user clicks on that option.. it should load a form where the user has to enter his/her personal details.

i cannot understand how to load that form. It would be very helpful if someone could help me with the code or syntax or refer some online tutorials,books. Any sort of help is welcome.

Thank you,
Sreya.

Recommended Answers

All 9 Replies

You can create a separate java file. That class would be the form. It will have fields and labels and buttons.
When you select the option, instantiate that class. It should extend the JFrame class. So when you create that instance and make it visible enter whatever you want. You can have buttons with actions as well.

Thanks a lot for help. I have already created a separate java class for the form (and i have designed the interface) i want to load when the user clicks on the menu item. But i am not sure about the code/syntax which will load that form( as in instantiating that class). Yes this class extends JFrame. If you could help me with the code or syntax it would be really helpful.

Thank you,
Sreya.

What code do you have in your main method in order to start the application?

/*
 * StudentManagementView.java
 */

package studentmanagement;

import java.awt.Frame;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class StudentManagementView extends FrameView {

    public StudentManagementView(SingleFrameApplication app) {
        super(app);

        initComponents();



        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = StudentManagementApp.getApplication().getMainFrame();
            aboutBox = new StudentManagementAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        StudentManagementApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu studInfo = new javax.swing.JMenu();
        jStudInfoNew = new javax.swing.JMenuItem();
        jStudInfoEdit = new javax.swing.JMenuItem();
        jStudInfoView = new javax.swing.JMenu();
        jStudInfoDelete = new javax.swing.JMenuItem();
        studAcc = new javax.swing.JMenu();
        jStudAccNew = new javax.swing.JMenuItem();
        jStudAccEdit = new javax.swing.JMenuItem();
        jStudAccView = new javax.swing.JMenu();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(studentmanagement.StudentManagementApp.class).getContext().getResourceMap(StudentManagementView.class);
        menuBar.setBorder(javax.swing.BorderFactory.createEtchedBorder(null, resourceMap.getColor("menuBar.border.shadowColor"))); // NOI18N
        menuBar.setName("menuBar"); // NOI18N

        studInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder(null, resourceMap.getColor("studInfo.border.shadowColor"))); // NOI18N
        studInfo.setText(resourceMap.getString("studInfo.text")); // NOI18N
        studInfo.setName("studInfo"); // NOI18N

        jStudInfoNew.setText(resourceMap.getString("jStudInfoNew.text")); // NOI18N
        jStudInfoNew.setName("jStudInfoNew"); // NOI18N
        studInfo.add(jStudInfoNew);

        jStudInfoEdit.setText(resourceMap.getString("jStudInfoEdit.text")); // NOI18N
        jStudInfoEdit.setName("jStudInfoEdit"); // NOI18N
        studInfo.add(jStudInfoEdit);

        jStudInfoView.setText(resourceMap.getString("jStudInfoView.text")); // NOI18N
        jStudInfoView.setName("jStudInfoView"); // NOI18N
        studInfo.add(jStudInfoView);

        jStudInfoDelete.setText(resourceMap.getString("jStudInfoDelete.text")); // NOI18N
        jStudInfoDelete.setName("jStudInfoDelete"); // NOI18N
        studInfo.add(jStudInfoDelete);

        menuBar.add(studInfo);

        studAcc.setBorder(javax.swing.BorderFactory.createEtchedBorder(null, resourceMap.getColor("studAcc.border.shadowColor"))); // NOI18N
        studAcc.setText(resourceMap.getString("studAcc.text")); // NOI18N
        studAcc.setName("studAcc"); // NOI18N

        jStudAccNew.setText(resourceMap.getString("jStudAccNew.text")); // NOI18N
        jStudAccNew.setName("jStudAccNew"); // NOI18N
        studAcc.add(jStudAccNew);

        jStudAccEdit.setText(resourceMap.getString("jStudAccEdit.text")); // NOI18N
        jStudAccEdit.setName("jStudAccEdit"); // NOI18N
        studAcc.add(jStudAccEdit);

        jStudAccView.setText(resourceMap.getString("jStudAccView.text")); // NOI18N
        jStudAccView.setName("jStudAccView"); // NOI18N
        studAcc.add(jStudAccView);

        menuBar.add(studAcc);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(studentmanagement.StudentManagementApp.class).getContext().getActionMap(StudentManagementView.class, this);
        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(resourceMap.getColor("statusPanel.border.highlightColor"), null)); // NOI18N
        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 339, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(203, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap(281, Short.MAX_VALUE)
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(statusMessageLabel)
                        .addComponent(statusAnimationLabel))
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11))
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>                        

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem jStudAccEdit;
    private javax.swing.JMenuItem jStudAccNew;
    private javax.swing.JMenu jStudAccView;
    private javax.swing.JMenuItem jStudInfoDelete;
    private javax.swing.JMenuItem jStudInfoEdit;
    private javax.swing.JMenuItem jStudInfoNew;
    private javax.swing.JMenu jStudInfoView;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JMenu studAcc;
    // End of variables declaration                   

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;


}

This is the class which needs to load another class called AddNew.java when the menu item "New" is clicked. As I have said before i am new to using an IDE... the generated code is confusing me even more. So if u could help me fix this, it would be really really of great help.

When you start the application, the main method is executed. You have a class as the "starting point" and inside there is main method. Inside the main method there is probably an initialization of the object. Post what is inside the main class.

And then do the same thing. If you want to show that new frame you created, just initialize it by calling 'new' and then since it extends the JFrame just call the setVisible(true) method.

And don't use IDE for Gui and swing if you don't know what you are doing. Read some tutorials on how to write swing applications by hand and forget any GUI builder there is.

Yeah actually you are right. Even i don't want to use the IDE while im still getting my concepts clear. Ok i will post the code of main class. I thought we could load the "AddNew.java" form from the class where the "filemenu" is declared. But i've been 'asked' to use an IDE so here i am stuck and struggling. :(
I guess setVisible(true) is what i need to do to load that form after creating it's object. I'll definitely try and let you know. Thanks a lot for helping out.

/*
 * StudentManagementApp.java
 */

package studentmanagement;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class StudentManagementApp extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new StudentManagementView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of StudentManagementApp
     */
    public static StudentManagementApp getApplication() {
        return Application.getInstance(StudentManagementApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(StudentManagementApp.class, args);

    }
}

I actually enjoy programming and I'm genuinely eager to learn than just to get the work done. But I do not have enough time in hand to first learn to implement the whole thing without using IDE and then again do it using IDE. I have a deadline to meet. After this project is done i will definitely go through every detail.It would be kind if you could suggest some online tutorials also.

I can't understand anything from that code. Did you wrote that or it was generated by NetBeans?

Anyway. Assume that you have your form:

class MyForm extends JFrame {
  
  // declare components (text fields, buttons and others) 

  public MyForm() {
    // initialize the components put and add them to the form.
    // Here is when you need to use the NetBeans gui builder. When placing the components at the form.
  }
}

After you are done, whenever you want to display that form simply call it:

MyForm form = new MyForm();
form.setVisible(true);

Remember it is an object. It can have attributes with get/set methods. You can pass as parameter another form and hide/show it depending on a button clicked. Or you can have the MyForm you created as a local attribute to another form. By calling setVisible(false), you can hide it and keep on calling any methods you have declared for that object as long as it is scope.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.