hello everybody;
i want to know that how i will open another gui when i click on a button??

Recommended Answers

All 3 Replies

1. You can have your class implement ActionListener.
2. Register a button (for example) with addActionListener method.
3. In your actionPerformed method, define the other GUI.

e.g code

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.util.*;

public class DemoJFileChooser extends JPanel
   implements ActionListener {
	JButton go;
   JFileChooser chooser;
   String choosertitle;
  public DemoJFileChooser() {
    go = new JButton("Select directory");

  public void actionPerformed(ActionEvent e) {
    int result;
    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File("."));
    // disable the "All files" option.
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
      System.out.println("getSelctedDirectory(): " 
         +  chooser.getSelectedFile());
      File folder = chooser.getSelectedFile();
      File[] listOfFiles = folder.listFiles();

      for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
          System.out.println("File " + listOfFiles[i].getName());
        } else if (listOfFiles[i].isDirectory()) {
          System.out.println("Directory " + listOfFiles[i].getName());
    else {
      System.out.println("No Selection ");
  public Dimension getPreferredSize(){
    return new Dimension(200, 200);
  public static void main(String s[]) {
    JFrame frame = new JFrame("");
    DemoJFileChooser panel = new DemoJFileChooser();
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {

Like girola said:
--->You can have your class implement ActionListener.

e.g public class myclass extends JFrame implements actionListener
dont implement action Listener
Call the:

ButtonHandler handler = new Button Handler(); class

to call another class call its main

to call another class call its main

No!! that is wrong. You don't call its main. The main is used only when you run the class, in order to start your program. If you want to call another class, call its constructor. Create a new instance and call its methods. In this case just instantiate the Frame you want to open and make it visible.

If you have two classes that extend the JFrame or even 2 JFrame instances whenever you create them and call the setVisible they are displayed. So when the button is clicked just create the frame you want to open.

Be a part of the DaniWeb community

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