Klick auf JButton in JTable -> Dialog aufrufen, Daten speichern

mas666

Mitglied
Hallo zusammen,

Ich habe eine JTable mit einer Splate, wo JButtons dargestellt werden. Mit Klick auf den JButton öffne ich einen Dialog. Die Daten aus dem Dialog möchte ich in die Tabellenzeile speichern.


Ich tue folgendes:

Ich setze den CellEditor für die gewünschte Spalte:
Code:
table.getColumnModel().getColumn(0).setCellEditor( new ButtonEditor( new JCheckBox(), parent, this ) );

Der ButtonEditor sieht folgendermassen aus:
Code:
import java.awt.Component;
...
import ch.arocom.leonardo.saveable.Touchpanel;

public class ButtonEditor extends DefaultCellEditor {
	  protected JButton button;
	  private String    label;
	  private boolean   isPushed;
	  private MainWindow parent;
	  private TouchpanelTable panel;
	 
	  public ButtonEditor(JCheckBox checkBox, MainWindow parent, TouchpanelTable panels) {
	    super(checkBox);
	    this.parent = parent;
	    button = new JButton();
	    button.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        fireEditingStopped();
	      }
	    });
	  }
	 
	  public Component getTableCellEditorComponent(JTable table, Object value,
	                   boolean isSelected, int row, int column) {
	    if (isSelected) {
	      button.setForeground(table.getSelectionForeground());
	      button.setBackground(table.getSelectionBackground());
	    } else{
	      button.setForeground(table.getForeground());
	      button.setBackground(table.getBackground());
	    }
	    label = (value ==null) ? "" : value.toString();
	    button.setText( label );
	    isPushed = true;
	    return button;
	  }
	 
	  public Object getCellEditorValue() {
	    if (isPushed)  {
	    	if(!button.getText().equals("Nicht verfügbar")){
	    		IPConfigurationDialog ipcd = new IPConfigurationDialog( parent, panel );
	    		ipcd.setVisible(true);
	    	}
	    }
	    isPushed = false;
	    return new String( label ) ;
	  }
	   
	  public boolean stopCellEditing() {
	    isPushed = false;
	    return super.stopCellEditing();
	  }
	 
	  protected void fireEditingStopped() {
	    super.fireEditingStopped();
	  }
	}

Für die JTable habe ich ein Model:
Code:
import javax.swing.JButton;
..
import ch.arocom.leonardo.saveable.entries.TouchpanelEntry;

public class TouchpanelTableModel extends AbstractTableModel{
	
	public static final int TABLE_SIZE = 6;
	
	private Touchpanel panel;
  
	public TouchpanelTableModel(Touchpanel panel){
		this.panel = panel;
	}
	
	public void addPanelEntry(TouchpanelEntry entry){
	}
	
	public void removeRow(int row){
	}
	
	public void removeRows(int rowStart, int rowEnd){
	}
	
	public void moveRow(int row, int shiftTo){
	}
	
	public Class getColumnClass(final int col) {
		switch( col ){
		case 0: return String.class;
		case 1: return String.class;
		case 2: return Integer.class;
		case 3: return Integer.class;
		case 4: return Integer.class;
		case 5: return IpSettings.class;
		default: return null;
		}
    }
   
    public boolean isCellEditable(final int row, final int col) {
    }

	public int getColumnCount() {
		return TABLE_SIZE;
	}

	public String getColumnName(int columnIndex) {
	}

	public int getRowCount() {
		return panel.getNumLines();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		TouchpanelEntry panelEntry = (TouchpanelEntry)panel.getLine( rowIndex );
		
		switch( columnIndex ){
		case 0: return panelEntry.getName();
		case 1: return panelEntry.getType();
		case 2: return new Integer( panelEntry.getDevice().getDeviceNumber() );
		case 3: return new Integer( panelEntry.getDevice().getDevicePort() );
		case 4: return new Integer( panelEntry.getDevice().getDeviceSystem() );
		case 5: return new IpSettings( panelEntry.getIp() );
		default: return null;
		}
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		TouchpanelEntry panelEntry = (TouchpanelEntry)panel.getLine( rowIndex );
		AxLinkDevice device;
		
		switch( columnIndex ){
		case 0: 	// NAME
			panelEntry.setName( (String)aValue );
			break;
		case 1: 	// TYP
			panelEntry.setType( (TouchpanelType)aValue );
			break;
		case 2: 	// DEVICE
			if(aValue!=null){
				device = new AxLinkDevice();
       	 		device.setDeviceNumber( ((Integer)aValue).intValue() );
       	 		device.setDevicePort(panelEntry.getDevice().getDevicePort());
       	 		device.setDeviceSystem(panelEntry.getDevice().getDeviceSystem());
       	 		panelEntry.setDevice(device);
			}
       		break;
		case 3: 	// PORT
			if(aValue!=null){
				device = new AxLinkDevice();
       	 		device.setDeviceNumber(panelEntry.getDevice().getDeviceNumber());
       	 		device.setDevicePort( ((Integer)aValue).intValue() );
       	 		device.setDeviceSystem(panelEntry.getDevice().getDeviceSystem());
       	 		panelEntry.setDevice(device);
			}
       		break;
		case 4: 	// SYSTEM
			if(aValue!=null){
				device = new AxLinkDevice();
       	 		device.setDeviceNumber(panelEntry.getDevice().getDeviceNumber());
       	 		device.setDevicePort(panelEntry.getDevice().getDevicePort());
       	 		device.setDeviceSystem( ((Integer)aValue).intValue() );
       	 		panelEntry.setDevice(device);
			}
       		break;
		case 5:
			// TODO set IP Conf value
			break;
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}
	
	public TouchpanelEntry getRow(int k) {
		return panel.getLine(k);
	}
}

Nun fehlt mir ein bisschen der Ansatz. Wie kriege ich die Daten vom Dialog dazu, dem Model übergeben zu werden? Das muss ja wohl irgendwo im ButtonEditor passieren, ich seh aber nicht genau wo...

Einen Tipp?

Gruss
mas
 
Zurück