Display JList holding JLabel with icons of different heights screwed up

I started to create a wrapper for unix 'make', that filter for binary files created and display their names and potentially an icon for those in a JList.

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

Below you can find the test program.

You have to put two image of different size of the files binary1.jpg and binary2.jpg in the execution of the generation of java directory (I use eclipse)

and create a test like this input file:

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

The build run directory TEST_DIR environment variable value.

Finally start the java class as follows:

> 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 question.

(Doesn't bother me on the JAVA code in general - I know, that there is enough other questions of difficulty :-))

-Thanks a lot!

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);
    }
}

I discovered, that the problem is caused by trying to use the automatic scrolling via the #100 line:

list.ensureIndexIsVisible(list.getModel().getSize() - 1);

This helped to solve:

Java - ensureIndexIsVisible (int) does not work - Stack Overflow

The result is:

if (autoScroll.isSelected()) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
         list.ensureIndexIsVisible(list.getModel().getSize() - 1);
  }
  });
}

Rgds,
Frank

Tags: Java

Similar Questions

  • How can I make the favorite file as a desktop shortcut. Before having my calculation fixed, I had a shortcut on the desktop that displays all my favorites with icons?

    My computer repaired now requires that I go on Google etc. which at the favorite toolbar appears and when I click it I get my favorites OK listedn. Before the repair, I got a desktop shortcut that, once clicked gave me all my favorite disolayed on an entire page with icons. I know that the entire page is that I got once. but I can't seem to get it as a shortcut on the desktop. Can anyone help?

    In my computer | Local disk C | Documents and Settings. User name | Favorites. Right click on the Favorites folder and send to | Desktop computer.

  • Flash Web Sites with Pages of different heights

    http://louiswu.aisites.com/imd233/final/index.html

    The link above for the my last class project. On all pages of the site, you will notice that when you scroll in your browser, it is a big gap between the bottom of the content and the bottom of the browser. The exception is the STORE.

    I just stumbled on a "'limited ' of creating websites with Flash, in that the page with the largest content will determine which scene is the height for the rest of the site?"

    I don't know yet what I should ask here: is there some kind of workaround, or altogether different kind, I should work when using Flash and HTML/CSS together?

    The only option I see right now is to create separate .swf for each pages files and then incorporate them into their own HTML files.

    Laughing out loud. Just to answer my own question?

    When you create a Flash swf file it has a size of stage, and that the size of stage, it is everything that makes you.

    Most people design a Flash site to match the size of the browser window and not one that requires scrolling.  In this scenario, if it was necessary to scroll, the scroll would be built into the SWF and scrolling the browser would remain unchanged.

    For a site whose height varies from one page to another, it would be more likely to not create a Flash site and instead of creating individual pages for each section.

    If somehow you answered your own question so.  If you want to use Flash to design and you want to have different sections parade only to the extent necessary by using the wheel of the browser, the best way to accomplish this is to have each page as a separate html page containing a separate swf file.

  • Display text with icons

    JDev Version: 11.1.16

    I would like to display text with icons, when the text wrapping it looks like below

    for example. blah blah blah blah blah blah blah blah blah blah blah blah blah

    blah blah blah blah blah blah blah blah blah blah blah blah blah

    blah blah blah blah blah blah blah blah blah blah

    I tried with ADF CommandImageLink and goImageLink but these display the first icon next text.

    I also tried outputText and image, but the creation of two columns.

    Thank you

    Kala

    I tried with ADF CommandImageLink and goImageLink but these display the first icon next text.


    The default value is the first, but the position of the icon can be defined as an accompaniment with iconPosition.


    Icon="/images/search.gif".

    actionListener = "#{some.el.expression}" iconPosition = "creeping" / > "

  • With the help of iPhone 6 s. try to copy the text message with a tap double and get a bubble with icons. How can I copy the message to paste in another?

    Try to copy a text to paste in another. When I double tap, as I always have, all I get is a bubble with icons on it. With the help of OS 10.0.2.

    maryml wrote:

    Try to copy a text to paste in another. When I double tap, as I always have, all I get is a bubble with icons on it. With the help of OS 10.0.2.

    Press and hold on the text and wait for the copy/more pop up.

  • My icons are different!

    Hey,.

    I played a game (the one who changes your screen resolution), and after that I started my computer today, some of my icons are different!
    I don't know if it is a virus, but I put this thread here, just in case.
    In any case, what happened was that some of the programs on my desktop (Adobe ROME, TeamViewer and Malwarebytes) had the icon you see on Windows XP when a file has no association. I am running Windows 7, by the way. I then went to the folders that the underlined shortcuts, and I saw that the exe that ran the shortcuts had the same icon! I scanned ROME with AVG, AVG crashed the first time, but has analyzed the file next time, saying: there is no threat, but I am still concerned.
    I rebooted once. Same icons.
    Then, I deleted my cache icon (with disk cleanup) and restarted. Always the same icons.
    I then ran Malwarebytes from the office, and to my surprise, the taskbar icon was that XP icon too!
    I'm really scared. What is going on?!

    See if that helps.

    http://www.thewindowsclub.com/file-association-Fixer-for-Windows-7-Vista-released

    http://www.Winhelponline.com/blog/file-Asso-fixes-for-Windows-7/

    http://www.SevenForums.com/tutorials/19449-default-file-type-associations-restore.html

    -steve

  • Excel, Word, PDF icons and newly created files changed with the page folded with icons instead of appropriate icons.

    Original title: Excel, Word, and PDF icons do not appear I folded the page with icons instead of using appropriate icons - even with newly created files - Microsoft Windows 7!

    I have Windows 7.  I folded the page with icons instead of appropriate icons of Microsoft - even with the newly created files.

    When I change the "open with" to the different program and then select the right product to Microsoft again, it always produces generic folded paper with icons on it instead of the icon to the right for Excel, Word, PowerPoint, etc.  Help!

    Restore your default file types associations...

    http://www.SevenForums.com/tutorials/19449-default-file-type-associations-restore.html

    Scroll down to exe - executable application files.
    Click on the exe

    Follow the instructions from #1 to #6 in the upper part.

    Scroll to lnk - shortcut link (LNK) files.
    Click on lnk

    Follow the instructions from #1 to #6 in the upper part.

  • working capital with images of different sizes in the table

    Hello, I have a table with images of different size.  I want to use a bearing on each Panel, but my image is not the same size.  When I roll over the image the first image disappears.  Any suggestions?  I use CS5.5 Show map to the bottom.of page.

    Kappa Delta Pi

    Thank you

    Also I have never been able to use the "Preview in browser" globe icon  I don't seem to be, any suggestions on how to add this?

    A very simple solution is to make a second image that contains the image superimposed on the map. You must have 4 total images for this (all images have the same size):

    1. the plan with no photos

    2 - 4. The card with a unique image on the desired region (i.e., 3 additional images).

    In your access points, you exchange the entire map with the map of the desired region.

  • Menu with icons for submenus

    Hello

    I need to create a page that links to 4 pages of submenu. I would like to do with icons. What are my options/best ways to solve this problem?

    When I tried to just create a menu, it listed only the main pages, but not what I need.

    Hello

    To display submenu in the Menu pages, please change the Type of Menu to 'all pages '. If "top level pages" are selected in the Types Menu, it displays pages of hand only.

    See you soon!

    Aish

  • Design Notes - DW display all files referenced with notes?

    Hi all

    Is there a way to have the DW to display all files with Design Notes on them? I would like to take notes on all the php files that I edited to keep track of changes to the code (especially when the updating of modules), but I can't seem to find a way for all editions see for a working site. Is this possible? If so, how? If not, does anyone know of I workaround? (apart from using a repository of course).

    Thank you very much if anyone has any ideas on this.
    Steve

    Well, it's not the perfect solution for me, but here's the closest thing to literature.

    Notes: To display the yellow Design Notes icons, select Site > Manage Sites > [your site name] > Edit > file on the Advanced tab view columns. Select Notes in the list Panel and choose the display option. When you click the expand button on the file toolbar to view the site for both local and remote, you have a Notes column in your local site that displays a yellow icon notes for any file with a Note of Design.

    Man, I just kept missing this in the docs, finally... I hope this can help someone else in the future!
    Steve

  • Firefox has crashed on the desktop. Get only the black screen, taskbar with icons and hourglass cursor of the mouse on the taskbar. Windows 10.

    I tried to restart Firefox, nothing helps. Then I tried to restart my desktop computer Dim screen with taskbar with icons, hourglass cursor that does nothing on tasckbar only. I tried to close everything down and restart - without success. I write from my laptop, the problem is on my desk - Hewlett-Packard, 2-3 years, running windows 10. Only one prior occasion when Firefox crashed. Restart of Firefox took care of the problem and has been fine since.

    Contact a computer repair. It seems that the system files are corrupted.

  • In the visualization of my photos display problem: I have red spots in different parts of the pictures. Someone knows how to fix this?  I rebooted my MacBook Pro, but it does not solve the problem.

    In the visualization of my photos display problem: I have red spots in different parts of the pictures. Someone knows how to fix this?  I rebooted my MacBook Pro, but it does not solve the problem.

    Details please

    What version of Aperture? Is OS X 10.7.5 as you say correct for your OS? You shoot JPEG or RAW? If RAW device? Wharton has changed since this worked?

    LN

  • I have a question why U removed the session of keyboard spelling checker is a very useful sit Apple give it in products. It helps a lot in communicting with others from different countries, even in our studies and the communication

    I have a question why U removed the session of keyboard spelling checker is a very useful sit Apple give it in products. It helps a lot in communicting with others from different countries, even in our studies and communication with our teachers.plz it back... .hope you will do

    Nothing has been deleted. Perhaps if you explain better what it is you are looking for, a person might be able to help. You mean maybe predictive text? All these settings are in settings > general > keyboard.

  • Office filled with icons of unused files

    Sometimes, when I connect my desktop is completely filled with icons "unused folders."

    Hello

    1. which version of the operating system is installed on your computer?

    2 did you change on your computer before the show?

    This could be caused because the computer might be infected by the virus.

    Download Microsoft Safety Scanner, then scan your computer for viruses and malware then check how it works.

    You can download Microsoft Safety Scanner from the link "scan to Microsoft Security.

    Note:  The Microsoft Safety Scanner ends 10 days after being downloaded. To restart a scan with the latest definitions of anti-malware, download and run the Microsoft Safety Scanner again. All data that are infected must be cleaned only by deleting all of the data, which means that there is a risk of data loss.

  • Why can't I find the display on the Control Panel icon.

    original title: vista Control Panel

    Why can't I find the display on the Control Panel icon.

    Why can't I find the display on the Control Panel icon.

    Control Panel has no icon

    Start button > in the search box, type services > press Enter ket > scrollsoftware licensing...
    topic status, it must be said... Has begun
    under Startup Type, it must be said... Automatic
    If this isn't what you see, look in the left column, click Start (or restart)

    For the benefits of others looking for answers, please mark as answer suggestion if it solves your problem.

