Problem of a Jbutton actionlistener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashmita
    New Member
    • Jan 2010
    • 9

    Problem of a Jbutton actionlistener

    Please help me

    i have a problem that whenever i run this program and during execution if i click the button then the remainig menubar and menuitems will not respond all the actions are blocked


    package desktop;

    import java.awt.FlowLa yout;
    import java.awt.event. ActionEvent;
    import java.awt.event. ActionListener;
    import javax.swing.*;


    public class SamplDesktop {

    JFrame frame = new JFrame();
    JMenuBar mb = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMenu view = new JMenu("View");
    JMenu tools = new JMenu("Tools");
    JMenu help = new JMenu("Help");

    JMenuItem newconnection = new JMenuItem("NewC onnection");
    JMenuItem recentconnectio n=new JMenuItem("Rece ntConnection");
    JMenuItem closeconnection =new JMenuItem("Clos eConnections");
    JMenuItem exit=new JMenuItem("Exit ");


    JFrame frame1 = new JFrame();
    JDesktopPane desktop = new JDesktopPane();

    JButton btn1=new JButton("Port1" );

    void run(){
    frame.setJMenuB ar(mb);
    mb.add(file);
    mb.add(edit);
    mb.add(view);
    mb.add(tools);
    mb.add(help);
    file.add(newcon nection);
    file.add(recent connection);
    file.add(closec onnection);
    file.add(exit);
    frame.add(btn1) ;

    btn1.addActionL istener(new ActionListener( ){
    public void actionPerformed (ActionEvent e) {
    ServerInitiator ser=new ServerInitiator ();
    ser.initialize( 6666);
    }
    });


    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.setExtend edState(frame.g etExtendedState ()|JFrame.MAXIM IZED_BOTH);
    frame.setLayout (new FlowLayout());
    frame.setSize(4 00, 125);
    frame.setVisibl e(true);
    }

    public static void main(String[] args) {
    SamplDesktop sd=new SamplDesktop();
    sd.run();
    }


    }

    i must access the object and the methods of the other class please help for me from this problem also

    Thanks in Advance
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    It's because when you click the button, the commands
    Code:
    ServerInitiator ser=new ServerInitiator();
    ser.initialize(6666);
    inside your ActionListener method are called.
    So if these commands take a long time or are crashing, the program can't go on and draw the menu or respond to more mouseclicks from you.
    You can avoid that by putting these commands inside a thread and then run the thread when the button is clicked.

    666, the number of the beast! And 6666, an even bigger one.
    No wonder the devil doesn't let it work.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      Shashmita send me a private email instead of replying here.
      This is not a good idea, see forum guidelines. There is no benefit for others that have the same problem if I reply her privately back and I can't see anything private in her email, so I am posting it here:

      Originally posted by shashmita
      thanks alot sir but i have been using the already existing program which is in thread compiling without any problem.
      my main problem araises when i was suppose to keep the existing program into GUI commands and by clicking the button r if i call the object of serverinitiator program into the tabbedpane or in the button it is blocking remaining components
      now i wil send the total program please help me sir.
      Thanks in Advance
      She has sent me a huge, unindented program that took me a long time to scan, but only a small part is important, which I will quote here:
      Code:
      ...
      frame.add(btn1);
      btn1.addActionListener(new ActionListener(){
        JFrame frm=new JFrame("Server");
        JDesktopPane desktop = new JDesktopPane();
        int port=666;
        ServerSocket sc;
        Socket client;
        public void actionPerformed(ActionEvent e) {
        try {
          sc = new ServerSocket(port);
          drawGUI();
          while(true){
            client = sc.accept();
            JOptionPane.showMessageDialog(frm,"New client Connected to the server");
             new ClientHandler(client,desktop);
             client = sc.accept();.
          }
      ...
      First, Shashmita, your main program is running as a thread or process, that's right. But that's not what I ment. When you click on the button the method actionPerformed is called, but instead of executing your code there, you should open a new thread there and start it (see java.lang.Threa d how to create a thread). This thread then will run your code. That means your program (main thread) and your child thread (the code inside actionPerformed ) will run in parallel. In this way, it doesn't matter if the code in your child thread will run only seconds, hours or forever. Because the main thread doesn't need to wait until your child thread finishes and can draw the GUI-elements right away. No blocking anymore.

      In the code above you sent me, when you click a button you are entering a while-loop that runs forever: "while (true)". It is only stopped if an error happens. So you can't see your GUI drawn until an error happens. Worse, if it runs without errors, you will wait forever, all "remaining componentes" are blocked.
      I/O-operations, like network connections, can take a long time. There is hardware involved, firewalls, routers, shared throughput etc., so that you can never be sure that your request will only take milliseconds. I mean, it can usually take milliseconds to run, but you can not rely on it. But under some circumstances it will run for hours! That's why it is better to open a new thread and perform the I/O-operation there. So that your main program will not be blocked under any circumstances.

      Comment

      Working...