I have a Java desktop GUI application that the user can run multiple
times. In order to keep one instance of the application distinct from
another, I'd like to put the instance number of the application in the
title bar.
For example, the user starts the application, and the title bar says
"Control Panel". The user starts another instance of the application,
and it knows that one instance is already running, so the title bar of
the second instance says "Control Panel - 2nd" or something like that.
Is there some way that static methods and variables can keep track of
how many times the class has been instantiated, so that I could put that
information in the title bar?
I know I could create a little data file to do that, but hopefully there
is something simpler.
I do not need to keep track of when any instances stop.
Thanks,
Mark 5 3813
Mark Fisher wrote: I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the application in the title bar.
For example, the user starts the application, and the title bar says "Control Panel". The user starts another instance of the application, and it knows that one instance is already running, so the title bar of the second instance says "Control Panel - 2nd" or something like that.
Is there some way that static methods and variables can keep track of how many times the class has been instantiated, so that I could put that information in the title bar?
I know I could create a little data file to do that, but hopefully there is something simpler.
I do not need to keep track of when any instances stop.
Mark, you guessed the solution already. Just use a static variable to keep
track of the number of instances you have created. You can think of a
static variable as belonging to a class, rather than being associated with
a single instance of the class. Just have a look a this simple example and
it will help you solve your problem.
class Myclass {
static int count = 0;
Myclass() {
count++}
}
The variable count is static, meaning there is only one count, no matter how
many instances of Myclass you may have created. When the class is loaded,
the static variable is initialised at 0. When you create an instance of
Myclass, the constructor is called and the static variable count is
incremented, allowing you to keep track of the number of instances that
have been created. You can reference this static variable from your
instance using the class name, i.e. something like:
....
myTitleBarVariable = MyClass.count;
.....
Hope this helps,
Brgds
--
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Visit us : http://users.skynet.be/herman.timmermans
Herman Timmermans wrote: Mark Fisher wrote:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the application in the title bar.
For example, the user starts the application, and the title bar says "Control Panel". The user starts another instance of the application, and it knows that one instance is already running, so the title bar of the second instance says "Control Panel - 2nd" or something like that.
Is there some way that static methods and variables can keep track of how many times the class has been instantiated, so that I could put that information in the title bar?
I know I could create a little data file to do that, but hopefully there is something simpler.
I do not need to keep track of when any instances stop.
Mark, you guessed the solution already. Just use a static variable to keep track of the number of instances you have created. You can think of a static variable as belonging to a class, rather than being associated with a single instance of the class. Just have a look a this simple example and it will help you solve your problem.
class Myclass { static int count = 0; Myclass() { count++} }
The variable count is static, meaning there is only one count, no matter how many instances of Myclass you may have created. When the class is loaded, the static variable is initialised at 0. When you create an instance of Myclass, the constructor is called and the static variable count is incremented, allowing you to keep track of the number of instances that have been created. You can reference this static variable from your instance using the class name, i.e. something like: ... myTitleBarVariable = MyClass.count; ....
Hope this helps, Brgds
Thanks for your reply! However, things do not quite work as I had hoped.
Here's the actual relevant code (running Java 1.5.0):
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
You want inter-application API's!
Static fields are for inter-instance commumication.
--
Regards,
Casey
Mark Fisher wrote:
Herman Timmermans wrote: Mark Fisher wrote:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the application in the title bar.
For example, the user starts the application, and the title bar says "Control Panel". The user starts another instance of the application, and it knows that one instance is already running, so the title bar of the second instance says "Control Panel - 2nd" or something like that.
Is there some way that static methods and variables can keep track of how many times the class has been instantiated, so that I could put that information in the title bar?
I know I could create a little data file to do that, but hopefully there is something simpler.
I do not need to keep track of when any instances stop.
Mark, you guessed the solution already. Just use a static variable to keep track of the number of instances you have created. You can think of a static variable as belonging to a class, rather than being associated with a single instance of the class. Just have a look a this simple example and it will help you solve your problem.
class Myclass { static int count = 0; Myclass() { count++} }
The variable count is static, meaning there is only one count, no matter how many instances of Myclass you may have created. When the class is loaded, the static variable is initialised at 0. When you create an instance of Myclass, the constructor is called and the static variable count is incremented, allowing you to keep track of the number of instances that have been created. You can reference this static variable from your instance using the class name, i.e. something like: ... myTitleBarVariable = MyClass.count; ....
Hope this helps, Brgds
Thanks for your reply! However, things do not quite work as I had hoped.
Here's the actual relevant code (running Java 1.5.0):
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++ . . // Keep track of number of times started private static int instances = 0;
// main() to start things ====================================== public static void main(String[] args) { new CentralLimit(); }
// constructor ================================================ public CentralLimit() {
CentralLimit.instances++; // also tried -> instances++;
System.out.println("CentralLimit.instances: " + CentralLimit.instances); . . ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
From one command line, I start it -> java CentralLimit and it says "CentralLimit.instances: 1". From another command line, I start a second instance (java CentralLimit), while the first GUI is still on display, and the reply is again "CentralLimit.instances: 1".
Any ideas?
Regards,
Mark
Mark, I 'm sorry, but I think I was a little to fast in replying. I was
referring to intra-application instances of classes, what you are looking
for is some form of inter-application ...
If you change your example like this :
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
***.
***.
***//*Keep*track*of*number*of*times*started
***private*static*int*instances*=*0;
***//*main()*to*start*things*========================== ============
***public*static*void*main(String[]*args)
***{
****CentralLimit cl1 =*new*CentralLimit();
CentralLimit cl2 = new CentralLimit();
***}
***//*constructor*===================================== ===========
***public*CentralLimit()
***{
*****CentralLimit.instances++;
*****//*also*tried*->*instances++;
*****System.out.println("CentralLimit.instances:*" *+*
CentralLimit.instances);
***.
***.
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
you'll see that instances = 2;
When it comes to counting how many times your application has been started,
I would suggest using a file containing an instance count. This file needs
to be created with the first start of the application and the variable
initialized to 1.
Subsequent application instances should check for 1) the file and 2)
increment the instance count. Upon termination, the application 1) reads
the file and 2) decrements the instance count, when the instance count
reaches 0 the application should delete the file before termination.
Brgds,
Herman
------------------------------------------------------------------
Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb
Anti Spam = remove the "dot" and the "at"
Registered Linux User #264690
Visit us : http://users.skynet.be/herman.timmermans
Herman Timmermans wrote: Mark Fisher wrote:
Herman Timmermans wrote:
Mark Fisher wrote: I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the application in the title bar.
For example, the user starts the application, and the title bar says "Control Panel". The user starts another instance of the application, and it knows that one instance is already running, so the title bar of the second instance says "Control Panel - 2nd" or something like that.
Is there some way that static methods and variables can keep track of how many times the class has been instantiated, so that I could put that information in the title bar?
I know I could create a little data file to do that, but hopefully there is something simpler.
I do not need to keep track of when any instances stop.
Mark, you guessed the solution already. Just use a static variable to keep track of the number of instances you have created. You can think of a static variable as belonging to a class, rather than being associated with a single instance of the class. Just have a look a this simple example and it will help you solve your problem.
class Myclass { static int count = 0; Myclass() { count++} }
The variable count is static, meaning there is only one count, no matter how many instances of Myclass you may have created. When the class is loaded, the static variable is initialised at 0. When you create an instance of Myclass, the constructor is called and the static variable count is incremented, allowing you to keep track of the number of instances that have been created. You can reference this static variable from your instance using the class name, i.e. something like: ... myTitleBarVariable = MyClass.count; ....
Hope this helps, Brgds
Thanks for your reply! However, things do not quite work as I had hoped.
Here's the actual relevant code (running Java 1.5.0):
++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++ . . // Keep track of number of times started private static int instances = 0;
// main() to start things ====================================== public static void main(String[] args) { new CentralLimit(); }
// constructor ================================================ public CentralLimit() {
CentralLimit.instances++; // also tried -> instances++;
System.out.println("CentralLimit.instances: " + CentralLimit.instances); . . ++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++
From one command line, I start it -> java CentralLimit and it says "CentralLimit.instances: 1". From another command line, I start a second instance (java CentralLimit), while the first GUI is still on display, and the reply is again "CentralLimit.instances: 1".
Any ideas?
Regards,
Mark
Mark, I 'm sorry, but I think I was a little to fast in replying. I was referring to intra-application instances of classes, what you are looking for is some form of inter-application ... If you change your example like this : ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++ . . // Keep track of number of times started private static int instances = 0;
// main() to start things ====================================== public static void main(String[] args) { CentralLimit cl1 = new CentralLimit(); CentralLimit cl2 = new CentralLimit(); }
// constructor ================================================ public CentralLimit() {
CentralLimit.instances++; // also tried -> instances++;
System.out.println("CentralLimit.instances: " + CentralLimit.instances); . . ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
you'll see that instances = 2;
When it comes to counting how many times your application has been started, I would suggest using a file containing an instance count. This file needs to be created with the first start of the application and the variable initialized to 1. Subsequent application instances should check for 1) the file and 2) increment the instance count. Upon termination, the application 1) reads the file and 2) decrements the instance count, when the instance count reaches 0 the application should delete the file before termination.
Brgds, Herman ------------------------------------------------------------------ Suse Linux Professional 8.1 on Athlon 1.1 Ghz 512 Mb Anti Spam = remove the "dot" and the "at" Registered Linux User #264690 Visit us : http://users.skynet.be/herman.timmermans
xmmmm
this id the wrong way ....
do you have heard the Singleton Patern ?
use singleton patern and make a counter class ..
this class initialize at the first time tha you will bi start the
aplication ...
at the second time you run the aplication and this go to initialize
again the VM see tha have already loaded this class and dont Initialize
again ...
so you Have a counter tha lives in VM ....
;)
Giorgos Maravelias
Java Certified Programmer This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Bentley |
last post by:
John Bentley:
INTRO
The phrase "decimal number" within a programming context is ambiguous. It could
refer to the decimal datatype or the related but separate concept of a generic
decimal number....
|
by: Christopher W. Douglas |
last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a
method that handles several events, such as closing a form and changing the
visible status of a form. I have some code...
|
by: Daniel |
last post by:
after opening socket, sending data then closing socket 3000 times i get
"Only one usage of each socket address"
what am i doing wrong? is there some thing else i need to do to free up the
socket...
|
by: Ross Presser |
last post by:
OK, I've been researching this problem and can't find a definitive answer
yet.
The situation is one that seems to have come up a few times to different
folks. I am writing an application that...
|
by: msa |
last post by:
Hi there,
First off, let me say that I know that launching to full screen is a
bad idea. I would never do it given the choice, but I must follow
orders from my boss, the boss that desparately...
|
by: Mike |
last post by:
Is there a way to determine what a user's default email client is? I
read a post from 3 years ago that said no. I guess I'm hoping
something has come along since then.
|
by: adm |
last post by:
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.
|
by: Curious George |
last post by:
I have a frameset with multiple aspx pages whithin it.
I lauch the page and notice that it triggers many session_start events. In
fact I knotice multiple events for the same page.
I am also...
|
by: Tom |
last post by:
How can one determine if a .NET program is running in the developer IDE or
running stand-alone? I am sure I saw something somewhere but I don't
remember where or what.
Thanks!
Tom
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |