import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class TableListDemo extends JFrame {
private ListTableModel model;
private int start, end;
public TableListDemo() {
List<Integer> data = new ArrayList<Integer>();
for (int i=1; i<32; i++)
data.add(i);
model = new ListTableModel(data);
JTable table = new JTable(model);
this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
JPanel panel = new JPanel(new GridLayout(1, 3));
final JSpinner spinner1 = new JSpinner();
final JSpinner spinner2 = new JSpinner();
spinner1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
start = Integer.parseInt(spinner1.getValue().toString());
if (start <= end)
model.fireTableDataChanged();
else
spinner2.setValue(start);
}
});
panel.add(spinner1);
spinner2.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
end = Integer.parseInt(spinner2.getValue().toString());
if (end >= start)
model.fireTableDataChanged();
else
spinner1.setValue(end);
}
});
panel.add(spinner2);
JButton button = new JButton("DEL");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
model.removeIntervall(start, end);
}
});
panel.add(button);
this.getContentPane().add(panel, BorderLayout.SOUTH);
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value!=null) {
int intValue = Integer.parseInt(value.toString());
if (intValue>=start && intValue<=end)
this.setBackground(Color.RED);
else
this.setBackground(Color.WHITE);
}
return this;
}
});
}
class ListTableModel extends AbstractTableModel {
private List<Integer> data = new ArrayList<Integer>();
public ListTableModel(List<Integer> data) {
this.data = data;
}
public void removeIntervall(int start, int end) {
for(int i = data.size()-1; i>=0; i--) {
System.out.println(data.get(i));
if (data.get(i)>=start && data.get(i)<=end)
data.remove(i);
}
this.fireTableDataChanged();
}
public int getColumnCount() {
return 7;
}
public int getRowCount() {
return data.size()/getColumnCount() + (data.size()%getColumnCount()>0?1:0);
}
public Object getValueAt(int rowIndex, int columnIndex) {
int index = rowIndex*getColumnCount() + columnIndex;
if (index >= data.size())
return null;
return data.get(index);
}
}
public static void main(String[] s) {
JFrame frame = new TableListDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}