Maybe you are looking for

  • Reinstall XP Pro on nc8230

    I just reinstalled XP Pro on a nc8230.  Everything works, but in Device Manager, I got some yellow markings for Mass Storage, network controller and Simple PCI Communications.  I had a lot more but I was able to fix them.  I don't know what drivers t

  • Pavilion dv 7: pavilion dv7 hanging on the Welcome screen

    Laptop hangs on the HP welcome screen at the beginning... "Press esc Hey for the start menu ' and HP will appear on the screen. I pressed the ESC key at startup and startup displayed TI paused but was still frozen.

  • WRT160N: Getting dynamic IP address 169

    Hello! Linksys WRT160N, bought yesterday and I want it behaves same way did my Buffalo WHR-HP-G54. So I live in apartment building for students, and we don't have modems, just the RJ-45 ports in the wall. If I plug the cable directly from the wall of

  • Windows 7 Build 7601, non-genuine copy after the failure of an attempt to upgrade to Windows 10.

    I have a notebook Acer Aspire that I was given as a gift in 2009 after being seriously ill, it came with Windows 7 above and that it is authentic! I had a neighbour/friend IT fix a few years after obtaining a virus, had no problem until recently trie

  • 1280 x 1024 resolution is no longer and the option

    original title: resolution problem My computer gives me is no longer the option "1280 x 1024" resolution, how do I get it back?  Highest available is '1024 x 768 '.