about JCheckBox

How cam I add JCheckBox to JList?Now I have in textArea renderer for the jlist setText(string + "\t" +string);and the result is I have 2 columns in the list :)I whan that my items will be:setText(string+ "\t" +checkBox);Thank you
[278 byte] By [yael800a] at [2007-11-26 12:38:24]
# 1
[url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables[/url], would be the simpler solution. Why duplicate code in a JList when the support is already available in another component.
camickra at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 2
becouse I was builld "multy selection component" with 2 jlists that you can remov values between them. I am using alredy with list model.
yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 3
> becouse I was builld "multy selection component" with 2 jlists that > you can remov values between them.You can move values between TableModels as well.
camickra at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 4
Do you have example how to use with jtable as jlist?I don't have eny choyse? I can not add to list JCheckBox?Thanks
yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 5
You don't use a Jtable as a Jlist. You just use a JTable.There are a lot of examples here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.htmland all over the forum.
zadoka at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 6

How can I add to this renderer CheckBox?

myList.setCellRenderer(new TextAreaRenderer(4));

class TextAreaRenderer extends JTextArea implements ListCellRenderer {

public TextAreaRendererDefault(int tabSize) {

setTabSize(tabSize);

}

public Component getListCellRendererComponent(JList list, Object value,

int index, boolean isSelected, boolean cellHasFocus) {

if (isSelected) {

setForeground(Color.white);

setBackground(new Color(0, 0, 139));

} else {

setForeground(null);

setBackground(null);

}

setFont(GlobalProperties.font);

setText("Hello"+ "t" + CheckBox);

return this;

}

}

yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 7
> How can I add to this renderer CheckBox?> [code]> myList.setCellRenderer(new TextAreaRenderer(4));That looks fine.What is this:> setText("Hello"+ "t" + CheckBox);It doesn't look like it will compile. But either way why not use a
zadoka at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 8
Extend JPanel instead of JTextArea for your list cell renderer. Then, put a text area and a checkbox in that panel and set the values accordingly in the get...() method.
kirillga at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 9

Y E S !!!

This what I whanted but..plead run my example.

I neel one check box in every line of name.

I get alots of check boxs and I can't change the cb value (true / false)..

import java.awt.Color;

import java.awt.Component;

import java.awt.GridLayout;

import javax.swing.DefaultListModel;

import javax.swing.JCheckBox;

import javax.swing.JComponent;

import javax.swing.JDialog;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.ListCellRenderer;

import javax.swing.ListSelectionModel;

import javax.swing.event.ListSelectionEvent;

public class SelectionContacts extends JDialog{

private JList listContacts;

private DefaultListModel listModel;

public SelectionContacts() {

getContentPane().add(getList());

setTitle("List");

pack();

setSize(300, 300);

setAlwaysOnTop(true);

setModal(true);

setVisible(true);

}

public JComponent getList() {

listModel = new DefaultListModel();

listModel.addElement(new Item("yael", false));

listModel.addElement(new Item("roni", true));

listModel.addElement(new Item("dan", true));

listContacts = new JList(listModel);

listContacts

.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

listContacts.setSelectedIndex(0);

listContacts.setVisibleRowCount(-1);

listContacts.setCellRenderer(new TextAreaRenderer(4));

JScrollPane scrollPane = new JScrollPane(listContacts);

return scrollPane;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new SelectionContacts();

}

public void valueChanged(ListSelectionEvent arg0) {

// TODO Auto-generated method stub

}

class Item {

private String name;

private boolean checked;

public Item(String department, boolean checked) {

this.name = department;

this.checked = checked;

}

public boolean isChecked() {

return checked;

}

public String getName() {

return name;

}

}

class TextAreaRenderer extends JPanel implements ListCellRenderer {

JTextArea t;

public TextAreaRenderer(int tabSize) {

t = new JTextArea();

t.setTabSize(tabSize);

}

public Component getListCellRendererComponent(JList list, Object value,

int index, boolean isSelected, boolean cellHasFocus) {

if (isSelected) {

setForeground(Color.white);

setBackground(new Color(0, 0, 139));

} else {

setForeground(null);

setBackground(null);

}

Item item = (Item) value;

JCheckBox cb = new JCheckBox();

cb.setBackground(Color.white);

cb.setEnabled(true);

if(item.isChecked())

cb.setSelected(true);

else

cb.setSelected(false);

t.setText(item.getName());

setLayout(new GridLayout(1,2,0,0));

add(t);

add(cb);

return this;

}

}

}

yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 10

Please ignore my last code, I was pixed the problrom exept that I can't change the value of the JCheckBox to true/false for the list:

import java.awt.Color;

import java.awt.Component;

import java.awt.GridLayout;

import javax.swing.DefaultListModel;

import javax.swing.JCheckBox;

import javax.swing.JComponent;

import javax.swing.JDialog;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.ListCellRenderer;

import javax.swing.ListSelectionModel;

import javax.swing.event.ListSelectionEvent;

public class SelectionContacts extends JDialog{

private JList listContacts;

private DefaultListModel listModel;

public SelectionContacts() {

getContentPane().add(getList());

setTitle("List");

pack();

setSize(300, 300);

setAlwaysOnTop(true);

setModal(true);

setVisible(true);

}

public JComponent getList() {

listModel = new DefaultListModel();

listModel.addElement(new Item("yael", false));

listModel.addElement(new Item("roni", true));

listModel.addElement(new Item("dan", true));

listContacts = new JList(listModel);

listContacts

.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

listContacts.setSelectedIndex(0);

listContacts.setVisibleRowCount(-1);

listContacts.setCellRenderer(new TextAreaRenderer(4));

JScrollPane scrollPane = new JScrollPane(listContacts);

return scrollPane;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new SelectionContacts();

}

public void valueChanged(ListSelectionEvent arg0) {

// TODO Auto-generated method stub

}

class Item {

private String name;

private boolean checked;

public Item(String department, boolean checked) {

this.name = department;

this.checked = checked;

}

public boolean isChecked() {

return checked;

}

public String getName() {

return name;

}

}

class TextAreaRenderer extends JPanel implements ListCellRenderer {

JTextArea t;

JCheckBox cb;

public TextAreaRenderer(int tabSize) {

t = new JTextArea();

cb = new JCheckBox();

cb.setBackground(Color.white);

cb.setEnabled(true);

t.setTabSize(tabSize);

}

public Component getListCellRendererComponent(JList list, Object value,

int index, boolean isSelected, boolean cellHasFocus) {

if (isSelected) {

setForeground(Color.white);

setBackground(new Color(0, 0, 139));

t.setForeground(Color.white);

t.setBackground(new Color(0, 0, 139));

cb.setForeground(Color.white);

cb.setBackground(new Color(0, 0, 139));

} else {

setForeground(null);

setBackground(null);

t.setForeground(null);

t.setBackground(null);

cb.setForeground(null);

cb.setBackground(null);

}

Item item = (Item) value;

if(item.isChecked())

cb.setSelected(true);

else

cb.setSelected(false);

t.setText(item.getName());

add(t);

add(cb);

return this;

}

}

}

yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 11

> I can't change the value of the JCheckBox to true/false for the list:

Thats correct. A renderer is not a real component, so there is nothing to click on. A JList does not support the concept off editing the value of a cell.

A JTable uses a renderer for displaying each cell and an editor for changing the value of a cell. How many times do you need to be told to use a table? Otherwise you have to rewrite the code of JList so it performs exactly like a table.

camickra at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...
# 12
OK, naybe I have no choice..I'm going to rewrite oll me code.Can I do D&D from one table to the ater?Can I filter table respectively to textfield as searching? I mean, with startWith method..Because, this what I created in the list.Thanks
yael800a at 2007-7-7 16:07:12 > top of Java-index,Desktop,Core GUI APIs...