Рейтинг 3 из 5 звёзд

T2

import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.*;
import javax.swing.table.*;

interface TableData{

String[] kopfzeile = { "Ort","Zeit","Beschreibung","Preis" };
}

public class Buchungen extends JFrame implements TableData, ActionListener{

private JButton element1 = new JButton("weiter");
private JButton element2 = new JButton("zurück");
private JButton element3 = new JButton("Ausgeben");
private JTable tabelle;
private int indexRow = 0; //Selektieren!

String [][] daten;

Buchungen(String [][] gelesen){
super("String titel");
this.setSize(500,150);

daten = gelesen;

//Panel:
JPanel panel_button = new JPanel();

//Container:
Container cp = getContentPane();

//Layout:
cp.setLayout(new BorderLayout());

//Setzen Panel:
cp.add(panel_button, BorderLayout.SOUTH);
panel_button.add(element1);
panel_button.add(element2);
panel_button.add(element3);

element3.addActionListener(this);
element1.addActionListener(this);
element2.addActionListener(this);

//Spaltenmodell

DefaultTableColumnModel sm = new DefaultTableColumnModel();

for(int i = 0; i < kopfzeile.length;i++){

TableColumn col = new TableColumn(i,60);

col.setHeaderValue(kopfzeile[i]);

sm.addColumn(col);

}

//Tabellenmodel

TableModel tm = new DefaultTableModel(){

public int getRowCount(){

return daten.length;

}

public int getColumnCount(){

return daten[0].length;

}

public Object getValueAt( int row, int column){

return daten[row][column];

}
};
tabelle = new JTable (tm, sm);
tabelle.setRowSelectionInterval(0, 0);//VorSelektieren!
getContentPane().add(new JScrollPane(tabelle));

this.setVisible(true);
}

public void actionPerformed(ActionEvent e){

String cmd = e.getActionCommand();

if (cmd.equals("Ausgeben")){
int i = tabelle.getSelectedRow();
for(String xy : daten[i])
{
System.out.println(xy);

ObjectOutputStream out;

try {
out = new ObjectOutputStream(new FileOutputStream( "Datei.txt" ));
out.writeObject(xy);

out.flush();
out.close();
} catch( IOException ex ) {
System.out.println( ex );
}
}
}
if (cmd.equals("weiter")){ //Selektieren!
indexRow = tabelle.getSelectedRow() == daten.length-1? 0: indexRow + 1;
System.out.println("indexRow:" + indexRow);
tabelle.setRowSelectionInterval(indexRow, indexRow);
}
if (cmd.equals("zurück")){
indexRow = tabelle.getSelectedRow() == daten.length+1? 0: indexRow - 1;
System.out.println("indexRow:" + indexRow);
tabelle.setRowSelectionInterval(indexRow, indexRow);
}
}

public static void main(String[] args) {
Buchungen frame = new Buchungen(null);
}
}