0

タスクのリストを表示するカスタム JList を作成しようとしています。各タスクは次のように表されます。タイプ: 3 つの異なるアイコンで表され、2 つのアイコンでステータスが表され、単純な文字列として名前が付けられます。

私の問題は、アイテムを強調表示するまで、最初はカスタム CellRenderer がリスト内のアイテムに適切なアイコンを使用しないことです。今は少し変更し、常に同じアイコン (task_small.png) を使用しています。正しいデータを渡していると確信しています。

また、他の方法でこのコードを改善する方法についてアドバイスや批判があれば、喜んで伺います。JPanel を使用することが正しい選択であるかどうかはわかりません。

//imports...

class TaskListCellRenderer extends JPanel implements ListCellRenderer {
    private ClassLoader cl = this.getClass().getClassLoader();
    private JLabel statusAndName = new JLabel();
    private JLabel icon;

      public TaskListCellRenderer() {
        setOpaque(true);
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(5,5,5,5));
      }

      public Component getListCellRendererComponent(JList list, Object value,
          int index, boolean isSelected, boolean cellHasFocus) {
        Task entry = (Task) value;

        if (entry.isStatus())
            statusAndName.setIcon(new ImageIcon(cl.getResource("finished.png")));
        else
            statusAndName.setIcon(new ImageIcon(cl.getResource("unfinished.png")));
        statusAndName.setText(entry.getName());

        statusAndName.setBorder(new EmptyBorder(0,15,0,0));
        int type = entry.getType();
        System.out.println(type);
        if (type == 1)
            icon =  new JLabel(new ImageIcon(cl.getResource("task_small.png")));
        else if (type == 2)
            icon = new JLabel(new ImageIcon(cl.getResource("issue_small.png")));
        else if (type == 3)
            icon  = new JLabel(new ImageIcon(cl.getResource("request_small.png")));


        //My attempts to make this work- at first I had 3 pre-made Icons which I made icon = to;
        remove(statusAndName);
        remove(icon);
        add(icon, BorderLayout.WEST);
        add(statusAndName, BorderLayout.CENTER);
        revalidate();
        repaint();
        if (isSelected) {
          setBackground(Constants.blue);
          setForeground(Constants.black);
        } else {
          setBackground(Constants.lightGrey);
          setForeground(Constants.black);
        }
        return this;
      }
    }
4

1 に答える 1

1

これは私の問題を解決し、おそらくはるかに効率的です。みんなありがとう!

class TaskListCellRenderer extends JPanel implements ListCellRenderer {
    private ClassLoader cl = this.getClass().getClassLoader();
    private JLabel statusAndName = new JLabel();
    private JLabel icon = new JLabel();
    private Icon finishedIcon = new ImageIcon(cl.getResource("finished.png"));
    private Icon unfinishedIcon = new ImageIcon(cl.getResource("unfinished.png"));
    private Icon taskIcon = new ImageIcon(cl.getResource("task_small.png"));
    private Icon requestIcon = new ImageIcon(cl.getResource("request_small.png"));
    private Icon issueIcon = new ImageIcon(cl.getResource("issue_small.png"));

      public TaskListCellRenderer() {
        setOpaque(true);
        setLayout(new BorderLayout());
        setBorder(new EmptyBorder(5,5,5,5));
        add(icon, BorderLayout.WEST);
        add(statusAndName, BorderLayout.CENTER);
      }

      public Component getListCellRendererComponent(JList list, Object value,
          int index, boolean isSelected, boolean cellHasFocus) {
        Task entry = (Task) value;

        if (entry.isStatus())
            statusAndName.setIcon(finishedIcon);
        else
            statusAndName.setIcon(unfinishedIcon);

        statusAndName.setText(entry.getName());
        statusAndName.setBorder(new EmptyBorder(0,15,0,0));

        int type = entry.getType();
        System.out.println(type);
        if (type == 1)
            icon.setIcon(taskIcon); 
        else if (type == 2)
            icon.setIcon(issueIcon); 
        else if (type == 3)
            icon.setIcon(requestIcon); 

        if (isSelected) {
          setBackground(Constants.blue);
          setForeground(Constants.black);
        } else {
          setBackground(Constants.lightGrey);
          setForeground(Constants.black);
        }
        return this;
      }
    }
于 2015-04-19T18:50:10.383 に答える