import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.Vector; // Beispiel einer JList mit einem Vector // Eine ArrayList ist nicht erlaubt // mit Klasse CPruefung public class Liste_Labor4 extends JFrame { JList jlist; // Anzeige der Daten Vector vliste; // Speichert die Daten in einer Liste //Frame konstruieren public Liste_Labor4() { setSize(620, 520); setTitle("Liste_Labor4"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setGUI(); } private void setGUI() { JButton bnInsert = new JButton("Insert"); JButton bnDelete = new JButton("Löschen"); JButton bnESC = new JButton("Ende"); this.getContentPane().setLayout ( new GridBagLayout() ); // Liste setData(); // create + insert jlist = new JList(vliste); // Konstruktor mit der Vector-Liste this.getContentPane().add(new JScrollPane(jlist), new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,20,0,20), 0, 0)); jlist.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); /* SINGLE_SELECTION SINGLE_INTERVAL_SELECTION MULTIPLE_INTERVAL_SELECTION */ JPanel panelBn = new JPanel(); panelBn.setLayout( new FlowLayout() ); panelBn.add(bnInsert); panelBn.add(bnDelete); panelBn.add(bnESC); this.getContentPane().add(panelBn, new GridBagConstraints(0, 1, 4, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(20,0,20,0), 0, 0)); bnInsert.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnInsert_click(); } }); bnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnDelete_click(); } }); bnESC.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bnESC_click(); } }); jlist.setFont( new Font("Arial", Font.BOLD,18) ); bnInsert.setFont( new Font("Arial", Font.BOLD,18) ); bnDelete.setFont( new Font("Arial", Font.BOLD,18) ); bnESC.setFont( new Font("Arial", Font.BOLD,18) ); } // create void bnInsert_click() { CPruefung prf = new CPruefung("Grundlagen der Informatik 1", 1.3); vliste.add(prf); jlist.setListData( vliste ); jlist.updateUI(); } void bnDelete_click() { CPruefung prf; // ist ein Eintrag ausgewaehlt? int index = jlist.getSelectedIndex(); if (index<0) { Error( "Eintrag loeschen","Bitte markieren Sie einen Eintrag"); } else { prf = vliste.get(index); // hole die Pruefung Message( "Eintrag loeschen",prf.toString()); // Testausgabe // Erst in Liste loeschen vliste.remove(index); // Loeschen aus dem Vector // JList aktualisieren mit neuer Liste jlist.setListData( vliste ); // der Vector wird nun WIEDER in die JList eingefuegt jlist.updateUI(); // Neuzeichnen } } public void Error( String sTitle, String sMessage) { JOptionPane.showConfirmDialog(null,sMessage,sTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); // Symbol } // Error public void Message( String sTitle, String sMessage) { JOptionPane.showConfirmDialog(null,sMessage,sTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); // Symbol } // Message void bnESC_click() { System.exit(0); } // Eintragen private void setData() { CPruefung prf; vliste = new Vector(); prf = new CPruefung( "Grundlagen der Informatik 1", 1.3); vliste.add(prf); prf = new CPruefung( "Grundlagen der Informatik 2", 2.3); vliste.add(prf); prf = new CPruefung( "Grundlagen der Informatik 3", 1.7); vliste.add(prf); prf = new CPruefung( "Mathematik 1", 2.3); vliste.insertElementAt(prf,0); prf = new CPruefung( "Mathematik 2", 1.0); vliste.add(prf); prf = new CPruefung( "Mathematik 3", 2.0); vliste.add(prf); prf = new CPruefung( "Physik 1", 2.3); vliste.add(prf); prf = new CPruefung( "Physik 2", 3.3); vliste.add(prf); prf = new CPruefung( "Elektrotechnik 1", 3.0); vliste.add(prf); prf = new CPruefung( "Elektrotechnik 2", 5.0); vliste.add(prf); } // setData public static void main(String[] args) { Liste_Labor4 frame = new Liste_Labor4(); frame.setVisible(true); } } class CPruefung { public static int typ=0; private String _Pruefung; private double _Note; public CPruefung() { this( "keinen Namen", 5.0); } public CPruefung( String Pruefung, double Note) { _Pruefung = Pruefung; _Note = Note; } public String getPruefung() { return _Pruefung; } public void setPruefung(String Pruefung) { _Pruefung = Pruefung; } public Double getNote() { return new Double (_Note); } public void setNote(double Note) { _Note = Note; } public String toString() { return "Prüfung: "+_Pruefung+" Note: "+_Note; } } // CPruefungen /* public String toString() { switch (typ) { case 0: return "Prüfung: "+_Pruefung; case 1: return "Note: "+_Note; case 2: return "Prüfung: "+_Pruefung+" Note: "+_Note; case 3: return "Note: "+_Note+" Prüfung: "+_Pruefung; default: return ""; } } public String toString() { switch (typ) { case 0: return "Prüfung: "+_Pruefung; case 1: return "Note: "+_Note; case 2: return "Prüfung: "+_Pruefung+" Note: "+_Note; case 3: return "Note: "+_Note+" Prüfung: "+_Pruefung; default: return ""; } } */