... aber es geht schon wieder ums JTable. Bin ein ziemlicher noob wenns um Swing geht und das JTable FAQ von Beni hat mir wirklich sehr geholfen (danke für die Arbeit . Jedenfalls soll die Applikation einfach Telefonbucheinträge verwalten. Bei der Anzeige hab ich zwei Renderer verwendet weil ich das mal so probieren wollte, einen für die normalen Einträge und einen für die Adresseinträge der die Daten in einer ComboBox darstellt. Mein Problem ist jetzt dass wenn ich eine Reihe selektiere... nur die ComboBox die grüne Farbmarkierung bekommt obwohl ich mit gedacht hätte dass es die restlichen Einträge auch grün werden da auch im LabelRenderer der Hintergrund grün gesetzt wird.
thx
Edit von L-ectron-X: Bitte Aussage kräftige Titel verwenden! Kein Mensch kann die Frage zuordnen wenn da steht: "Trau mich gar nicht zu fragen..."
thx
Code:
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class Node implements Serializable {
Adresse add;
Kontakt kon;
Node(Kontakt k, Adresse a) {
kon = k;
add = a;
}
public Kontakt getContact() {
return kon;
}
public Adresse getAdress() {
return add;
}
}
class Adresse implements Serializable {
String strasse, ort, staat;
Adresse( String s, String o, String st) {
strasse = s;
ort = o;
staat = st;
}
}
class Kontakt implements Serializable {
String name, vorname, nick, telefon, handy, email, email2, geb;
Kontakt( String n,
String v,
String ni,
String t,
String h,
String e,
String e2,
String g) {
name = n;
vorname = v;
nick = ni;
telefon = t;
handy = h;
email = e;
email2 = e2;
geb = g;
}
}
public class Bilder
{
public static void main( String args[] )
{
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createGUI();
}
});
}
public static void createGUI() {
UIManager.put("Label.font", "javax.swing.plaf.FontUIResource" +
"[family=dialog.bold,name=Monospace,style=bold,size=14]");
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame f = new JFrame();
final Container c = f.getContentPane();
c.setLayout(new GridBagLayout());
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setSize(900, 700);
f.setLocationRelativeTo(null);
f.setTitle("Bianca's Telefonbuch");
Vector data = loadData();
final Model model = new Model(data);
final JTable table = new JTable(model);
table.setRowHeight(20);
table.setDefaultRenderer(Adresse.class, new ComboRenderer(model));
table.setDefaultRenderer(String.class, new Renderer(model));
table.setDefaultEditor(Adresse.class, new ComboEditor(model));
table.setSelectionBackground(Color.BLUE);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Datei");
JMenuItem about = new JMenuItem("Über...");
JMenuItem contact = new JMenuItem("Kontakt hinzufügen");
JMenuItem contact2 = new JMenuItem("Kontakt löschen");
JMenuItem save = new JMenuItem("Speichern");
JMenuItem exit = new JMenuItem("Beenden");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
contact.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createDialog(f, model, false);
}
});
contact2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
model.delete(table);
}
});
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
model.save();
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(about);
menu.add(contact);
menu.add(contact2);
menu.add(save);
menu.addSeparator();
menu.add(exit);
bar.add(menu);
f.setJMenuBar(bar);
JButton button1 = new JButton("Speichern");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
model.save();
}
});
JButton button2 = new JButton("Kontakt hinzufügen");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
createDialog(f, model, false);
}
});
JButton button3 = new JButton("Kontakt löschen");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
model.delete(table);
}
});
JButton button4 = new JButton("Kontakt bearbeiten");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
createDialog(f, model, true);
}
});
c.add(new JScrollPane(table), new GridBagConstraints(0, 0, 4, 1, 1, 10, GridBagConstraints.NORTH,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
c.add(button1, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
c.add(button4, new GridBagConstraints(1, 1, 1, 1, 2, 0, GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
c.add(button3, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
c.add(button2, new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
f.setVisible(true);
}
public static void createDialog(JFrame f, final Model model, boolean changed) {
JDialog.setDefaultLookAndFeelDecorated(true);
final JDialog dialog = new JDialog();
Container p = dialog.getContentPane();
p.setLayout(new GridBagLayout());
JPanel c = new JPanel();
JPanel q = new JPanel();
q.setLayout(new FlowLayout(FlowLayout.CENTER));
c.setLayout(new GridBagLayout());
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
dialog.setLocation((d.width - 600) / 2, (d.height - 200) / 2);
dialog.setModal(true);
dialog.setSize(600, 300);
JButton button1 = new JButton(" OK ");
JButton button2 = new JButton("Abbrechen");
JPanel vor = new JPanel();
JLabel vor1 = new JLabel("Vorname");
final JTextField vor2 = new JTextField();
vor.setLayout(new GridLayout(2, 1));
vor.add(vor1);
vor.add(vor2);
JPanel nach = new JPanel();
JLabel nach1 = new JLabel("Nachname");
final JTextField nach2 = new JTextField();
nach.setLayout(new GridLayout(2, 1));
nach.add(nach1);
nach.add(nach2);
JPanel nick = new JPanel();
JLabel nick1 = new JLabel("Nickname");
final JTextField nick2 = new JTextField();
nick.setLayout(new GridLayout(2, 1));
nick.add(nick1);
nick.add(nick2);
JPanel tel = new JPanel();
JLabel tel1 = new JLabel("Telefon");
final JTextField tel2 = new JTextField();
tel.setLayout(new GridLayout(2, 1));
tel.add(tel1);
tel.add(tel2);
JPanel han = new JPanel();
JLabel han1 = new JLabel("Handy");
final JTextField han2 = new JTextField();
han.setLayout(new GridLayout(2, 1));
han.add(han1);
han.add(han2);
JPanel em = new JPanel();
JLabel em1 = new JLabel("Email");
final JTextField em2 = new JTextField();
em.setLayout(new GridLayout(2, 1));
em.add(em1);
em.add(em2);
JPanel ema = new JPanel();
JLabel ema1 = new JLabel("Email2");
final JTextField ema2 = new JTextField();
ema.setLayout(new GridLayout(2, 1));
ema.add(ema1);
ema.add(ema2);
JPanel ad = new JPanel();
JLabel ad1 = new JLabel("Strasse");
final JTextField ad2 = new JTextField();
ad.setLayout(new GridLayout(2, 1));
ad.add(ad1);
ad.add(ad2);
JPanel ad_ = new JPanel();
JLabel ad_1 = new JLabel("Wohnort");
final JTextField ad_2 = new JTextField();
ad_.setLayout(new GridLayout(2, 1));
ad_.add(ad_1);
ad_.add(ad_2);
JPanel ad__ = new JPanel();
JLabel ad__1 = new JLabel("Staat");
final JTextField ad__2 = new JTextField();
ad__.setLayout(new GridLayout(2, 1));
ad__.add(ad__1);
ad__.add(ad__2);
JPanel geb = new JPanel();
JLabel geb1 = new JLabel("Geburtsdatum");
final JTextField geb2 = new JTextField();
geb.setLayout(new GridLayout(2, 1));
geb.add(geb1);
geb.add(geb2);
c.add(vor, new GridBagConstraints(0, 0, 1, 1, 5, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(nach, new GridBagConstraints(1, 0, 1, 1, 5, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(nick, new GridBagConstraints(2, 0, 1, 1, 5, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(tel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(han, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(em, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(ema, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(ad, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(ad_, new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(ad__, new GridBagConstraints(2, 3, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
c.add(geb, new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 10, 0, 10), 0, 0));
q.add(button1);
q.add(button2);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Kontakt k = new Kontakt( vor2.getText(),
nach2.getText(),
nick2.getText(),
tel2.getText(),
han2.getText(),
em2.getText(),
ema2.getText(),
geb2.getText());
Adresse a = new Adresse(ad2.getText(), ad_2.getText(), ad__2.getText());
model.addItem(new Node(k, a));
dialog.dispose();
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dialog.dispose();
}
});
p.add(c, new GridBagConstraints(0, 0, 1, 1, 3, 3, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
p.add(q, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.PAGE_END,
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
dialog.setVisible(true);
}
public static Vector loadData() {
Vector data = null;
try {
FileInputStream fi = new FileInputStream("daten.dat");
ObjectInputStream ois = new ObjectInputStream(fi);
data = (Vector) ois.readObject();
ois.close();
}
catch (FileNotFoundException e) {
data = new Vector();
//data.add(new Kontakt( "", "", "Herzlich Willkommen", "",
//"", "", "", ""));
}
catch (Exception e) {
JOptionPane.showMessageDialog( null, "Fehler beim Laden der Daten" );
System.exit(0);
}
return data;
}
}
class Model implements TableModel {
private Vector contacts;
private Vector listeners = new Vector();
public Model(Vector data){
contacts = data;
}
public Vector getContacts() {
return contacts;
}
public void delete(JTable table) {
int index = table.getSelectedRow();
if (index != -1) contacts.removeElementAt(index);
TableModelEvent e = new TableModelEvent( this, index, index,
TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );
for( int i = 0, n = listeners.size(); i<n; i++ ) {
((TableModelListener)listeners.get( i )).tableChanged( e );
}
}
public void save() {
FileOutputStream fo;
try {
fo = new FileOutputStream("daten.dat");
ObjectOutputStream oos = new ObjectOutputStream(fo);
oos.writeObject(contacts);
oos.flush();
oos.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog( null, "Fehler beim Speichern der Daten" );
System.exit(0);
}
}
public void addItem(Node k) {
int index = contacts.size();
contacts.add(k);
TableModelEvent e = new TableModelEvent( this, index, index,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );
for( int i = 0, n = listeners.size(); i<n; i++ ) {
((TableModelListener)listeners.get( i )).tableChanged( e );
}
}
public int getColumnCount() {
return 7;
}
public int getRowCount() {
return contacts.size();
}
public String getColumnName(int column) {
switch(column) {
case 0: return "Nickname";
case 1: return "Telefon";
case 2: return "Handy";
case 3: return "Email";
case 4: return "Email2";
case 5: return "Adresse";
case 6: return "Geburtstag";
default: return null;
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
Kontakt k = ((Node) contacts.get(rowIndex)).getContact();
switch (columnIndex) {
case 0: return k.nick;
case 1: return k.telefon;
case 2: return k.handy;
case 3: return k.email;
case 4: return k.email2;
case 6: return k.geb;
default: return null;
}
}
public Class getColumnClass(int columnIndex) {
if (columnIndex == 5) return Adresse.class;
else return String.class;
}
public void addTableModelListener(TableModelListener l) {
listeners.add(l);
}
public void removeTableModelListener(TableModelListener l) {
listeners.remove(l);
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Kontakt k = ((Node) contacts.get(rowIndex)).getContact();
switch(columnIndex) {
case 0:
k.nick = (String) aValue;
break;
case 1:
k.telefon = (String) aValue;
break;
case 2:
k.handy = (String) aValue;
break;
case 3:
k.email = (String) aValue;
break;
case 4:
k.email2 = (String) aValue;
break;
case 6:
k.geb = (String) aValue;
break;
}
}
}
class Renderer implements TableCellRenderer {
Model model;
public Renderer(Model m){
model = m;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel l = new JLabel();
l.setText(value.toString());
if (isSelected) l.setBackground(Color.GREEN);
if (column == 0) {
String vor = ( (Node) model.getContacts().get(row)).getContact().vorname;
String nach = ( (Node) model.getContacts().get(row)).getContact().name;
l.setToolTipText(vor + " " + nach);
}
return l;
}
}
class ComboRenderer implements TableCellRenderer {
Model model;
public ComboRenderer(Model m){
model = m;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JComboBox l = new JComboBox();
l.setEditable(true);
if (isSelected) l.setBackground(Color.GREEN);
l.addItem(((Node)model.getContacts().get(row)).getAdress().strasse);
l.addItem(((Node)model.getContacts().get(row)).getAdress().ort);
l.addItem(((Node)model.getContacts().get(row)).getAdress().staat);
return l;
}
}
class ComboEditor implements TableCellEditor {
private Model model;
private Vector listeners = new Vector();
public ComboEditor(Model m){
model = m;
}
public void addCellEditorListener(CellEditorListener l) {
listeners.add( l );
}
public void removeCellEditorListener(CellEditorListener l) {
listeners.remove( l );
}
public Object getCellEditorValue() {
return this;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
JComboBox l = new JComboBox();
l.setEditable(true);
l.addItem(((Node)model.getContacts().get(row)).getAdress().strasse);
l.addItem(((Node)model.getContacts().get(row)).getAdress().ort);
l.addItem(((Node)model.getContacts().get(row)).getAdress().staat);
return l;
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public void cancelCellEditing() {
fireEditingCanceld();
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
protected void fireEditingCanceld(){
ChangeEvent e = new ChangeEvent( this );
for( int i = 0, n = listeners.size(); i<n; i++ )
((CellEditorListener)listeners.get( i )).editingCanceled( e );
}
protected void fireEditingStopped(){
ChangeEvent e = new ChangeEvent( this );
for( int i = 0, n = listeners.size(); i<n; i++ )
((CellEditorListener)listeners.get( i )).editingStopped( e );
}
}
Edit von L-ectron-X: Bitte Aussage kräftige Titel verwenden! Kein Mensch kann die Frage zuordnen wenn da steht: "Trau mich gar nicht zu fragen..."