JCombobox Rendering Problem

M

Mkhululi.Tyukala

Hi All,

I am writing a swing application with the following functionality:
(1) There is a base class that supports things like changing the look
and feel – this is done via a JDialog that loads all the installed
lafs from which the user selects a choice.
(2) Enables the user to set/change the physical database properties –
location, password usernames and the like. -
The idea is that when the application starts up for the first time the
user will be asked to provide the database settings and then they will
be stored in a file and then every time the application starts the
settings will be loaded from this file. This works out fine!!!!!!!!!

Now here is the problem:
All the above is supported by an abstract class that extends JFrame
(I’ve put abstract after testing) - MySuperFrame. After testing, I
packaged this class together with other utility classes into a jar
file.
So my application which inherits from MySuperFrame fails on start-up
with an exception that the database settings file cannot be found –
And I know that it is in the jar file.

I think that my problem is the path and nothing but the path – or
relative path.+

This is the structure of my project:
There is the src and the bin directories and then there is an external
folder (not a package inside these two folders) called files (this is
where my database settings file is stored).
How do I solve this problem?
How do I make sure my application loads the settings file regardless
of where it is? I thought of passing the database settings file to a
constructor of a database settings dialog class. But I don’t know how
to sort out the path issue.

Please give me advice on how to implement that?
 
J

Jan Thomä

How do I make sure my application loads the settings file regardless
of where it is? I thought of passing the database settings file to a
constructor of a database settings dialog class. But I don’t know how
to sort out the path issue.


One possible approach might be to use the classloader for locating the
file. Eg. you have a jar with the following contents

- META-INF
- com
- yourproject
- dbsettings.properties


Put this jar file into the classpath of your application. Then you can
use the following code to look it up:


public static void main(String args[]) {
InputStream is =
MySuperFrame.class.getRessourceAsStream("/dbsettings.properties");
Properties p = new Properties();
p.load(is);

// now p contains all your settings
}

This should work regardless of the actual location of the properties
file, as long as it is in your classpath. You can put it inside a jar
or even put it plain into a folder, as long as you put that folder into
your classpath.

Hope that helps.

Best regards,
Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top