简体   繁体   中英

Swing: JList holding multiple Components as a single item

Here is my problem: I'm creating a window that is responsible for listing a layer, where it displays the layer's current image (in the form of a ImageIcon as of now), layer's name, and a check box to alter the current on/off state of said layer. The entire thing is supposed to be a knock-off of Paint.NET's Layer Window , as seen here:

My problem has been how to bypass JTable to structure it as so. I figure in the end I might have to resort to just making a dynamic table, but I am wondering if there is a way to make an item/container that can individually display those three components.

The closest I got was using a JLabel with its icon and text properties used, but I had trouble figuring out how to add the check box.

Should I use the layout manager to move the label list to the left, and add a new list filled with check boxes within the pane?

My code is probably as follows:

public class StudioLayerWindow extends JFrame
{
// Objects
JPanel buttonPanel;
JScrollPane layerScroll;

JButton addNewLayer;
JButton deleteCurrentLayer;
JButton duplicateCurrentLayer;
JButton mergeCurrentLayer;
JButton moveCurrentLayerUp;
JButton moveCurrentLayerDown;
JButton layerProperties;

// Constructors & Initializers
public StudioLayerWindow()
{
    // Main Window Initialization
    this.setTitle("Layers");
    this.setType(Type.UTILITY);
    this.setSize(200,200);
    this.setResizable(false);
    this.setAlwaysOnTop(true);

    initButtons();

    buttonPanel = new JPanel(new GridLayout(1,7));
    buttonPanel.setPreferredSize(new Dimension(this.getWidth(), this.getHeight() /    7));
    buttonPanel.add(addNewLayer);
    buttonPanel.add(deleteCurrentLayer);
    buttonPanel.add(duplicateCurrentLayer);
    buttonPanel.add(mergeCurrentLayer);
    buttonPanel.add(moveCurrentLayerUp);
    buttonPanel.add(moveCurrentLayerDown);
    buttonPanel.add(layerProperties);

    // Code for what I'd add here
    layerScroll = new JScrollPane();

    this.add(layerScroll , BorderLayout.CENTER);
    this.add(buttonPanel , BorderLayout.PAGE_END);
}

The code above does not contain any of my tried solutions, only the basic template I have been at.

Is there any way to make multiple components on a single row?

JList is a generic class.

  • Create a class which extends JPanel . (Let us call it RowPanel )
  • Put all elements in it required to be present in a single row (using a horizontal layout)
  • Create your JList using those panels like

     JList<RowPanel> list = new JList<RowPanel>(); 
  • You can refer to this for creating ListCellRenderer and ListCellEditor : https://docs.oracle.com/javase/tutorial/uiswing/components/list.html

Note: This should be done if you don't want to use JTable under any circumstances. JTable is a good alternative to this solution.

Hope this helps.
Good luck.

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