3 Replies - 198 Views - Last Post: 03 April 2011 - 07:31 AM Rate Topic: -----

#1 jasperFernandes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-March 11

frame closes after executing one button

Posted 03 April 2011 - 01:04 AM

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
 
public class Main {
 
    private static void createAndShowGUI()  {
 
        JFrame frame1 = new JFrame("FINAL YEAR PROJECT VER 1.0");
        JTextArea test = new JTextArea(200, 200);
        frame1.setSize(500,500);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.add(test);
        
        FlowLayout experimentLayout = new FlowLayout();
        experimentLayout.setAlignment(FlowLayout.CENTER);
        frame1.setLayout(experimentLayout);
 
       
        
        JButton button = new JButton("Extract Links");
        JButton button1 = new JButton("Render To Text");
        JButton button2 = new JButton("Calculate Similarity");
        JButton button3 = new JButton("File Search");
        JButton button4 = new JButton("Exit");
        
         
        
        //Add action listener to button
        
        button.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                try {
					SimpleWebCrawler.main(null);
				
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });      
      //Add action listener to button 1
        button1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)
            {
        		try {
					RenderToText.main(null);
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
            }
        });
        
        button2.addActionListener(new ActionListener() {
        	 
            public void actionPerformed(ActionEvent e)
            {
                try {
				 readFile.main(null);
				
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });   
        
        button3.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e)
            {
                try {
					FileInput.main(null);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
        });   
        
        button4.addActionListener(new ActionListener() {
          	 
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });     
        
        
 
        frame1.getContentPane().add(button);
        frame1.getContentPane().add(button1);
        frame1.getContentPane().add(button2);
        frame1.getContentPane().add(button3);
        frame1.getContentPane().add(button4);
        frame1.getContentPane().add(test);
        
       
        frame1.setVisible(true);
    }
 
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


This is a code for creating a frame with 5 buttons. Each buttons has their own functions. The problem i facing now is , after i click the first button, the code runs for the first button, and after that the frame closes. How do i maintain the frame after the first button code run, allowing me to click other button after the first button run.

This post has been edited by jasperFernandes: 03 April 2011 - 01:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: frame closes after executing one button

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: frame closes after executing one button

Posted 03 April 2011 - 05:37 AM

First, why do you use a Thread to show the GUI? The Java-Program already runs in a thread, you don't need to create one yourself.

Second, what is this: SimpleWebCrawler.main(null);? Why don't you create an Object of the Class you want to use and then call the Methods?

After you changed that, try again.

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1984
  • View blog
  • Posts: 4,827
  • Joined: 10-September 10

Re: frame closes after executing one button

Posted 03 April 2011 - 05:49 AM

Your construction of the frame is different than I'm used to, but I think it's workable for some specific cases. I don't have enough experience with it to verify what all of those specific cases are, but it seems to work best when it calls console programs that provide the user with a graceful way to exit them. If your buttons call on another Swing program, say a simple "Hello World" JPanel, when that Hello World JPanel terminates, your Main() program will also terminate. It has something to do with the way Swing does threading.

I think it comes down to:

1. Type of program being called by your menu frame
2. How the program in (1) terminates

Don't give up. Before I can give more help short of a rewrite suggestion, I'd have to know more about the programs you're calling with the buttons in your menu frame. The legion of others smarter than me may be more help.
Was This Post Helpful? 0
  • +
  • -

#4 jasperFernandes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-March 11

Re: frame closes after executing one button

Posted 03 April 2011 - 07:31 AM

View PostGregBrannon, on 03 April 2011 - 05:49 AM, said:

Your construction of the frame is different than I'm used to, but I think it's workable for some specific cases. I don't have enough experience with it to verify what all of those specific cases are, but it seems to work best when it calls console programs that provide the user with a graceful way to exit them. If your buttons call on another Swing program, say a simple "Hello World" JPanel, when that Hello World JPanel terminates, your Main() program will also terminate. It has something to do with the way Swing does threading.

I think it comes down to:

1. Type of program being called by your menu frame
2. How the program in (1) terminates

Don't give up. Before I can give more help short of a rewrite suggestion, I'd have to know more about the programs you're calling with the buttons in your menu frame. The legion of others smarter than me may be more help.



Sir, thanks for your attention. I already solve it by changing my other class to extends JDialog. Voila, i can use it already. Thanks lot.

View PostMrLuke187, on 03 April 2011 - 05:37 AM, said:

First, why do you use a Thread to show the GUI? The Java-Program already runs in a thread, you don't need to create one yourself.

Second, what is this: SimpleWebCrawler.main(null);? Why don't you create an Object of the Class you want to use and then call the Methods?

After you changed that, try again.

Greetings: Luke



Sir, thank you for your attention. I already solve it by changing my other class to extends JDialog. Voila, i can use it already. Thanks lot. And have a good day.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1