简体   繁体   中英

Java Swing. Add custom items to scroll pane (Tile selector)

I am currently working on a TiledMap editor for a game I am working on. I am now at the part where I need to implement a tile selector. I am pretty sure I would be able to do this in canvas and draw each tile individually and draw a box around the selected tile but I want to know if there is an easier, more professional looking way already implemented in java swing before I start making my own.

Here is a basic drawing of what I want: 在此输入图像描述

Green boxes in tile selector are individual tiles and the one with bold blue it the selected one (Just a basic example, colors don't matter to me).

Additional Info: Each tile has it's own object, so if the solution involves an ArrayList or something, it would work very well. Also I would like to be able to manipulate which objects are shown in the selector or not. I have a search bar that I want to use to narrow down the tiles shown.

Thanks in advance, if you need more details, please ask.

A JList can display a collection of tile images just fine and would be the likely candidate for the tile selector component on the left.

Ok looks pretty good. How do you tell the JList what you want to be displayed about each item in the list. For my case I want to JList to display a simple image, not text, is that possible?

If you add Icons to the JList, they will be automatically displayed correctly. If you need to fine tune the display, then you will want to write a ListCellRenderer as per the tutorials and API.

Also, can it do lists with multiple columns?

If you mean multiple columns of the same thing, such as a 4x4 grid of images, then yes a JList can handle this great, and you would call setLayoutOrientation(JList.HORIZONTAL_WRAP) (or vertical wrap if desired). If you mean columns which each hold a different data type such as an image, text, a checkbox, then go with a JTable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM