-1

I want to display image from JTable selected index text. For example, if selected index text = jeans, I want to display on JLabel jeans image. But my code doesn't work. I can't display image to label.

Here's the code:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     

   DefaultTableModel model = (DefaultTableModel)jTable1.getModel();

    // get the selected row index
   int selectedRowIndex = jTable1.getSelectedRow();

    // set the selected row data into jtextfields
   jTextField1.setText(model.getValueAt(selectedRowIndex, 0).toString());
   jTextField2.setText(model.getValueAt(selectedRowIndex, 1).toString());
   jTextField3.setText(model.getValueAt(selectedRowIndex, 4).toString());


   String strJeans ="Jeans";

    ImageIcon imageresult = null;
    if(jTextField3.getText()==strJeans)
    {
            icon = new ImageIcon(icon.getImage().getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH));
            jLabelPic.setIcon(icon);  
    }

    else
    {
    JOptionPane.showMessageDialog(null, "err");  
    }
    jLabelPic.setIcon(imageresult);
}            
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
iskifogl
  • 1
  • 4
  • so... what's the problem? – sleepToken Dec 17 '19 at 13:56
  • I can tell you already that you should *not* be comparing Strings in java with `==`. – sleepToken Dec 17 '19 at 13:56
  • You are comparing your string with `==` instead of `equals`. Why? – RealSkeptic Dec 17 '19 at 13:56
  • it doesnt work, jlabel doesnt display image – iskifogl Dec 17 '19 at 13:56
  • 2
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sleepToken Dec 17 '19 at 13:57
  • Also, are you sure you want to create an `ImageIcon` containing the previous `icon`'s image? – RealSkeptic Dec 17 '19 at 13:58
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jan 06 '20 at 01:24

1 Answers1

0

After your if/else statement you call jLabelPic.setIcon(imageresult); where imageresult is null