2

So basically im creating a GUI that allows the user to select a file, this file is check to be a .wav file. Then this file's data is graphed through JFreechart. This graph or image created by Jfreechart i want to put into the JFrame. The problem is that the code:

 ImageIcon myIcon1 = new ImageIcon("blah.jpg");   
 JLabel graphLabel1 = new JLabel(myIcon1);
 southContent.add(graphLabel1);

must be created & declared in the method where i create the JFrame ( must be added to the frame ) thus i cannot dynamically update the image to new created graphs, depending on what file the user selects. ( on selection of a new file, via button a new graph image is created )

is there a way to force

  ImageIcon myIcon1 = new ImageIcon("blah.jpg");   
  JLabel graphLabel1 = new JLabel(myIcon1);
  southContent.add(graphLabel1);

to update to the new image ( in the same direcotry, with the same name )

or is there a way using Mapping to set the image name ("blah.jpg") dynamically with a counter?

here is my SSCCE

public class gui extends JFrame {

    ImageIcon myIcon1 = new ImageIcon("C:/location/chart1.jpg");
    JLabel graphLabel1 = new JLabel(myIcon1);

    gui() {
        // create Pane + contents & listeners...
        JPanel content = new JPanel();
        JPanel southContent = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(open_File);
        // Jfreechart graph image  -- not visible until selected
        graphLabel1.setVisible(false);
        // this is the graph image being added to the panel
        southContent.add(graphLabel1);
        southContent.setLayout(new FlowLayout());
        // add action listeners to buttons
        open_File.addActionListener(new OpenAction());
        // set Pane allignments & size...
        this.setContentPane(content);
        this.add(southContent, BorderLayout.SOUTH);
        this.setTitle("Count Words");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1100, 720);
        this.setLocationRelativeTo(null);
    }

    //  opening selected file directory.
    class OpenAction implements ActionListener {

        public void actionPerformed(ActionEvent ae) {
            /// gets user selection ( file ) and procesess .wav data into array
            /// loops through array creating X series for JfreeChart
            // Add the series "series" to data set "dataset"
            dataset.addSeries(series);
            // create the graph
            JFreeChart chart = ChartFactory.createXYLineChart(
                    ".Wav files", // Graph Title
                    "Bytes", // X axis name
                    "Frequency", // Y axis name
                    dataset, // Dataset
                    PlotOrientation.VERTICAL, // Plot orientation
                    true, // Show Legend
                    true, // tooltips
                    false // generate URLs?
                    );
            try {
                ChartUtilities.saveChartAsJPEG(
                        new File("C:/location/chart1.jpg"), chart, 1000, 600);
            } catch (IOException e) {
                System.err.println("Error occured:  " + e + "");
            }
            // !!!!! this is where i need to set the ImageIcon added to the 
            //  panel in "gui" to this new created Graph image ("chart1.jpg") 
            //  as above
            //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            // sets the image label itself to visible as a new image has been
            // added ot it
            graphLabel1.setVisible(true);
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mitchb
  • 159
  • 3
  • 13
  • 3
    *"must be created & declared in the method where i create the JFrame"* Either this can all be solved by making `graphLabel1` a class level attribute that is visible to the later methodology, or I have no idea what you are talking about. .. Or possibly both. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 02 '12 at 07:07
  • i believe graphLabel1 is class level. sorry for the inconvenience, i posted a SSCCE. – Mitchb Oct 02 '12 at 07:44
  • 1
    1) I cannot understand why anyone would use JPEG to encode a chart. Try PNG. 2) *`// !!!!! this is where i need to set the ImageIcon..`* No. The place to do that would have been within the try, directly after the statement that the code is trying. If it fails, there is no point setting an icon. 3) It seems the disk access is only to ensure an image to display. Which makes it unnecessary. Use [`writeChartAsPNG(..)`](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartUtilities.html#writeChartAsPNG%28java.io.OutputStream,%20org.jfree.chart.JFreeChart,%20int,%20int%29) w. BAOS. – Andrew Thompson Oct 02 '12 at 07:58

3 Answers3

3

Just add the JLabel to its container as you usually do. Then, once you created the image and you have an instance of an ImageIcon, just call the setIcon() method for the JLabel.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • graphLabel1.setIcon(new ImageIcon("C:/imagelocation/image.jpg); doesnt seem to update at all for this. Not sure why. Ill have to push out a new JFrame containing the image, where i can locally add the imageIcon to. – Mitchb Oct 02 '12 at 08:06
  • 2
    If the path to the image is correct then it should work. I've just tried it. – Dan D. Oct 02 '12 at 08:19
  • no syntax errors. java.lang.NullPointerException error so the file directory must be wrong right? i just checked it 4 times its not. – Mitchb Oct 02 '12 at 08:38
  • 2
    On which line are you getting the NPE? – Dan D. Oct 02 '12 at 08:42
  • ImageIcon myIcon1 = new ImageIcon("C:/location/chart1.png"); ///////////////////////////// graphLabel1.setIcon(myIcon1); // on this line – Mitchb Oct 02 '12 at 08:52
  • thanks, the error is gone. However the icon is still not updating, does it require a different image or directory for the .setIcon to work? – Mitchb Oct 02 '12 at 10:02
  • If the ImageIcon is built using a valid image that actually exists on disk then it will work. – Dan D. Oct 02 '12 at 10:05
  • 1
    @user1405235 :Please have a look at this answer of mine for [HOW TO ADD IMAGES TO YOUR PROJECT](http://stackoverflow.com/a/9866659/1057230) and this [example](http://stackoverflow.com/a/11372350/1057230) :-) – nIcE cOw Oct 02 '12 at 10:57
  • sorry Gagandeep, but the images i wish to create are based on user selection, through a windows browser. And need to be dynamically assigned/created each user of a button. Although it was interesting :) – Mitchb Oct 02 '12 at 11:58
  • Thanks again dan, it now works perfectly! i simply needed to create dynamic incrementing names for the images, rather than trying to create the new images and replacing the old with the same name. ImageIcon myIcon1 = new imageIcon("C:location/chart"+counterVariable+".png"); – Mitchb Oct 02 '12 at 12:14
3

Using something like this should allow displaying both images of the waveform, as well as new images of the waveform.

try {
    //ChartUtilities.saveChartAsJPEG(
    //        new File("C:/location/chart1.jpg"), chart, 1000, 600);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ChartUtilities.saveChartAsPNG(baos, chart, 1000, 600);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BufferedImage image = ImageIO.read(bais);
    // !!!!! this is where we need to set the ImageIcon..
    graphLabel1.setIcon(new ImageIcon(image));
} catch (IOException e) {
    e.printStackTrace();
    System.err.println("Error occured:  " + e + "");
}

OTOH you might look to increasing the memory size and generate the entire waveform in one pass, then display the label in a scroll pane.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Thanks to Dan and Andrew Thompson, i have a working product.

I used a counting variable to count teach time the "OpenAction" button was selected. i then used this variable to make dynamic names for each image i created through JFreechart. Thus i used .setIcon to reset the icon image to each new created image with a new name. .setIcon does not seem to work if you are trying to reset the label to a Image icon that has the same name as the previously selected Image icon.

the finished code segment looks like:

ImageIcon myIcon1 = new ImageIcon("C:/location/chart"+CounterVariable+".png");
graphLabel1.setIcon(myIcon1);

for example this will create charts; chart1 chart2 chart3 and so forth.

Community
  • 1
  • 1
Mitchb
  • 159
  • 3
  • 13