[Q] Java Swing questions (beginner)

E

Eric G.

Just starting to teach myself a bit a Java and had a couple of
questions.

As I understand it, a Java Swing application is based around the JFrame
class.

Is there always a window associated with a JFrame?

A design that I would like to use is to have the main part of the
application just be a menu bar which would then create any windows
needed. This kind of design seems to make sense when you want tool
palettes which are global to the application. Is this kind of design
possible with JFrame? Any recommended sample code demonstrating this?


Also, it is possible to write a Java Swing application which can then
communicate with an external code fragment (i.e. a plugin)? Any sample
code demonstrating a simple application that communicates with a plugin
designed just for it?
 
R

Rex

From your description, I gather you want to make an MDI (multiple document
interface) application, perhaps like Acrobat Reader, which lets u open
multiple pdfs within the same app window.
I've made a similar application in the past-
Add a JDesktopPane to a JFrame (use borderlayout on the jframe and put the
desktop pane in the center)
JDesktopPane is a subclass of Window, and acts as a container for
JInternalFrames, which are used as child windows.
I dont have the code here-but you could look at Sun's Swing tutorial online
for code fragments. Roughly-u do the following.
1. Create a JFrame for your application
2.(optional, but recommended) Set it to use BorderLayout
3.Add a JDesktopPane at the center
4. Set the DesktopPane's layout to null (so that you can move windows
around)
5. Instantiate any JInternalFrames or subclasses and add/remove as
necessary. JDesktopPane provides all methods to enumerate windows, and add
/remove them.

I couldn't understand what you meant by a 'plugin'. Do you mean a browser
plugin? Or external platform specific native code?
Since you said Swing *application*-I suppose browser plugins are ruled
out..it would be relevant if you said u wanted to work with Swing applets.
If you want to use native code, such as a DLL for Windows, there are methods
available in the core API for invoking methods from these.You would also
have to set SecurityManager permissions for this, i think.

Hope this helps

Rex
 
E

Eric

Rex said:
I couldn't understand what you meant by a 'plugin'. Do you mean a browser
plugin? Or external platform specific native code?
Since you said Swing *application*-I suppose browser plugins are ruled
out..it would be relevant if you said u wanted to work with Swing applets.
If you want to use native code, such as a DLL for Windows, there are methods
available in the core API for invoking methods from these.You would also
have to set SecurityManager permissions for this, i think.

Sorry, I was less then clear here.

I want my own Java Swing Application which has it's own custom plugins.

If this still isn't clear, the specific application I have in mind
involves election methods.

Based on a group of ranked ballots, there are many methods to determine
the winner.

I would like to seperate these method from my application and place them
into a plugin.

The obvious benefits here are that if I wanted to write a new method, I
would not have to modify the application itself and it would be possible
to allow others to develop a plugin implementing their own method.

Is this possible? Is there a tutorial on this or some sample code?
 
D

David Segall

Just starting to teach myself a bit a Java and had a couple of
questions.

As I understand it, a Java Swing application is based around the JFrame
class.

Is there always a window associated with a JFrame?

A design that I would like to use is to have the main part of the
application just be a menu bar which would then create any windows
needed. This kind of design seems to make sense when you want tool
palettes which are global to the application. Is this kind of design
possible with JFrame? Any recommended sample code demonstrating this?


