Note that there are some explanatory texts on larger screens.

plurals
  1. POupdating JLabel image, dynamic via selection
    text
    copied!<p>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 <a href="http://www.jfree.org/jfreechart/" rel="nofollow">JFreechart</a>. This graph or image created by Jfreechart i want to put into the JFrame. The problem is that the code:</p> <pre><code> ImageIcon myIcon1 = new ImageIcon("blah.jpg"); JLabel graphLabel1 = new JLabel(myIcon1); southContent.add(graphLabel1); </code></pre> <p>must be created &amp; 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 )</p> <p>is there a way to force</p> <pre><code> ImageIcon myIcon1 = new ImageIcon("blah.jpg"); JLabel graphLabel1 = new JLabel(myIcon1); southContent.add(graphLabel1); </code></pre> <p>to update to the new image ( in the same direcotry, with the same name )</p> <p>or is there a way using Mapping to set the image name ("blah.jpg") dynamically with a counter?</p> <p>here is my SSCCE</p> <pre><code>public class gui extends JFrame { ImageIcon myIcon1 = new ImageIcon("C:/location/chart1.jpg"); JLabel graphLabel1 = new JLabel(myIcon1); gui() { // create Pane + contents &amp; 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 &amp; 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); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload