tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
1
ZUGRIFFE
2118
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    mas666 mas666 ist offline Mitglied Silber
    Registriert seit
    Jul 2005
    Beiträge
    57
    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 :
    1
    
    table.getColumnModel().getColumn(0).setCellEditor( new ButtonEditor( new JCheckBox(), parent, this ) );

    Der ButtonEditor sieht folgendermassen aus:
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    
    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
     

  2. #2
    egal Tutorials.de Gastzugang
    ___****___
     

Ähnliche Themen

  1. Swing JButton in JTable-Zelle: Event-Problem
    Von pcworld im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 0
    Letzter Beitrag: 04.11.10, 21:22
  2. JButton bei Klick aktualisieren
    Von SkateboardP im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 2
    Letzter Beitrag: 01.03.10, 12:18
  3. JTable in JTable mit JButton
    Von indianerrostock im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 1
    Letzter Beitrag: 25.02.09, 16:29
  4. JButton klick simulieren
    Von Busi im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 16
    Letzter Beitrag: 22.11.07, 10:33
  5. JButton in JTable
    Von Seibi im Forum Java
    Antworten: 2
    Letzter Beitrag: 17.12.06, 14:04