Guten Abend,
ich versuche mich gerade in Tabellen, und möchte einen booleschen Wert ändern, was ich leider nicht hinbekomme. Vermutlich liegt der Fehler im TableModel, ich wäre hoch erfreut, wenn sich das jemand ansähe.
TableModel:
ShowTable:
FileObject:
..Und der Starter (Demo.java)
ich versuche mich gerade in Tabellen, und möchte einen booleschen Wert ändern, was ich leider nicht hinbekomme. Vermutlich liegt der Fehler im TableModel, ich wäre hoch erfreut, wenn sich das jemand ansähe.
TableModel:
Java:
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
class TableModel extends AbstractTableModel implements TableModelListener
{
public int getRowCount()
{
return Demo.fileObjList.size();
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Object getValueAt(int row, int col) {
switch(col) {
case 0: return Demo.fileObjList.get(row).getSize();
case 1: return Demo.fileObjList.get(row).getPath();
case 2: return Demo.fileObjList.get(row).getName();
case 3: return Demo.fileObjList.get(row).getType();
case 4: return Demo.fileObjList.get(row).getDel();
case 5: return Demo.fileObjList.get(row).getMov();
case 6: return Demo.fileObjList.get(row).getPack();
}
return null;
}
public String getColumnName(int col) {
switch(col) {
case 0: return "Size";
case 1: return "Path";
case 2: return "Name";
case 3: return "Type";
case 4: return "del";
case 5: return "mov";
case 6: return "pack";
default: return null;
}
}
public Class getColumnClass(int col) {
switch(col) {
case 0: return Float.class;
case 1: return String.class;
case 2: return String.class;
case 3: return String.class;
case 4: return Boolean.class;
case 5: return Boolean.class;
case 6: return Boolean.class;
default: return null;
}
}
public void setValueAt(Object value, int row, int col) {
Demo.fileObjList.get(row).change(value, col);
}
@Override
public void tableChanged(TableModelEvent e) {
fireTableChanged(e);
}
}
ShowTable:
Java:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
//import javax.swing.table.TableModel;
public class ShowTable {
static JFrame frame = new JFrame("Table");
static JTable table = null;
public static void start() {
table = new JTable();
table.setModel(new TableModel());
frame.getContentPane().add(table, BorderLayout.CENTER);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel();
JScrollPane scroller = new JScrollPane(table);
panel.add(scroller);
frame.add(panel);
}
}
FileObject:
Java:
public class FileObject {
float size;
String path;
String name, type;
boolean del, mov, pack;
public FileObject(float size, String path, String name, String type, boolean del, boolean mov, boolean pack) {
this.size = size;
this.path=path;
this.name=name;
this.type=type;
this.del=del;
this.mov=mov;
this.pack=pack;
}
float getSize() {
return size;
}
public void setSize(float size) {
this.size=size;
}
String getPath() {
return path;
}
public void setPath(String path) {
this.path=path;
}
String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
String getType() {
return type;
}
public void setType(String type) {
this.type=type;
}
boolean getDel() {
return del;
}
public void setDel(boolean del) {
this.del=del;
}
boolean getMov() {
return mov;
}
public void setMov(boolean mov) {
this.mov=mov;
}
boolean getPack() {
return pack;
}
public void setPack(boolean pack) {
this.pack=pack;
}
public void change(Object value, int col) {
if (col==4) {
boolean b = (Boolean)value;
setDel(b);
} else if (col==5) {
boolean b = (Boolean)value;
setMov(b);
}else if (col==6) {
boolean b = (Boolean)value;
setPack(b);
}
}
}
..Und der Starter (Demo.java)
Java:
import java.util.ArrayList;
import java.util.List;
public class Demo {
static List<String> fileList;
static List<FileObject> fileObjList;
public static void main(String args[]) {
FileObject fo1 = new FileObject(0.003f,"~/workspace/JTableTest/src", "TableModel.java", ".java", false, false, false);
FileObject fo2 = new FileObject(0.002f, "~/workspace/JTableTest/src", "FileObject.java", ".java", false, false, false);
fileObjList=new ArrayList<FileObject>();
fileObjList.add(fo1);
fileObjList.add(fo2);
ShowTable.start();
}
}