473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.FlowLayout;
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("NewConnection");
JMenuItem recentconnection=new JMenuItem("RecentConnection");
JMenuItem closeconnection=new JMenuItem("CloseConnections");
JMenuItem exit=new JMenuItem("Exit");


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

JButton btn1=new JButton("Port1");

void run(){
frame.setJMenuBar(mb);
mb.add(file);
mb.add(edit);
mb.add(view);
mb.add(tools);
mb.add(help);
file.add(newconnection);
file.add(recentconnection);
file.add(closeconnection);
file.add(exit);
frame.add(btn1);

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


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setExtendedState(frame.getExtendedState()|JF rame.MAXIMIZED_BOTH);
frame.setLayout(new FlowLayout());
frame.setSize(400, 125);
frame.setVisible(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
Feb 8 '10 #1
2 4758
chaarmann
785 Expert 512MB
It's because when you click the button, the commands
Expand|Select|Wrap|Line Numbers
  1. ServerInitiator ser=new ServerInitiator();
  2. 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.
Feb 8 '10 #2
chaarmann
785 Expert 512MB
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:

@shashmita
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:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. frame.add(btn1);
  3. btn1.addActionListener(new ActionListener(){
  4.   JFrame frm=new JFrame("Server");
  5.   JDesktopPane desktop = new JDesktopPane();
  6.   int port=666;
  7.   ServerSocket sc;
  8.   Socket client;
  9.   public void actionPerformed(ActionEvent e) {
  10.   try {
  11.     sc = new ServerSocket(port);
  12.     drawGUI();
  13.     while(true){
  14.       client = sc.accept();
  15.       JOptionPane.showMessageDialog(frm,"New client Connected to the server");
  16.        new ClientHandler(client,desktop);
  17.        client = sc.accept();.
  18.     }
  19. ...
  20.  
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.Thread 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.
Feb 9 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
oll3i
by: oll3i | last post by:
package zad41; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.BorderFactory;
8
oll3i
by: oll3i | last post by:
package zad41; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.BorderFactory;
1
by: dishal | last post by:
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its...
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
2
by: KiranJyothi | last post by:
Hi All, I am trying to add buttons and I am not able to see them while running. Please let me know where I am going wrong. Thanks in advance. KiranJyothi <CODE> //KiranJyothi
4
by: HxRLxY | last post by:
I am having a compile-time problem with a simple program I am writing. When I attempt to compile, I get the error "non-static variable this cannot be referenced from a static context". The error...
7
by: HxRLxY | last post by:
I posted a different question (Help with non-static/static problem) which was answered. I changed my inner class to a static nested class, but now I cannot create an object using that class and add...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.