Search code examples
javajbuttonjoptionpanemessageboxjgrasp

Java: How to open JOptionPane from a JButton


I have a program which has many JButtons which opens jframes, I want my help button to open a JoptionPane message box, but everytime I click on the help button nothing happens.

//Main Menu

import javax.swing.*;
// Create Main Menu page

import java.awt.event.*;
import java.awt. *;
import java.io.*;


   public class mainMenu implements ActionListener     // create new class mainMenu
{
    JFrame Start=new JFrame("Main Menu");             // name frame Main Menu
    JButton Search ;
    JButton Create;      // create new buttons
    JButton Delete;
    JButton Help;
 {
      Search=new JButton("Search for Existing Contact"); 
      Create=new JButton("Create a New Contact");           // set name of all buttons
      Delete=new JButton("Delete a Contact");
      Help=new JButton("Help");
      Search.setActionCommand("Search");
      Create.setActionCommand("Create");
      Delete.setActionCommand("Delete");
      Help.setActionCommand("Help");
      Start.setSize(510,600);       // set size of frame
      Start.add(new JLabel(new ImageIcon("mainMenuBG.jpg")));   // add background 
      Start.setVisible(true);
      Start.setLayout(null);
      Start.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      Start.getContentPane().add(Search);
      Start.getContentPane().add(Create);     // add buttons to frame
      Start.getContentPane().add(Delete);
      Start.getContentPane().add(Help);  
      Search.setBounds(100,25,295,88);
      Create.setBounds(100,150,295,88);        // set size of buttons
      Delete.setBounds(100,275,295,88);
      Help.setBounds(100,400,295,88);
      Search.addActionListener(this);
      Create.addActionListener(this);   
      Delete.addActionListener(this);
      Help.addActionListener(this);

 }

  public void actionPerformed(ActionEvent e) {
   String command = e.getActionCommand();
     if ("Search".equals(command)) {
      Start.dispose();
      LTS B=new LTS();
  }    
     else if ("Create".equals(command)) {
      Start.dispose();
      LTS B=new LTS();
  }
     else if ("Delete".equals(command)) {
      Start.dispose();
      LTS B=new LTS();
  }
     else if ("Help".equals(command)) {
      helpPage2 B=new helpPage2();
  }
}

   public static void main(String ag[])
  {
      mainMenu B=new mainMenu();
  }


}

The helpPage2 should open the message box

// Help Page 2

import javax.swing.JOptionPane;

public class helpPage2 {  // Create a new class helpPage

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "HELPPPPP PAGEE","Help", JOptionPane.INFORMATION_MESSAGE); // Create Information Message 
    }
}

Not sure What I am Missing, some guidance would be greatly appreciated, also the other buttons work, just the helpPage2 does not. Thank You.


Solution

  • you can do like this,

     if ("Help".equals(command)) {
    //  helpPage2 B=new helpPage2();
          JOptionPane.showMessageDialog(null, "HELPPPPP PAGEE","Help", JOptionPane.INFORMATION_MESSAGE); // Create Information Message  
    }
    

    with in the method you can call dialog box.

    let me if you have any quires.