Question

I am making a battleship game and I'm trying to figure out a way to control buttons in a pane so that I can drag drop them and keep track of their indexes with a default list model.If I add string or ImageIcons it works fine but with buttons I get something different.

Here's my code:

public class ListModelExample extends JPanel {

  JList list;
  DefaultListModel model;
  int counter = 15;

  public ListModelExample() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    final JButton button = new JButton("button");

    for (int i = 0; i < 5; i++)
      model.addElement(button);

    addButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        model.addElement(button);
        counter++;    

      }
    });
    removeButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (model.getSize() > 0)
          model.removeElementAt(0);
      }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ListModelExample());
    frame.setSize(260, 200);
    frame.setVisible(true);

  }
}

If I add Buttons I get this result:

Result of adding buttons in the pane

So my question is: How is it possible to make buttons appear normally and not as text in a default list model?

Was it helpful?

Solution

  • this could be done only by Renderer, put only String value to the DefaultListModel

  • don't put any JComponents to the XxxModel

  • I'd be use JPanel with JButtons instead of JList as containers (required to change getScrollableBlockIncrement / getScrollableUnitIncrement for natural scrolling in compare with JList or JTable)

example about both a.m. ways

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;

public class ListButtons extends JFrame {

    private static final long serialVersionUID = 1L;

    public ListButtons() {
        setLayout(new GridLayout(0, 2, 10, 10));
        DefaultListModel model = new DefaultListModel();
        model.addElement(createButtons("one"));
        model.addElement(createButtons("two"));
        model.addElement(createButtons("three"));
        model.addElement(createButtons("four"));
        model.addElement(createButtons("five"));
        model.addElement(createButtons("six"));
        model.addElement(createButtons("seven"));
        model.addElement(createButtons("eight"));
        model.addElement(createButtons("nine"));
        model.addElement(createButtons("ten"));
        model.addElement(createButtons("eleven"));
        model.addElement(createButtons("twelwe"));
        JList list = new JList(model);
        list.setCellRenderer(new PanelRenderer());
        JScrollPane scroll1 = new JScrollPane(list);
        final JScrollBar scrollBar = scroll1.getVerticalScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar.getValue());
            }
        });
        add(scroll1);
        JScrollPane scroll2 = new JScrollPane(createPanel());
        add(scroll2);        
        final JScrollBar scrollBar1 = scroll2.getVerticalScrollBar();
        scrollBar1.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar1.getValue());
            }
        });

    }

    public static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 1, 1));
        panel.add(createButtons("one"));
        panel.add(createButtons("two"));
        panel.add(createButtons("three"));
        panel.add(createButtons("four"));
        panel.add(createButtons("five"));
        panel.add(createButtons("six"));
        panel.add(createButtons("seven"));
        panel.add(createButtons("eight"));
        panel.add(createButtons("nine"));
        panel.add(createButtons("ten"));
        panel.add(createButtons("eleven"));
        panel.add(createButtons("twelwe"));
        return panel;
    }

    public static JButton createButtons(String text) {
        JButton button = new JButton(text);
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ListButtons frame = new ListButtons();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                //frame.pack();
                frame.setSize(270, 200);
                frame.setVisible(true);
            }
        });
    }

    class PanelRenderer implements ListCellRenderer {

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JButton renderer = (JButton) value;
            renderer.setBackground(isSelected ? Color.red : list.getBackground());
            return renderer;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top