1 Reply Latest reply: Oct 24, 2013 7:06 AM by user8704911 RSS

    Displaying JList holding JLabel with icons of different height screwed up

    user8704911

      I started to create a unix 'make' wrapper, which filters for the binaries being created and display their names and potentially some icon for those in a JList.

      But sometimes the display of the icons is screwed up. It's cut to the size of a pure text (JLabel) line in the JList.

       

      Below you can find test program.

       

      You need to put two different sized picture files binary1.jpg and binary2.jpg in the run directory of the java build (i use eclipse)

      and create some test input file like this:

      > cat input
      binary1
      binary2
      binary3
      binary4
      binary5
      binary6
      binary7
      binary8
      binary9
      
      

       

       

      Set TEST_DIR environment variable to the build run directory.

      Finally start the java class like this:

      > bash -c 'while read line; do echo $line; sleep 1; done' < input| (cd $TEST_DIR; java -classpath $TEST_DIR myTest.MyTest)
      
      

       

       

      Then you should see the issue.

      ( Don't mind about the JAVA code in general - i know, that there are quite some other issues to fix :-) )

       

      - many thanks!

       

      best regards,
      Frank

       

      ================================================

      package myTest;
      
      import java.io.*;
      import java.util.HashMap;
      import java.util.Map;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Component;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.Dimension;
      import javax.imageio.ImageIO;
      import javax.swing.DefaultListModel;
      import javax.swing.ImageIcon;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JList;
      import javax.swing.JScrollPane;
      import javax.swing.ListCellRenderer;
      
      public class MyTest {
          static class MyCellRenderer extends JLabel implements
                  ListCellRenderer<Object> {
              private static final long serialVersionUID = 577071018465376381L;
      
              public MyCellRenderer() {
                  setOpaque(true);
              }
      
              public Component getListCellRendererComponent(JList<?> list,
                      Object value, int index, boolean isSelected,
                      boolean cellHasFocus) {
                  if (value.getClass().equals(JLabel.class)) {
                      JLabel label = JLabel.class.cast(value);
                      setText(label.getText());
                      setIcon(label.getIcon());
                      setBackground(label.getBackground());
                      setForeground(label.getForeground());
                  } else {
                      setText(value.toString());
                      setBackground(Color.WHITE);
                      setForeground(Color.BLACK);
                  }
                  return this;
              }
          }
      
          static final Map<String, String> fileToPicture = new HashMap<String, String>() {
              private static final long serialVersionUID = 1L;
              {
                  put("binary1", "binary1.jpg");
                  put("binary2", "binary2.jpg");
              }
          };
          static boolean endProcess;
          static boolean guiStarted;
          static DefaultListModel<Object> listModel;
          static JList<Object> list;
      
          static public void startGui() {
              JFrame frame = new JFrame("Building...");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setLayout(new BorderLayout());
              listModel = new DefaultListModel<Object>();
              list = new JList<Object>(listModel);
              list.setCellRenderer(new MyCellRenderer());
              list.setLayoutOrientation(JList.VERTICAL);
              list.setFixedCellHeight(-1);
              JScrollPane scrollPane = new JScrollPane(list);
              scrollPane.setPreferredSize(new Dimension(300, 500));
              frame.getContentPane().add(scrollPane, BorderLayout.NORTH);
              JButton ok = new JButton("CLOSE");
              ok.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      endProcess = true;
                  };
              });
              frame.getContentPane().add(ok, BorderLayout.SOUTH);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
              guiStarted = true;
          }
      
          static private void addElem(String string, Color color) {
              String fileName = fileToPicture.get(string);
              ImageIcon icon = null;
              if (null != fileName) {
                  File file = new File(new File(".").getAbsolutePath() + "/"
                          + fileName);
                  try {
                      icon = new ImageIcon(ImageIO.read(file));
                  } catch (IOException e) {
                      System.err
                              .println("Exception: IOException for trying to read file '"
                                      + file + "'");
                      e.printStackTrace();
                  }
              }
              JLabel label = new JLabel(string);
              Color darker = color.darker();
              label.setForeground(darker);
              label.setBackground(Color.WHITE);
              label.setIcon(icon);
              listModel.addElement(label);
              list.ensureIndexIsVisible(list.getModel().getSize() - 1);
          }
      
          public static void main(String[] args) {
              endProcess = false;
              guiStarted = false;
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      startGui();
                  }
              });
              while (!guiStarted) {
                  try {
                      Thread.sleep(200);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              String line = null;
              try {
                  while (!endProcess) {
                      line = br.readLine();
                      if (line == null) {
                          break;
                      }
                      addElem(line, Color.BLACK);
                      System.out.println(line);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
              if (!endProcess) {
                  addElem("...DONE", Color.RED);
                  while (!endProcess) {
                      try {
                          Thread.sleep(200);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }
              System.exit(0);
          }
      }