0

I have a question that has been boring me for some days. I'm implementing some swing interfaces that is supposed to load an image:

When i press a button it triggers an event that calls the following method:

private void GetDataPanelGeral()
{
    ArrayList<String> Files = new ArrayList<>();

    java.util.Date selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_START.getModel().getValue();
    java.sql.Date sqlDateINICIO = new java.sql.Date(selectedDate.getTime());

    selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_END.getModel().getValue();
    java.sql.Date sqlDateFIM = new java.sql.Date(selectedDate.getTime());


    //CALL MODULE 5
    RunningLoop loop = null;
    Thread t = new Thread
    (       
        loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
        this.StringDBUser,this.StringDBPass,this.PathToData,
        sqlDateINICIO.toString(),
        sqlDateFIM.toString(),
        true)
    );
    t.start();
    loop.StopLooping();


    this.ImageLabelGERALMUNDANCAS.revalidate();  
    this.ImageLabelGERALMUNDANCAS.repaint();


    this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png",""));
    this.ImageLabelGERALMUNDANCAS.repaint();


    JPanel Aggregator = new JPanel();
    Aggregator.add(this.ImageLabelGERALMUNDANCAS);
    Aggregator.setBackground(this.color);

    //this.Aggregatorr.add(this.ImageLabelGERALMUNDANCAS);
    this.JPanel_TABGeral.remove(1);
    this.JPanel_TABGeral.add(Aggregator,1);
    this.JPanel_TABGeral.revalidate();
    this.JPanel_TABGeral.repaint();
    Aggregator.revalidate();
    Aggregator.repaint();

}

the image loaded in the line this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png","")); is created by a module called in the lines

//CALL MODULE 5
    RunningLoop loop = null;
    Thread t = new Thread
    (       
        loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
        this.StringDBUser,this.StringDBPass,this.PathToData,
        sqlDateINICIO.toString(),
        sqlDateFIM.toString(),
        true)
    );
    t.start();
    loop.StopLooping();

it seems to be all ok but it does not load the image.. If i try to load another image it works perfectly so i think the problem is the creation of the image in the thread.

Can somebody help me?

Ps: I'm still need to work in some code, currently i'm trying everything to get this work, then i'll worry about better code

Kind regards

Pedro Neves
  • 364
  • 1
  • 8
  • 26
  • You create a new instance of `ImageLabelGERALMUNDANCAS`, but it is never added to anything so it is never displayed on the screen – MadProgrammer Jun 23 '14 at 00:03
  • That label is added to the JPanel Agregator that it is the added to the JPanel_TABGeral. I think the problem must be related with the creation of that image.. Maybe its nos yet ready when i try to load it ir something else.. Because if i try to add some other image it works properly – Pedro Neves Jun 23 '14 at 00:34
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem, this will reduce the guess work and produce better response – MadProgrammer Jun 23 '14 at 00:37
  • 3
    I would avoid using `ImageIcon` directly to load the image and instead use `ImageIO.read`, it will, at least, throw an `IOException` if the image can't be loaded. Also, I'm pretty sure `"this.PathToData+"/Results/Outputs/P5/0.png"` isn't going to compile... – MadProgrammer Jun 23 '14 at 00:39
  • As already stated, it is hard to tell without much information. Even the directory structure, used for the project is unknown. Though, please have a look at how to [add images to Java Project](http://stackoverflow.com/a/9866659/1057230) __(especially the last link)__, hopefully this be able to help somewhere :-) – nIcE cOw Jun 23 '14 at 05:19
  • 1
    Echoing what MadProgrammer said. Create a `URL`, perhaps even set the image file up as a classpath resource and load the `URL` that way (it'll generate an `Exception` if it fails as well) and use `ImageIO.read()`. – Gorbles Jun 23 '14 at 09:19
  • The runnable calls JFreechart API and creates a chart in one of the directories of OS which then i have to get. The ImageIO.read() helped a lot, in fact, the problem gets almost resolved with it, but the major problem is that sometimes the image is simple not ready when i require it. ;). Thanks a lot – Pedro Neves Jun 23 '14 at 10:17

0 Answers0