Also, it is possible to write a Java Swing application which can then
communicate with an external code fragment (i.e. a plugin)? Any sample
code demonstrating a simple application that communicates with a plugin
designed just for it?
Have a look at the free, open source, NetBeans (www.netbeans.org).
NetBeans is both a sophisticated development environment for Java and
a set of JavaBeans which you can use to develop your own application.
I am sure that you will find it an excellent tool for developing
JFrame based applications even if the NetBeans format does not suit
your application. The NetBeans site contains links to some
applications which were developed using the NetBeans source beans. If
you want to pursue the topic I found "NetBeans, The Definitive Guide"
(http://www.oreilly.com/catalog/netbeans/) an excellent reference
although you should check the reviews at www.amazon.com before you buy
it.
 
R

Rex

Assuming I understand this approach, would I be correct in saying that
only one plugin could be accessed at a time?
You can load any number of them into a data structure by storing Ballot
references in it.
This would seem to be the way to go as it would appear to allow me to
load multiple 'Ballot' classes at the same time so I could pass the same
set of ballots to the FindWinner method and see the different winners
chosen by each method.

I would likely implement in such a way that each of these plugins uses a
standard naming convention so I can use the Java file directory access
functions to dynamically build the list of plugins to load.
JDK 1.4 and above have classes for storing configuration settings in text
files..you could use those.
However, I am still uncertain about some details on how to make this
work. If anyone knows of a tutorial or some basic sample code showing
how to accomplish this, it would likely answer my questions.

So, let's say that my Ballot class looks something like this:

I have modified your code to reflect things a bit better:
Ballot is an interface, not a class. It's like a C++ pure virtual class.
Basically, if you're familiar with C++, this is the Java way of doing what
you would have done with virtual methods in C++.
public interface Ballot
{
public Ballot() { /* initialization code */ }
public Winner FindWinner( VoterPreference[] ) { /* compute winner */ }
public PrintResults() { /* print results */ }
}

In Java, you implement an interface, and extend a class
public class IRV implements Ballot
{
/* OT editorial comment...IRV...thppt */
public IRV() { /* initialization code */ }
public Winner FindWinner( VoterPreference[] ) { /* compute winner */ }
public PrintResults() { /* print results */ }
}
public class CondorcetRP implements Ballot
{
/* OT editorial comment...CondorcetRP...woo hoo */
public CondorcetRP() { /* initialization code */ }
public Winner FindWinner( VoterPreference[] ) { /* compute winner */ }
public PrintResults() { /* print results */ }
}

Apart from the syntactical glitches, yes, you've got the hang of it, this is
exactly what i had in mind.

1. Are these stored in .java (i.e. as source) or
.jar (i.e. compiled) files?
You would write a separate source file for each class (henceforth i refer to
all plugins as classes, cos thats what they are)
A jar file is something different-it is a compressed file(in ZIP format
actually) containing classes used to distribute applications that are
composed of multiple classes.
You can simply create a zip file and change the extension to jar.
2. Do I need to define a main for each plugin?
If so, what would the main look like?
You need a main only if you plan to run each class on its own. There is
nothing unique about the main method-its like it would be in any other java
program -
public static void main(String args[])
{
}

3. In C++, I can force, for example, CondorcetRP to define
the FindWinner and PrintResults methods. Is this possible
with Java?
The moment class A implements interface B, it HAS to define implementations
for all the methods defined in the interface, or it won't compile.
So it's downright compulsory, not just possible!
In, my application, I could then have something like:

public void runElection(Ballot someballot)
{
/* would write this using an array, but don't know that syntax yet */
Class ballotMethodOne = Class.forName( "CondorcetRP" );
Class ballotMethodTwo = Class.forName( "IRV" );
Yes, this is exactly the syntax!
// ....
}

4. How can I access the FindWinner method which is somewhere in
ballotMethodOne?

The documentation I've found is a bit unclear on this point. It would
appear that I can do something like

Method findWinnerMethod;

findWinnerMethod = ballotMethodOne.getMethod( "Findwinner", /*???*/ );
This is called introspection-and is needed only if you dont know what
methods a class contains. However, in this case you do, cos u have already
defined these methods in Ballot, and all these classes implement Ballot.
In C++ terminology,
when u have a pointer to a base class that has virtual methods-you can call
the virtual method like base->method() without worrying about which child
class is pointed to by the base class pointer.
Same way-in Java-when u have a reference to an interface
(reference:pointer::java:c++) , in this case Ballot, you can call the
methods defined in Ballot without worrying abt the implementing class that
the reference actually refers to.
5. How do I construct the second method that getMethod needs?

The documentation I have is also a bit unclear on how to invoke the
method once I have it. Part of the answer would appear to be:

Winner theWinner;

theWinner = findWinnerMethod.invoke( /*???*/, /*???*/ );

6. Is the first parameter to invoke always the first parameter to the
method I am invoking?

7. Is the second parameter to invoke always an array of parameters to
be passed to the method I am invoking, assuming the method I am
invoking is expecting more then a single parameter to be passed to
it?

I've lost you at this point. Seems like u want to invoke methods on a
totally unknown class that you have instantiated. As i said before-if it
subclasses or implements any of your existing classes/interfaces, you
already know which methods to invoke. If it defines methods of its own,
which are not inherited, then you need to use introspection (there is a
package, i think its called java.lang.introspection) to do this. I tried it
out long ago-its easy. You can easily construct method signatures, find out
their parameters and types etc.
 
E

Eric

Rex said:
JDK 1.4 and above have classes for storing configuration settings in
text files..you could use those.

Not sure I understand what you are talking about here...
2. Do I need to define a main for each plugin?
If so, what would the main look like?
You need a main only if you plan to run each class on its own. There is
nothing unique about the main method-its like it would be in any other java
program -
public static void main(String args[])
{
}

I see. So, if I understand you correctly, I could include a main or not
and it would not make any difference in my case.

A main function, perhaps, would be useful if I wanted to be able to run
tests on the class apart from everything else...?
Yes, this is exactly the syntax!
This is called introspection-and is needed only if you dont know what
methods a class contains. However, in this case you do, cos u have already
defined these methods in Ballot, and all these classes implement Ballot.
In C++ terminology,
when u have a pointer to a base class that has virtual methods-you can call
the virtual method like base->method() without worrying about which child
class is pointed to by the base class pointer.
Same way-in Java-when u have a reference to an interface
(reference:pointer::java:c++) , in this case Ballot, you can call the
methods defined in Ballot without worrying abt the implementing class that
the reference actually refers to.

Ok. So, could I then write it as:

Winner theWinner;

theWinner = ballotMethodOne.FindWinner( someVoterPreferences );

Am I correct?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top