-2

I am working for Swing accessibility. I am using ImageIcon. I need to place the alternative textual information when image URL is broken.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    can you post what you have tried so far? See http://stackoverflow.com/help/how-to-ask – ib11 Jun 21 '16 at 07:50
  • 1
    Welcome to Stack Overflow. Please read stackoverflow.com/help/how-to-ask for guidelines on how to write questions. For starters: 1. Please format your code. 2. Add more code to your question to help us understand it better and for future readers to benefit from this question. 3. Look for other questions which might help you out already. 4. Format your text, and give us a minimum working example – abarisone Jun 21 '16 at 07:58

2 Answers2

2

For this purpose, all supported Swing Look & Feel implementations include an Icon named "html.missingImage":

Icon missing = UIManager.getIcon("html.missingImage");

A ListCellRenderer using the icon is shown here; a related TableCellRenderer is shown here. Note that each uses setHorizontalTextPosition() and setVerticalTextPosition() to position the label's text relative to the icon.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

If I understand well your problem so the answer is : Just surround you code with try and catch

ImageIcon icon = null;
try {
  icon = new ImageIcon(new URL("yourimageUrlHere"));
} catch(Exception e) {
  icon = new ImageIcon("your placeholder path");
}
Mel
  • 5,837
  • 10
  • 37
  • 42
BOUALI ALI
  • 230
  • 2
  • 14