Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(vari ables needed) and then after it runs (after you setVisible(true )) you can call something to get the information that was stored inside like this: fd.getDirectory ()...
So I tried something like that here: -
public static void main(String args[]) {
-
ArrayList<String> temp = new ArrayList<String>(2);
-
temp.add("First Tag\\");
-
temp.add("Second Tag\\");
-
final TagChoosingWindow TCW = new TagChoosingWindow(temp);
-
//Schedule a job for the event-dispatching thread:
-
//creating and showing this application's GUI.
-
javax.swing.SwingUtilities.invokeLater(new Runnable() {
-
public void run() {
-
TCW.createAndShowGUI();
-
}
-
});
-
Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
-
}
-
but this spits out a NullPointerExce ption error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...
So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.
What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.
Thanks for all your help
-blazed
7 2139 JosAH 11,448
Recognized Expert MVP
Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(vari ables needed) and then after it runs (after you setVisible(true )) you can call something to get the information that was stored inside like this: fd.getDirectory ()...
So I tried something like that here: -
public static void main(String args[]) {
-
ArrayList<String> temp = new ArrayList<String>(2);
-
temp.add("First Tag\\");
-
temp.add("Second Tag\\");
-
final TagChoosingWindow TCW = new TagChoosingWindow(temp);
-
//Schedule a job for the event-dispatching thread:
-
//creating and showing this application's GUI.
-
javax.swing.SwingUtilities.invokeLater(new Runnable() {
-
public void run() {
-
TCW.createAndShowGUI();
-
}
-
});
-
Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
-
}
-
but this spits out a NullPointerExce ption error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...
So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.
What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.
Thanks for all your help
-blazed
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.
kind regards,
Jos
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.
kind regards,
Jos
Unfortunately I'm still making TCW final which means it can not change right? Yet I can't access a non-final variable inside the run() area... ? I'll try using join() ... thank you.
-blazed
Alright, so my program has been modifed, I now have a class which extends thread that is as follows (very simple): -
import java.util.*;
-
-
class runTCW extends Thread {
-
TagChoosingWindow TCW;
-
public runTCW(ArrayList<String> al) {
-
TCW = new TagChoosingWindow(al);
-
}
-
-
public void run() {
-
TCW.createAndShowGUI();
-
}
-
-
public TagChoosingWindow getTCW() { return this.TCW; }
-
}
-
And my main thread now looks like this: -
public static void main(String args[]) {
-
ArrayList<String> temp = new ArrayList<String>(2);
-
temp.add("First Tag\\");
-
temp.add("Second Tag\\");
-
-
runTCW rTCW = new runTCW(temp);
-
rTCW.start();
-
try {
-
rTCW.join();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
Utilities.print(rTCW.getTCW().getNames());
-
}
-
It's STILL throwing a nullPointerExce ption on that line that says "Utilities.prin t(rTCW.getTCW() .getNames());"
I'm wondering why this could be... after all it makes sense that the thread has finished. It could be a certain line of code where I do frame.dispose()... but I thought that the variables not associated with the GUI would disappear, but not the arraylist I may have had stored somewhere.
Again thanks for the help,
-blazed
P.S.
The nullPointerExce ption is thrown before the thread finishes so that brings me to the conclusion that I'm closing the thread incorrectly or join() is not working! Excuse my lack of experience, I'm sure I'm doing something wrong...
I'm trying to look up examples of others using join() but it's not going well...
I tried to add a .isAlive() printed afterwards to see if the join() is working... well, obviously it is: -
public static void main(String args[]) {
-
ArrayList<String> temp = new ArrayList<String>(2);
-
temp.add("First Tag\\");
-
temp.add("Second Tag\\");
-
-
runTCW rTCW = new runTCW(temp);
-
rTCW.start();
-
try {
-
rTCW.join();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
System.out.println(rTCW.isAlive());
-
Utilities.print(rTCW.getTCW().getNames());
-
}
-
Prints the following: -
false
-
Exception in thread "main" java.lang.NullPointerException
-
at AsafUtil.Utilities.print(Utilities.java:139)
-
at TagChoosingWindow.main(TagChoosingWindow.java:116)
-
-
Process completed.
-
So clearly I'm doing something else wrong... It must have to do with my createAndShowGu i method right? Is there some way to cause the thread to keep running until the frame is closed?
Thanks for your help again,
-blazed
Alright, so I basically fixed my problem, but if someone who knows what they're doing reads this maybe you can tell me if there's a more conventional solution out there. This is what my createAndShowGU I() method now looks like: -
public void createAndShowGUI() {
-
//Create and set up the window.
-
frame = new JFrame("TableDemo");
-
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //Don't ask me why a space appears between the C and LOSE
-
frame.setContentPane(this);
-
-
//Display the window.
-
frame.pack();
-
frame.setVisible(true);
-
-
while(frame.isVisible()) { } //THIS IS THE LINE I ADDED
-
}
-
JosAH 11,448
Recognized Expert MVP
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.
kind regards,
Jos
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.
kind regards,
Jos
This is a much better solution... I have to finish this asap, but I will come back and make it a JDialog later...
Thank you for the help,
-blazed
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Vinod I |
last post by:
Hi Team,
I am having a string as "System.Data.SqlDbType.Int". Now I want to convert
this string type to actual type to use with my Command object Parameter
Creation. How I will convert this string to the type object ?.
Thanks in advance
|
by: Microsoft News Server |
last post by:
Hi,
I am currently having a problem with random, intermittent lock ups in my
ASP.net application on our production server (99% CPU usage by 3 threads,
indefinately). I currently use IIS Debug Tools to do a memory dump of the
app when the lock up occurs, however the stack information is not very
useful.
I have just put a new build of our system onto production, and this build is
a "Debug" build as opposed to a "Release" build. I am...
|
by: Michael Glass |
last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework,
and I have a serious problem with the page I'm currently working on. The page
has, among other things, two FormViews and a GridView control, each with its
own SqlDataSource. FormView1 talks to my Opportunity table and has an
ItemTemplate and an EditItemTemplate. FormView2 talks to my Activities table
and has an ItemTemplate, InsertItemTemplate and an...
|
by: Ankit Aneja |
last post by:
The code of classes given below is for server to which clients connect
i want to get ip address of client which has connected
pls help how can i get
//listen class
public class listen
{
|
by: Fred B |
last post by:
I am launching a new thread from my application's main process (using VB.net
2003), and I can't get the child to receive the parameter I'm attempting to
send it in a named data slot.
The code for launching the thread:
Dim NewThread As New Thread(AddressOf LaunchCommThread)
NewThread.AllocateNamedDataSlot("Offset")
NewThread.IsBackground = True
NewThread.Name = SIMclass.SIM(1).strCtrlDesignator
| |
by: Angus |
last post by:
Hello
I am doing this in a worker thread:
char* szPartial = new char;
if (szPartial)
{
lstrcpy(szPartial, szBuffer);
PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial);
}
|
by: JamesB |
last post by:
I am writing a service that monitors when a particular app is started.
Works, but I need to get the user who is currently logged in, and of course
Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM).
I understand that when the service starts, no user may be logged in, but
that's ok, as the app I am monitoring can only be run by a logged in user.
Do I need to use WMI to get the user context of Explorer.exe or is there a...
|
by: Tenowg |
last post by:
Hey everyone... Firstly let me tell you this is a project that is probably alittle over my head... I have played with programming most of my life, but I have only been doing VB 2005 for about 2 weeks. I know the concepts of most issues, but multi-threading seems to be giving me a headache... anyways here is what is happening...
I have use the MSDN code sample to create a Async TCP server (using sockets, and beginaccept, etc, not Tcplistener...
|
by: Sami Vaisanen |
last post by:
Hello group,
I'm writing a C++ based application that embeds the python engine. Now I
have a problem regarding exception/error information. Is there a way to
get the exception message and possibly the traceback into a string for
example? I've been eyeballing the PyErr_ module and it seems fairly
limited. In other words PyErr_Print() calls the right functions for
getting the exception information but unfortunately it is hardwired to...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |