Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Как обнаружить JCheckBox


Автор: maxius 10.7.2004, 14:24

Засунул в JTable JCheckBox-ы, как new Boolean(false).
Как обратится к ним чтобы отследить изменение их состояния?


Автор: maxius 10.7.2004, 14:41
Отвечу сам себе.

можно обратиться непосредственно к ячейке массива и оттуда следить.


Может еще как-то можно???

Автор: Domestic Cat 10.7.2004, 20:37
Луче всего создать свою TableModel и cell renderer, типа такого:
Код

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.awt.event.*;

public class Test extends JFrame
{
   JTable table;
   MyTableModel mtm;
   
   public Test()
   {
       table = new JTable(new MyTableModel());
       table.setDefaultRenderer(Company.class, new CompanyRenderer());
       
       JPanel panel = new JPanel();
       panel.add(table);
       
       getContentPane().add(panel, BorderLayout.CENTER);
       
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       
       setSize(300, 300);
       setLocationRelativeTo(null);
       setVisible(true);
   }
   
   public static void main(String [] arguments)
   {
       new Test();
   }
}

class Company
{
   private boolean isGood;
   public Company(boolean isGood)
   {
       this.isGood = isGood;
   }
   
   public boolean isGood()
   {
       return isGood;
   }
   
   public void setGood(boolean isGood)
   {
       this.isGood = isGood;
   }
   
   public String toString()
   {
       return "" + isGood();
   }
}

class CompanyRenderer extends JCheckBox implements TableCellRenderer
{
   public CompanyRenderer()
   {
       super("Company");
   }
   
   public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected,
   boolean hasFocus, int row, int column)
   {
       boolean isGood = ((Company) value).isGood();
       setSelected(isGood);
       
       return this;
   }
}

class MyTableModel extends AbstractTableModel
{
   Object[] data [] = {{"Sun Microsystems", new Company(true)} , {"Microsoft", new Company(false)}};
   
   String headers[] = {"Company", "Description"};
   
   Class[] columnClasses = {String.class, Company.class};
   
   MyTableModel()
   {
       // empty
   }
   
   public Class getColumnClass(int column)
   {
       return columnClasses[column];
   }
   
   public String getColumnName(int column)
   {
       return headers[column];
   }
   
   public int getColumnCount()
   {
       return data[0].length;
   }
   
   public int getRowCount()
   {
       return data.length;
   }
   
   public Object getValueAt(int row, int column)
   {
       return data[row][column];
   }
}



Тогда можно дописать метод getData() и просмотреть значения boolean'ов, если нужно.
Если речь о том что нужно сразу же реагировать на изменение состояния check box'ov,
то добавьте ChangeListener к нужным обeктам (Company в примере).

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)