Question about method calling (Java 1.5.0_05)

Imagine for example the javax.swing.border.Border hierarchy.
I'm writing a BorderEditor for some gui builder, so I need a function that takes a Border instance and returns the Java code. Here is my first try:
1 protected String getJavaInitializationString() {
2     Border border = (Border)getValue();
3     if (border == null)
4         return "null";
5
6     return getCode(border);
7 }
8
9 private String getCode(BevelBorder border) {...}
10 private String getCode(EmptyBorder border) {...}
11 private String getCode(EtchedBorder border) {...}
12 private String getCode(LineBorder border) {...}
13
14 private String getCode(Border border) {
15     throw new IllegalArgumentException("Unknown border class " + border.getClass());
16 }This piece of code fails. Because no matter of what class is border in line 6, this call always ends in String getCode(Border border).
So I replaced line 6 with:
6     return getCode(border.getClass().cast(border));But with the same result. So, I try with the asSubClass() method:
6     return getCode(Border.class.asSubClass(border.getClass()).cast(border));And the same result again! Then i try putting a concrete instance of some border, say BevelBorder:
6     return getCode(BevelBorder.class.cast(border));Guess what! It worked! But this is like:
6     return getCode((BevelBorder)border);And I don't want that! I want dynamic cast and correct method calling.
After all tests, I give up and put the old trusty and nasty if..else if... else chain.
Too bad! I'm doing some thing wrong?
Thank in advance
Quique.-
PS: Sorry about my english! it's not very good! Escribo mejor en espa�ol!

Hi, your spanish is quite good!
getCode(...) returns the Java code for the given border.
So getCode(BevelBorder border) returns a Java string that is something like this "new BevelBorder()".
I want Java to resolve the method to call.
For example: A1, A2 and A3, extends A.
public void m(A1 a) {...}
public void m(A2 a) {...}
public void m(A3 a) {...}
public void m(A a) {...}
public void p() {
    A a = (A)getValue();
    // At this point 'a' could be instance of A1, A2 or A3.
    m(a); // I want this method call, to call the right method.
}This did not work. So, i've used instead of m(a):
    m(a.getClass().cast(a));Didn't work either. Then:
    m(A.class.asSubClass(a.getClass()).cast(a));No luck! But:
    m(A1.class.cast(a)); // idem m((A1)a);Woks for A1!
I don't know why m(A1.class.cast(a)) works and m(a.getClass().cast(a)) doesn't!
thanks for replying!
Quique

