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 3847
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:
....
myTitleBarVaria ble = 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: ... myTitleBarVaria ble = 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 informatio n 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: ... myTitleBarVaria ble = 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.in stances++; // also tried -> instances++;
System.out.prin tln("CentralLim it.instances: " + CentralLimit.in stances); . . +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++
From one command line, I start it -> java CentralLimit and it says "CentralLimit.i nstances: 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.i nstances: 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*stat ic*int*instance s*=*0;
***//*main()*to*star t*things*====== =============== =============== ==
***public*stati c*void*main(Str ing[]*args)
***{
****CentralLimi t cl1 =*new*CentralLi mit();
CentralLimit cl2 = new CentralLimit();
***}
***//*constructor*== =============== =============== =============== =
***public*Centr alLimit()
***{
*****CentralLim it.instances++;
*****//*also*tried*->*instances++ ;
*****System.out .println("Centr alLimit.instanc es:*"*+*
CentralLimit.in stances);
***.
***.
+++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++
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 informati on 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 incremente d, 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: ... myTitleBarVaria ble = 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.in stances++; // also tried -> instances++;
System.out.prin tln("CentralLim it.instances: " + CentralLimit. instances); . . +++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++++
From one command line, I start it -> java CentralLimit and it says "CentralLimit.i nstances: 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.i nstances: 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.in stances++; // also tried -> instances++;
System.out.prin tln("CentralLim it.instances: " + CentralLimit.in stances); . . +++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++++++
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. "Decimal Number" sometimes serves to distinguish Base 10
numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal
Number" serves to differentiate a number from an integer. For the rest of this
post I shall only use either...
|
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 that applies to all these
events, but I need to have specific code execute when the form closes. The
properties for this method are sender (the originator) and e (event
arguments). I know how to get typeof (sender) to determine what form or...
|
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 after i send data into it?
I simply want to open socket, send data, close socket and have the server
just handle one client thread to recieve connection, recieve data, and close
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 will function as a windows service
and will also present a GUI to the user. I don't want "Interact with
desktop"; I want the same exe to run as a normal non-interactive service
when started properly by the service control manager, or instead to run...
|
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 wants a desktop
application made out of Internet technologies.
We're writing an application that has JavaScript enabled as a client
machine requirement. We need to support Windows with IE 5+ and
Netscape 7+ plus Mac with IE 5+ and Netscape 6.2+.
| |
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 using windows authentication.
On a few pages I get guest and then the user who is loged into the computer.
I am writing user information to my session and do not want it writen 8
times.
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |