Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

can't get resources in jar file working
 Post subject: can't get resources in jar file working Post rating: 0   Post Posted: Thu 09 Sep, 2010, 12:25 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
Forex seems to mess up the resources in jar files. Instead off
getting "file://path" they will start with "jfx://" and the program
will crash. I was advised by DK support to update java but
I just tried with 1.6.0_21 and that also does not work. I added some
test content (see below and attachment). The dialog shows properly
when running on the command line with java -jar imagetest.jar but
fails on the platform. I probably do something wrong and I hope you
can tell me what that is.

==============================================================
In the platform:
==============================================================
package jforex;

import java.util.*;

import com.dukascopy.api.*;

import imagetest.Main;

@RequiresFullAccess
@Library("../JForex/Strategies/files/imagetest.jar")

public class ImageTest implements IStrategy {
       private IEngine engine;
       private IConsole console;
       private IHistory history;
       private IContext context;
       private IIndicators indicators;
       private IUserInterface userInterface;

       public void onStart(IContext context) throws JFException {
               this.engine = context.getEngine();
               this.console = context.getConsole();
               this.history = context.getHistory();
               this.context = context;
               this.indicators = context.getIndicators();
               this.userInterface = context.getUserInterface();

       Main m = new Main();
       }

       public void onAccount(IAccount account) throws JFException {
       }

       public void onMessage(IMessage message) throws JFException {
       }

       public void onStop() throws JFException {
       }

       public void onTick(Instrument instrument, ITick tick) throws JFException {
       }

   public void onBar(Instrument instrument, Period period, IBar
askBar, IBar bidBar) throws JFException {
   }
}


==============================================================
And In the jar file:
==============================================================
package imagetest;
import java.awt.BorderLayout;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.io.IOException;

public class Main {

       private JFrame frame = new JFrame("Configuration");
   private JPanel panel = new JPanel();

       public Main()
       {
           frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

           BoxLayout mainx = new BoxLayout(panel, BoxLayout.Y_AXIS);
           panel.setLayout(mainx);

           java.net.URL imageURL = Main.class.getResource("/grey_png.png");
               ImageIcon icon = new ImageIcon(imageURL);

           JButton addSetup = new JButton("Button Text", icon);

           JScrollPane scrollPane = new JScrollPane(panel);
           JLabel txt = new JLabel("icon: " + icon + " - imageURL: " + imageURL);

           panel.add(addSetup);

           panel.add(txt);

           frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

           frame.pack();
           frame.setVisible(true);
       }

       public static void main(String[] args) throws IOException {
           System.out.println("from main");
           Main m = new Main();

       }
}


Attachments:
imagetest.jar.zip [4.2 KiB]
Downloaded 408 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: can't get resources in jar file working Post rating: 0   Post Posted: Fri 10 Sep, 2010, 15:42 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Almost all manipulations with javax.swing objects must be done in AWT thread. You could use this:
           SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                   Main m = new Main();
               }
           });


But... thanks to your post we found that image loading is not working when done like in your code :) (fixed in next version) For now you need to do it like in code below:
package jforex;

import java.util.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;

import com.dukascopy.api.*;

import imagetest.Main;

@RequiresFullAccess
@Library("imagetest.jar")

public class ImageTest implements IStrategy {
       private IEngine engine;
       private IConsole console;
       private IHistory history;
       private IContext context;
       private IIndicators indicators;
       private IUserInterface userInterface;

       public void onStart(IContext context) throws JFException {
               this.engine = context.getEngine();
               this.console = context.getConsole();
               this.history = context.getHistory();
               this.context = context;
               this.indicators = context.getIndicators();
               this.userInterface = context.getUserInterface();

           final Main2 m = new Main2();
           SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                   m.createFrame();
               }
           });
       }

       public void onAccount(IAccount account) throws JFException {
       }

       public void onMessage(IMessage message) throws JFException {
       }

       public void onStop() throws JFException {
       }

       public void onTick(Instrument instrument, ITick tick) throws JFException {
       }

   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
   }

class Main2 {

       private JFrame frame;
   private JPanel panel;
   private ImageIcon icon;

       public Main2() {
           try {
               InputStream is = Main2.class.getResourceAsStream("/grey_png.png");
               try {
                   ByteArrayOutputStream bos = new ByteArrayOutputStream();
                   int i;
                   byte[] buff = new byte[512];
                   while ((i = is.read(buff)) != -1) {
                       bos.write(buff, 0, i);
                   }
                   icon = new ImageIcon(bos.toByteArray());
               } finally {
                   is.close();
               }
           } catch (IOException e) {
               console.getOut().println(e.getMessage());
           }
       }
       
       public void createFrame() {
           frame = new JFrame("Configuration");
           panel = new JPanel();

           frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

           BoxLayout mainx = new BoxLayout(panel, BoxLayout.Y_AXIS);
           panel.setLayout(mainx);

           JButton addSetup = new JButton("Button Text", icon);

           JScrollPane scrollPane = new JScrollPane(panel);
           JLabel txt = new JLabel("icon: " + icon);

           panel.add(addSetup);

           panel.add(txt);

           frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

           frame.pack();
           frame.setVisible(true);
       }
}
}


 
 Post subject: Re: can't get resources in jar file working Post rating: 0   Post Posted: Tue 28 Sep, 2010, 09:45 

User rating: 0
Joined: Fri 20 Aug, 2010, 14:09
Posts: 20
In the current version it does not seem to work when I move the jfx file to another pc.


 
 Post subject: Re: can't get resources in jar file working Post rating: 0   Post Posted: Wed 06 Oct, 2010, 14:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please check if path to your library is correct and try again with current version available on the Dukascopy site. We have checked, and it works as expected.


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com