import java.awt.*; import java.awt.event.*; import java.io.FileInputStream; import java.io.ObjectInputStream; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; /** * @author Andreas Spirka, Sven Eisenhauer * * Klasse zum Anzeigen aller Tiere * */ public class AlleTiereDlg extends AbstractMyJDlg implements ListSelectionListener,WindowListener { Container c; JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); int counter = 0; User actUser=thv.getActUser(); UserCon users; TierCon tiere=thv.getTierCon(); JScrollPane scrollPane = new JScrollPane(); // Scroll-Pane JTable table = new JTable(); // Tabelle JPanel jPanelSouth = new JPanel(); JLabel jLabelSelection = new JLabel(); JTextField jTextFieldAuswahl = new JTextField(20); String fName = new String ("user.dat"); JButton delB = new JButton("Löschen"); JButton detailB = new JButton("Details"); JButton saveB = new JButton("Speichern"); JButton abbrB = new JButton("Abbrechen"); JComboBox comboBox = new JComboBox(Tier.TIERARTEN); JComboBox comboBoxG = new JComboBox(Tier.GESCHLECHT); JComboBox comboBoxMA = new JComboBox(); JCheckBox checkB = new JCheckBox(); JTextField tf = new JTextField(); JTextField editJTFint = new JTextField(); JTextField editJTF = new JTextField(); DefaultCellEditor cellEd = new DefaultCellEditor(editJTF); DefaultCellEditor cellEdint = new DefaultCellEditor(editJTFint); DefaultCellEditor comboEd = new DefaultCellEditor(comboBox); DefaultCellEditor comboEdG = new DefaultCellEditor(comboBoxG); DefaultCellEditor comboEDMA = new DefaultCellEditor(comboBoxMA); DefaultCellEditor checkBEd = new DefaultCellEditor(checkB); String[] UserNames; /** * Konstruktor */ public AlleTiereDlg() { editJTFint.setInputVerifier(new IntVerifier()); setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE ); ButtonListener2 bl = new ButtonListener2(this); c = getContentPane(); c.setLayout(borderLayout1); setSize(new Dimension(900, 400)); setTitle("Alle Tiere"); jPanelSouth.setBorder(BorderFactory.createEtchedBorder()); jLabelSelection.setText("Auswahl"); jTextFieldAuswahl.setText("kein Tier ausgewählt"); c.add( scrollPane, java.awt.BorderLayout.CENTER ); scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ); scrollPane.getViewport().add( table ); // Table in Scroll-Pane c.add(jPanelSouth, java.awt.BorderLayout.SOUTH); jPanelSouth.add(delB); jPanelSouth.add(detailB); jPanelSouth.add(abbrB); delB.addActionListener(bl); abbrB.addActionListener(bl); detailB.addActionListener(bl); detailB.setActionCommand("detail"); delB.setActionCommand("Löschen"); abbrB.setActionCommand("Abbrechen"); table.setModel( tiere ); // Model für Tabelle setzen // Einzelauswahl table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); ListSelectionModel lsm = table.getSelectionModel(); lsm.addListSelectionListener( this ); // TableModelListener regitrieren table.setCellSelectionEnabled(true); //table.setDoubleBuffered(true); try { FileInputStream fis = new FileInputStream(fName); ObjectInputStream ois = new ObjectInputStream(fis); users = (UserCon) ois.readObject(); ois.close(); fis.close(); UserNames=new String[users.size()]; for (int i=0;i= 0) tiere.removeTier( (Tier) tiere.elementAt( selRow ) ); } else if(e.getActionCommand().equals("detail")) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); int selRow = table.getSelectedRow(); if (selRow >= 0) { new TierDialog( selRow ); } } } /* (non-Javadoc) * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent) */ public void valueChanged( ListSelectionEvent e ) { /* in der Hörsaalübung kam es hier zum Crash, wenn eine Zeile * ausgewählt war und eine neue hinzugefügt wurde. In diesem * Fall ist die erste und zuletzt ausgewählte Zeile identisch. * Das wir hier abgefangen. */ /*if ( e.getFirstIndex() == e.getLastIndex() ) { jTextFieldAuswahl.setText( "kein Tier ausgewählt"); return; } String name = (String) table.getValueAt(table.getSelectedRow(), 1); String tierart = (String) table.getValueAt(table.getSelectedRow(), 2); this.jTextFieldAuswahl.setText( name + " (" + tierart + ")" ); */ } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent) */ public void windowClosing(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); dispose(); } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent) */ public void windowClosed(WindowEvent e) { } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent) */ public void windowOpened(WindowEvent e) { } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent) */ public void windowIconified(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); table.clearSelection(); } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent) */ public void windowDeiconified(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); table.clearSelection(); } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent) */ public void windowActivated(WindowEvent e) { } /* (non-Javadoc) * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent) */ public void windowDeactivated(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); table.clearSelection(); } /** * @param e */ public void windowGainedFocus(WindowEvent e) { } /** * @param e */ public void windowLostFocus(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); table.clearSelection(); } /** * @param e */ public void windowStateChanged(WindowEvent e) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } table.removeEditor(); table.clearSelection(); } }// end class AlleTiereDlg