Question

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);
        }
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top