Hallo,
Ich möchte ein JTable mit Zellen, deren Editierbarkeit komplett von Data abhängen, d.h. wenn ich das Model mit Daten fülle, sollte auch für jede Zelle diese Eigenschaft gesetzt werden. Es sollte auch für Column 4 eine automatische Berechnung von Wert Column 3*Berechnung von Wert Column 2 gemacht werden. Wenn ich in meine Zelle klicke, dann bekomme ich den Wert isCellEditable true, aber trotzdem kann ich nicht editieren.
Hat jemand eine Idee?
Vielen Dank!!
Ich möchte ein JTable mit Zellen, deren Editierbarkeit komplett von Data abhängen, d.h. wenn ich das Model mit Daten fülle, sollte auch für jede Zelle diese Eigenschaft gesetzt werden. Es sollte auch für Column 4 eine automatische Berechnung von Wert Column 3*Berechnung von Wert Column 2 gemacht werden. Wenn ich in meine Zelle klicke, dann bekomme ich den Wert isCellEditable true, aber trotzdem kann ich nicht editieren.
Hat jemand eine Idee?
Vielen Dank!!
Java:
/*code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TestFaktur.java
*
* Created on 14.05.2009, 11:52:54
*/
package gui;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
/**
*
* @author cbodnariu
*/
public class TestFaktur extends javax.swing.JPanel {
/** Creates new form TestFaktur */
private String[] columnNames = { "Dienst. Nr",
"Dienst. Name",
"Anzahl",
"Preis",
"Total"};
ArrayList data = new ArrayList();
private MyTableModel myModel=new MyTableModel();
class RowData {
CellItem dlNr;;
CellItem name;
CellItem anzahl;
CellItem preis;
CellItem total;
public RowData(CellItem dlNr,CellItem name,CellItem anzahl,CellItem preis,CellItem total ){
this.dlNr=dlNr;
this.name=name;
this.anzahl=anzahl;
this.preis=preis;
this.total=total;
}
public CellItem getDlNr(){
return dlNr;
}
public CellItem getName(){
return name;
}
public CellItem getAnzahl(){
return anzahl;
}
public CellItem getPreis(){
return preis;
}
public CellItem getTotal(){
return total;
}
public void setDlNr(CellItem l){
this.dlNr=l;
}
public void setName(CellItem l){
this.name=l;
}
public void setAnzahl(CellItem l){
this.anzahl=l;
}
public void setPreis(CellItem l){
this.preis=l;
}
public void setTotal(CellItem l){
this.total=l;
}
public CellItem getData(int col) {
CellItem returnWert=null;
if(col==0){
returnWert= dlNr;
}
if(col==1){
returnWert= name;
}
if(col==2){
returnWert= anzahl;
}
if(col==3){
returnWert= preis;
}
if(col==4){
returnWert= total;
}
return returnWert;
}
}
class CellItem {
String item;
boolean editable;
public CellItem(String item, boolean editable){
this.item=item;
this.editable=editable;
}
public CellItem(){
}
public String getItem(){
return item;
}
public boolean getEditable(){
return editable;
}
public void setItem(String l){
this.item=l;
}
public void setEditable(boolean l){
this.editable=l;
}
@Override
public String toString(){
return item;
}
}
public TestFaktur() {
myModel.addRow(new CellItem("1",false), new CellItem("1.Entsorgung",false), new CellItem("100",true), new CellItem("20",false),new CellItem("",false));
myModel.addRow(new CellItem("999",true), new CellItem("",true), new CellItem("",true), new CellItem("",true),new CellItem("",false));
initComponents();
setUpComboBox(jTable1, jTable1.getColumnModel().getColumn(0));
}
public void setUpComboBox(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem(new CellItem("1",false));
comboBox.addItem(new CellItem("2",false));
comboBox.addItem(new CellItem("3",false));
comboBox.addItem(new CellItem("4",false));
comboBox.addItem(new CellItem("999 ",false));
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TestFaktur");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TestFaktur newContentPane = new TestFaktur();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
//listenSocket();
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
class MyTableModel extends AbstractTableModel {
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
public void addRow(CellItem s1, CellItem s2, CellItem s3,CellItem s4,CellItem s5)
{
data.add(new RowData(s1,s2,s3,s4,s5));
}
public RowData getRowValue(int row)
{
return (RowData)data.get(row);
}
public CellItem getValueAt(int row, int col)
{
return getRowValue(row).getData(col);
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
@Override
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
CellItem value_new=(CellItem)getValueAt(row,col);
System.out.println("editable:"+value_new.getEditable());
return(value_new.getEditable());
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
CellItem value_Cell;
value_Cell=(CellItem)value;
System.out.println("Setting value at " + row + "," + col
+ " to " + value_Cell
+ " (an instance of "
+ value.getClass() + ")");
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
//wert setzen
getValueAt(row,col).setItem(value_Cell.getItem());
try{
if(col==3 || col==2){
// System.out.println("col 3:"+Double.valueOf((String)data[row][3]));
// System.out.println("col 2:"+Double.valueOf((String)data[row][2]));
// neuer wert für column 4 berechnen
getValueAt(row,4).setItem(String.valueOf(Double.valueOf(getValueAt(row,3).getItem())* Double.valueOf(getValueAt(row,2).getItem())));
//update für column 4 zwingen
setValueAt(getValueAt(row,4),row,4);
}
}catch(Exception ex){
// wenn exception bei berechnung, weil z.b. noch kein wert für column 2 oder 3 gesetzt ist
System.out.println("Error bei Number");
// alte wert von column 4 behalten
setValueAt(getValueAt(row,4),row,4);
}
fireTableCellUpdated(row, col);
System.out.println("New value of data:");
printDebugData();
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " +getValueAt(i,j) );
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jSeparator1 = new javax.swing.JSeparator();
jButton1 = new javax.swing.JButton();
jTable1.setAutoCreateRowSorter(true);
jTable1.setModel(myModel);
jTable1.setRowHeight(20);
jScrollPane1.setViewportView(jTable1);
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 625, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(21, Short.MAX_VALUE))
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(409, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(174, 174, 174))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
*/
Zuletzt bearbeitet von einem Moderator: