import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableRowSorter;
public class testFrame extends JFrame {
private JPanel panel;
private JTable channelTable;
private channelTableModel channelModel;
private JScrollPane channelScrollpane;
private TableRowSorter sorter;
private JCheckBox sort;
testFrame(){
panel = new JPanel();
add(panel);
channelModel = new channelTableModel();
channelTable = new JTable(channelModel);
channelTable.setName("channel");
channelScrollpane = new JScrollPane(channelTable);
channelTable.setPreferredScrollableViewportSize(new Dimension(500,300));
channelTable.setDefaultRenderer(String.class,new CenterAlignmentCellRenderer());
channelTable.setDefaultRenderer(JRadioButton.class,new CenterAlignmentCellRenderer());
panel.add(channelScrollpane);
sorter = new TableRowSorter(channelModel);
channelTable.setRowSorter(sorter);
channelTable.getColumn("allowed").setCellRenderer(new RadioButtonRenderer());
channelTable.getColumn("allowed").setCellEditor(new RadioButtonEditor(new JCheckBox()));
channelTable.getColumn("not recommended").setCellRenderer(new RadioButtonRenderer());
channelTable.getColumn("not recommended").setCellEditor(new RadioButtonEditor(new JCheckBox()));
channelTable.getColumn("not allowed").setCellRenderer(new RadioButtonRenderer());
channelTable.getColumn("not allowed").setCellEditor(new RadioButtonEditor(new JCheckBox()));
/**
* Der hier ist wichtig, da sonst die radiobuttons nicht richtig aktualsisiert werden
*/
channelTable.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
channelTable.repaint();
/*if (selectedRow != -1){
//analyzeChannelTableAndSetModeTable(String.valueOf(selectedRow));
}*/
}
});
for (int x = 0; x <= 600; x++){
channelModel.addEntry(x, String.valueOf(x), new JRadioButton("",getRandomEntry()), new JRadioButton("",getRandomEntry()), new JRadioButton("",getRandomEntry()));
}
for (int i = 0; i <= 600; i++){
ButtonGroup group1 = new ButtonGroup();
((JRadioButton)channelModel.getValueAt(i,1)).setBackground(Color.WHITE);
((JRadioButton)channelModel.getValueAt(i,2)).setBackground(Color.WHITE);
((JRadioButton)channelModel.getValueAt(i,3)).setBackground(Color.WHITE);
group1.add((JRadioButton)channelModel.getValueAt(i,1));
group1.add((JRadioButton)channelModel.getValueAt(i,2));
group1.add((JRadioButton)channelModel.getValueAt(i,3));
//group1.add((JRadioButton)modeModel.getValueAt(i,4));
}
sort = new JCheckBox("filtern");
sort.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setFilterForChannelTable(sort.isSelected());
}
});
panel.add(sort);
}
public void setFilterForChannelTable(boolean b){
sorter.setRowFilter(RowFilter.regexFilter(new String(".*")));
System.out.println(b);
String regex = ".*";
if (b){
regex = "(^[0-9]{1,2}$)|([01456789][0-9][0-9])";
}
else {
regex = ".*";
}
sorter.setRowFilter(RowFilter.regexFilter(regex));
System.out.println(regex);
System.out.println("Einträge übrig: " +channelTable.getRowCount());
}
/**
* @param args
*/
public static void main(String[] args) {
testFrame test = new testFrame();
test.setVisible(true);
}
public boolean getRandomEntry(){
//System.out.println((Math.random() > 0.5)? "X":"");
return (Math.random() > 0.5)? false:true;
}
}
class channelTableModel extends AbstractTableModel {
private String[] columnNames = { "no", "allowed", "not recommended", "not allowed"};
private List data = new LinkedList<Object[][]>();
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
Object[] value = (Object[]) data.get(row);
return value[col];
}
public void removeEntry(int row) {
data.remove(row);
fireTableDataChanged();
}
public void removeAll() {
data.clear();
fireTableDataChanged();
}
public void addEntry(int row, String number, JRadioButton allowed, JRadioButton not_recommended, JRadioButton not_allowed) {
data.add(row, new Object[] { number, allowed, not_recommended, not_allowed});
fireTableDataChanged();
}
public void editEntry(int row, int col, Object newvalue) {
if (row >= 0) {
Object[] value = (Object[]) data.get(row);
value[col] = newvalue;
data.set(row, value);
fireTableDataChanged();
}
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col >= 1 && col <= 4) {
return true;
} else {
return false;
}
}
}class CenterAlignmentCellRenderer extends DefaultTableCellRenderer {
Color uneditable = new Color(238, 238, 238);
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (table.getName().equals("mode")){
if (column == 0 | column == 4) {
cell.setBackground(uneditable);
}
else {
cell.setBackground(Color.white);
}
}
else if (table.getName().equals("channel")){
if (column == 0){
cell.setBackground(uneditable);
}
else {
cell.setBackground(Color.white);
}
}
((JLabel) cell).setHorizontalAlignment(SwingConstants.CENTER);
return cell;
}
}