[Q] Java Swing questions (beginner)

Discussion in 'Java' started by Eric G., Aug 23, 2003.

  1. Eric G.

    Eric G. Guest

    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?
    Eric G., Aug 23, 2003
    #1
    1. Advertising

  2. Eric G.

    Rex Guest

    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
    Rex, Aug 24, 2003
    #2
    1. Advertising

  3. Eric G.

    Eric Guest

    Rex <> wrote:

    > 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?
    Eric, Aug 24, 2003
    #3
  4. Eric G.

    David Segall Guest

    (Eric G.) wrote:

    >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.
    David Segall, Aug 24, 2003
    #4
  5. Eric G.

    Rex Guest

    Re: Custom Java Plugins (was Re: [Q] Java Swing questions (beginner))


    >
    > 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.
    > > Another way is-to instantiate the 'plugin' class at runtime, using
    > > Class.forName. You could store the name of the plugin class in a text

    file
    > > or something, and create it at runtime using the static method
    > > Class.forName(String classname)

    >
    > 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.


    > 8. If the method I am invoking expects either no parameters or just a
    > single parameter, what would a call to invoke look like?
    >
    > I think this covers everything.
    >
    >
    Rex, Aug 24, 2003
    #5
  6. Eric G.

    Eric Guest

    Re: Custom Java Plugins

    Rex <> wrote:

    > > 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.


    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...?

    > > 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.


    Ok. So, could I then write it as:

    Winner theWinner;

    theWinner = ballotMethodOne.FindWinner( someVoterPreferences );

    Am I correct?
    Eric, Aug 24, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    488
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    660
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,750
    lizard
    Jan 30, 2006
  4. S.T
    Replies:
    2
    Views:
    564
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,722
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page