Similar Messages

  • A question about design of Java Application on java card.

    Hi every one,
    I have a question about design of Java applet on java card,
    There is an application on my card that it is not java card, it include a DF and some EF under DF, I would like to know if I want to have this application on java card ,how can I design DF and EF on java card ,I mean is there any relation between DF,EF with applet and file? for example can I say DF is same as applet and EF is same as file?
    I'll appreciate for any help .
    yours sincerely,
    orchid

    hi
    you can write a java class for DF.
    an instance of DF class have some EF objects.
    for example,
    samples/com/sun/javacard/samples/JavaPurse/CyclicFile.java
    implements ISO 7816 CyclicFile ( EF )
    see also:
    http://forum.java.sun.com/thread.jspa?threadID=673745&messageID=3936272
    best regards,
    siavash

  • Question about the new JAVA Update J2SE 5.0 Release 3

    Here is some info about this update:
    Java 2 Platform, Standard Edition (J2SE) 5.0 Release 3 allows applications and applets developed for the J2SE 5.0 platform to run on Mac OS X v 10.4.2 and later.
    This update does NOT CHANGE the DEFAULT VERSION of Java on your Mac from Java 1.4.2 to J2SE 5.0, though Java applications that require J2SE 5.0 may specifically request it. You can change the preferred Java version for applications and applets by using the new Java Preferences utility. This utility is installed by the J2SE 5.0 update at /Applications/Utilities/Java/J2SE 5.0/.
    http://docs.info.apple.com/article.html?artnum=302412
    HERE IS MY QUESTION---
    This update makes me nervous cause I don't know much about Java. IF I change my default version to this new 5.0 will it mean that some of the older Java stuff on the web won't work? Should I just download this and ignore it? What should I do about this update?
    Thanks for the help!
    Renée

    I too seem to be suffering issues with loading web pages after the 10.4.3/Java 5.0 update. I have reinstalled both updates, changed the java settings, emptied all the caches, and tried various combinations thereof, and I still have problems with web pages loaded. most of the problems appear to be related to rendering frames and images - In fact a couple of web sites I navigate to (upon looking at the source for the page" state "It appears your browser does not support frames." I think this is something broken in the latest release. Unfortunately, I cannot reinstall the latest copy of Safari, because the installer states "This version of Safari cannot be installed. It requires version 10.4.2." I guess I am in a pickle until Apple comes out with another update. BTW I have some pages that Safari will not render but IE running on the same box will. I also noticed that the problem propagated to other applications that use Java....For instance the Dashboard widget from google (Google Maps) no longer renders the tiled (framed?) map pieces...instead you get the ubiquitous "?" icon.

  • Question about privacy in Java

    hi, i have a question about privacy practices in Java. I always thought it was bad form to program with too many global variable. However, when I program in Java i find that most of the time I am making my variables global. Is the fact that they are in a class supposed to make up for that fact? With subclasses you can also have 3rd parties working on your data. It seems a bit suspicious to me, like the data should be maintained more strictly, or is it my job to do that? I am just curious, but it seems java promotes global variables, which I find to be bad programming practice since anything can operate on them. I appreciate any input...

    However, when I program in Java
    i find that most of the time I am making my variables
    global. (meaning public)Sounds like you are writing bad code. Public variables should be extrememly rare except static final 'constants'.
    Is the fact that they are in a class supposed
    to make up for that fact? Absolutely not.
    With subclasses you can also
    have 3rd parties working on your data. It seems a bit
    suspicious to me, like the data should be maintained
    more strictly, or is it my job to do that? Yes, it's your job.
    I am just
    curious, but it seems java promotes global variables,
    which I find to be bad programming practice since
    anything can operate on them. I appreciate any
    input...Java does not promote public variables. You have to explicitly make them public.

  • Question about methods in a class that implements Runnable

    I have a class that contains methods that are called by other classes. I wanted it to run in its own thread (to free up the SWT GUI thread because it appeared to be blocking the GUI thread). So, I had it implement Runnable, made a run method that just waits for the thread to be stopped:
    while (StopTheThread == false)
    try
    Thread.sleep(10);
    catch (InterruptedException e)
    //System.out.println("here");
    (the thread is started in the class constructor)
    I assumed that the other methods in this class would be running in the thread and would thus not block when called, but it appears the SWT GUI thread is still blocked. Is my assumption wrong?

    powerdroid wrote:
    Oh, excellent. Thank you for this explanation. So, if the run method calls any other method in the class, those are run in the new thread, but any time a method is called from another class, it runs on the calling class' thread. Correct?Yes.
    This will work fine, in that I can have the run method do all the necessary calling of the other methods, but how can I get return values back to the original (to know the results of the process run in the new thread)?Easy: use higher-level classes than thread. Specifically those found in java.util.concurrent:
    public class MyCallable implements Callable<Foo> {
      public Foo call() {
        return SomeClass.doExpensiveCalculation();
    ExecutorService executor = Executors.newFixedThreadPool();
    Future<Foo> future = executor.submit(new MyCallable());
    // do some other stuff
    Foo result = future.get(); // get will wait until MyCallable is finished or return the value immediately when it is already done.

  • Questions About Printing in Java

    Alright, I've been looking at the Java printing API. I like it, it's nice. I've got a drag-and-drop workspace that the user wants to print, so I just use m_workspace.paint(Graphics) on the print Graphics object to paint the workspace onto the page. Very nice.
    Here's the issue. I want to fit as many workspaces on a page as is possible. Now, with a Graphics2D object, I can call a couple of methods to get its height and width. I need to be able to determine the height and width of the Graphics object that the printer hands me. Here are the questions as best as I can describe them:
    * Is it possible to find the height and width of a Graphics object? How else can you determine a Graphics object's coordinate range?
    * When the Printable interface is given a Graphics object on which to print, does the Graphcis object represent the whole page or just the printable area of the page?
    * The PageFormat class provides methods for determining the height and width as well as the printable height and width of a page in 72nds of an inch. How does one determine the intended printing resolution (dpi) of the print job? If this cannot be done, how is it possible to determine the size of the page in pixels?
    I'd really appreciate help with this as fast as it can be mustered. This program needs to be completed by Friday morning and I'm clueless as to the Java printing interface.
    Thanks for any time you've spent reading this and the time you may spend answering it. :) Best wishes!

    After perusing the many books of one of my coworkers, I think I have good reason to believe the following:
    * Printing with the Printable interface is always at a resolution of 72dpi (1 pixel per point).
    * The Graphics object passed to print(...) represents a pixel space equal to the number of points of each dimension of the imageable area of the page.
    Can anyone with experience with Java printing confirm these conclusions?

  • Question about mapping a JAVA Interface with Flex

    I am using Granite Data Services (Java backend) with my Flex
    client.
    The Java server has an Interface called
    public interface IService {String getServiceName();}
    The flex client makes a remote service call on the server
    POJO which returns any implementation of the specified interface.
    On the flex side I have an interface
    [Bindable]
    [RemoteClass(alias="com.*****.proxy.pojo.IService")]
    public interface IService{function getServiceName():String;}
    As shown i am binding it to the server interface.
    From the client I make a call to the server and handle the
    result as shown below
    var serviceInstance:IService =
    (remoteO.testInterface.lastResult as IService);
    Alert.show("Service Name :
    "+serviceInstance.getServiceName());
    The call reaches the server and the remote method is being
    called however the Alert is not working.
    Please Help !!

    //Start other thread closeT
    System.exit(0)
    //code for thread closeT
    //wait 10 s
    Runtime.getRuntime().halt()
    Gil

  • Question about Methods

    I was wondering why i was not getting the right output. The program should give me..
    Storing : 0
    Checking Queue...
    Printing: 0
    Storing: 1
    Checking Queue...
    Printing:1
    etc...
    but instead all I get is...
    Storing :0
    Checking Queue..
    Storing:1
    Checking Queue...
    etc..
    Here is my Code:
    import java.util.ArrayList;
    public class Problem {
         public Storage storeMe = new Storage();
         public static void main(String[] args) {
              (new Thread(new Counter())).start();
              (new Thread(new Printer())).start();
    class Storage {
         ArrayList<Integer> queue = new ArrayList<Integer>();
         public int value;
         public int getValue() {
              if (queue.size() > 0)
                   return queue.remove(0);
              else
                   return -1;
         public void storeValue(int newvalue) {
              value = newvalue;
              queue.add(value);
    class Counter extends Problem implements Runnable {
         public void run() {
              int counter = 0;
              while (true) {
                   synchronized (this) {
                        try {
                             this.wait(1000L);
                        } catch (Exception e) {
                   System.out.println("Storing : " + counter );
                   storeMe.storeValue(counter);
                   counter++;
    class Printer extends Problem implements Runnable {
         public void run() {
              while (true) {
                   synchronized (this) {
                        try {
                             this.wait(1000L);
                        } catch (Exception e) {
                   System.out.println("Checking Queue ...");
                   int num = storeMe.getValue();
                   if (num != -1)
                        System.out.println("Printing : " + num);
    }I think the problem with my code is that in the Printer class, the storeMe.getValue() method is not updated. Or more accurately, the queue is not updated.
    Can anyone shed some light on this matter?
    thanks in advance..

    thanks for that info.
    this program worked fine using static variables and methods. But now I'm trying to do it without using static modifier with the exception of the main().
    I tried checking if there was an update on queue in the Printer class by changing the code to this:
    class Printer extends Problem implements Runnable {
         public void run() {
              while (true) {
                   synchronized (this) {
                        try {
                             this.wait(1000L);
                        } catch (Exception e) {
                   System.out.println("Checking Queue ...");
                   int num = storeMe.queue.size();
                        System.out.println("Queue size: " + num);
    }and I got this output:
    Storing : 0
    Checking Queue ...
    Queue size: 0
    Checking Queue ...
    Queue size: 0
    Storing : 1
    So basically, when the Printer Class runs and tries the to get the value from the storage class, it can't because the value read was 0, there was no queue. Does this mean that I have created a wrong object of the Storage class? or is there another way to call that method without using the static modifier?
    Specifically, How do I Try to call an instance of a method with its values still intact? Let's say I want My Printer Class to access the values inputted by the Counter Class in the Storage Class.

  • Question about SSO (for Java Dialog instance) for Portal EP 6.0 SP 20

    Hi All
    I have following question.
    We are running Portal 6.0 SP 20. We have JAVA (Portal) CI+DB running on single host SAPQP1 and Two JAVA Dialog Instances (J2EE Application servers) running on separate host namely SAPAP35 and SAPAP36.
    I was able to configure SSO between SAPQP1 (CI+DB) and ECC R/3 system (QC1). SSO is working fine. Users can access all ESS/MSS data in portal when they use Portal URL running on SAPQP1 (http://sapqp1.xxxxx.com:50000/irj).
    But when users try to access ESS/MSS data via portal (URL) running on SAPQP35 (http://sapap35.xxxxx.com:50000/irj) and SAPAP36 ((http://sapap36.xxxxx.com:50000/irj) SSO does not work, i.e it system asks user id and password.
    So , how I can configure SSO between SAPAP35 and SAPAP36 JAVA Dialog instances and ECC R/3?

    Hi Sandip,
    obviously you already configured QC1 to accept Tickets issued by the Java Engine on SAPQP1. Did you imported the public (java) certificates from SAPAP35 and SAPAP36 into your ECC System? The certicficates need to be added in client 000 to your System PSE and in your production client to your ACL.
    Regards,
    Enno

  • Question about Dates in java.

    Hello ,
    I have a JTextfield in a swing app. that is supposed to hold date and saves its values to a database column of Date type .
    All I want is when i get the text from the TextField to use it in the prepared statement is to be of Date type not string , and also I want the user not to be able to insert anything but date in that field , I tried this but didnt work :
    Date date = new Date(0000-00-00);
    pstmt.setDate(5, date.valueOf(itemPurchaseDate.getText()));
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
            at java.sql.Date.valueOf(Date.java:103)
            at omsapplication.AssetsMainPanel$1.actionPerformed(AssetsMainPanel.java:104)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6216)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5981)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4583)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4413)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)Also how can I force the user to enter only dates ?
    Thanks.

    Im trying to get the the String in the TextField in order to use it in the setDate method of the Prepared Statement but there is something wrong with it :
    String DATE_FORMAT = "MM/dd/yyyy";
    sdf = new SimpleDateFormat(DATE_FORMAT);
    Date sdate = new Date();
    try{
    sdate = sdf.parse(itemPurchaseDate.getText());
    pstmt.setDate(5, sdate); //.............>>  The compiler says here : Cannot find symbol ?
    catch(ParseException pe)
    {pe.printStackTrace(); }

  • Question about VB6 (no Java)

    Maybe someone can help me...if I wante to compile and run Java programs, I download JDK...but what do I do if I want to download a VB6 compiler? Is it free? How can I download it? Please don't say do a google, cos I did and was confused with all the information given back...all this stuff about runtime and controls and have you what...
    someone please answer me if you know the answer to this...

    My brother is a phycisist. He has no use for Java.I don't see why a physicist would look down his nose at Java. They write code, too.
    He's just got into VB recently cos he's been using a bit of Excel lately.If your brother is that smart, he doesn't need your help to figure out how to get ahold of and learn VB.
    He is a very nice guy. He is very unargumentative. So do not speak ill of him.Your brother sounds like a better person than you. Can you shove off and send him over to the forum?
    Honestly, I don't understand how you could think this behavior would ingratiate you with forum regulars.
    Why don't you follow annie's example? She offers good technical bits when she can and fine craic the rest of the time. She's obviously got some brains and manners. Why are you so lacking in both?
    %

  • Question about Technologies in JAVA

    Hi all,
    I'm starting my final project for Computer Science in University and I pretend to develop an integrated e-learning environment for my faculty. I want to develop this by using JAVA WEB START within a JNLP, as a stand-alone application (not within a web-server as in JSP or so).
    I would like to connect this application to a database server (oracle or posgres) to retrieve some data, might use also some connections to a NFS server and may use som cryptographic utilities to ensure some security on the application.
    Would anyone recommend me some IDE to develop such application? I would also ask for some ideas about the techonologies to use, since there are so many that I cannot find myself on this mess ;-)
    Thanks in advance to everyone,
    Fernando Mart�n
    [email protected]

    However, when I program in Java
    i find that most of the time I am making my variables
    global. (meaning public)Sounds like you are writing bad code. Public variables should be extrememly rare except static final 'constants'.
    Is the fact that they are in a class supposed
    to make up for that fact? Absolutely not.
    With subclasses you can also
    have 3rd parties working on your data. It seems a bit
    suspicious to me, like the data should be maintained
    more strictly, or is it my job to do that? Yes, it's your job.
    I am just
    curious, but it seems java promotes global variables,
    which I find to be bad programming practice since
    anything can operate on them. I appreciate any
    input...Java does not promote public variables. You have to explicitly make them public.

  • Question about Flash and Java in OS X 10.6.6...

    I am about to get a MacBook Pro this weekend (after having numerous problems with Windows and getting it to work with my hardware - that led to me thinking of buying a Mac. Nothing to do with viruses actually ;))
    Anyhow - does Snow Leopard 10.6.6 ship with Adobe Flash, or do I need to install Flash myself? I'm not too concerned with doing that (I prefer to as I know I'm installing the latest version - just like Windows!)
    Secondly - does Apple provide updates for Java as well? Do I need to install Java myself, or does Snow Leopard ship with Java pre-installed too?
    Lastly - this is just your opinions, but which out of Safari, Chrome, Opera and FireFox do you consider to be the safest (and securest) browser for Mac?
    Thanks,
    Xavier12.

    It is best to follow Adobe's instructions for updating its software, which varies by the product & versions involved. This usually works well but Adobe is one of those companies big enough to play by its own rules, sometimes ignoring Apple's developer guidelines or inventing its own API's instead of using the Apple provided ones that do the same thing, so there is a small chance things won't go as expected even when you follow the instructions to the letter.
    If this happens it is best to seek advice on a product by product, version by version basis.

  • Questions about Video calling - Subscription?

    whenever i try calling my friends it says, "problem with playback device" why is that? and how can i fix it?

    In Skype open Tools -> Options -> Audio settings. What device is sellected there as your Speakers? Is there more than one option available in the selection box?

  • Question About Receiving Calls

    I thought I posted this today, but I can't find it. Why is it that when I receive a call, that as soon as I hang up the phone goes to my phone favorites?? Why does it do that? Why will it not go to my home screen where it was? I am missing a setting somewhere?
    Thanks,
    Joe

    Not with my phone.
    1. Open Safari. While using Safari, call your phone from another line and answer the call. End the call, and the screen goes back to Safari.
    2. Open Phone app, select the dial pad, close Phone app with the home button and lock your phone. Call your phone from another line, answer it and end the call. The phone then relocks. Unlock the phone and it will open to the dial pad screen.
    If your phone doesn't do this, do the usual troubleshooting steps as needed:
    1. Reset the phone - press both home and sleep/wake buttons until the Apple logo appears.
    2. Restore in iTunes from a backup
    3. Restore as a new iPhone.

Maybe you are looking for

  • Video Clip Properties Blank - Mixed Selection

    I have downloaded Encore 2.0 trial Version for Windows to assure it works before purchase. I succesfully created a DVD using QT Ref. imports from Avid MC. At one point I actually saw the properties for a video clip where I could add SCC file. Since t

  • Importing while digitizing, clips not importing

    I am trying to import footage from VHS tapes. I've done this before with FC Express and worked like a charm: I connect the VHS VCR to a DV camera with AV>DV capabilities with standard AV cables, and connect the DV camera to my iMac using firewire. Wi

  • Songs not showing up in playlist when I shuffle it

    Currently I have 1,493 songs on my iPhone. When I shuffle them without adding them to a playlist, only 1,490 display to play. Where did the other 3 go? I have this problem when I shuffle them on the playlist too. Does anyone else experience this? Ple

  • Developer Mode Exception

    I am not able to understand what the below exception means,I would like to fix this in the Development environment.Can somebody help me in deciphering the exception. Developer Mode Exception encountered in item CacNtsCreateNotes Error: The item CacNt

  • Signal dropped every minute

    Hi, I set up my Airport Extreme. However, my Vista laptop keeps dropping the wifi signal every minute or so and then reconnects to it. Any ideas? It only seems to be my laptop that does this. My other laptop running XP and my mac does just fine. Than