Dobrý den, jsem začátečník a potřeboval bych poradit, jak seřadit výsledky, také uložení tabulky do souboru a načtení.
package model;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class TableModelPolozky extends AbstractTableModel {
private List<Polozka> listPolozky;
private String[] sloupce = {"", "Auto", "Km", "Cena"};
public TableModelPolozky() {
listPolozky = new ArrayList<Polozka>();
}
public TableModelPolozky(List<Polozka> listPolozky) {
super();
this.listPolozky = listPolozky;
}
@Override
public int getColumnCount() {
return sloupce.length;
}
@Override
public int getRowCount() {
return listPolozky.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Polozka p = listPolozky.get(rowIndex);
switch (columnIndex) {
case 0: return rowIndex;
case 1: return p.getNazev();
case 2: return p.getMnozstvi();
case 3: return p.getCena() + " Kč";
default: return "";
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case 0: return Integer.class;
case 1: return String.class;
case 2: return Integer.class;
case 3: return String.class;
default: return String.class;
}
}
@Override
public String getColumnName(int column) {
return sloupce[column];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
public void addPolozka(Polozka p) {
listPolozky.add(p);
}
public void removePolozka(int ktera) {
listPolozky.remove(ktera);
}
public Polozka getPolozka(int ktera) {
return listPolozky.get(ktera);
}
public List<Polozka> getListPolozky() {
return listPolozky;
}
public void setListPolozky(List<Polozka> listPolozky) {
this.listPolozky = listPolozky;
}
}
package model;
public class Polozka {
private String nazev;
private int mnozstvi;
private int cena;
public Polozka() {
}
public Polozka(String nazev, int mnozstvi, int cena, boolean isVKosiku) {
super();
this.nazev = nazev;
this.mnozstvi = mnozstvi;
this.cena = cena;
}
public String getNazev() {
return nazev;
}
public void setNazev(String nazev) {
this.nazev = nazev;
}
public int getMnozstvi() {
return mnozstvi;
}
public void setMnozstvi(int mnozstvi) {
this.mnozstvi = mnozstvi;
}
public int getCena() {
return cena;
}
public void setCena(int cena) {
this.cena = cena;
}
public boolean isVKosiku() {
return isVKosiku;
}
}
package gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import model.Polozka;
public class PolozkaDialog extends JDialog {
private JTextField txtNazev = new JTextField();
private JTextField txtMnozstvi = new JTextField();
private JTextField txtCena = new JTextField();
private JButton btnOk = new JButton("OK");
private Polozka polozka;
public PolozkaDialog() {
setModal(true);
setLayout( new GridLayout(4,3) );
add(new JLabel("Auto:"));
add(txtNazev);
add(new JLabel("Km:"));
add(txtMnozstvi);
add(new JLabel("Cena:"));
add(txtCena);
add(btnOk);
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
polozka = new Polozka();
polozka.setNazev( txtNazev.getText() );
polozka.setCena( Integer.parseInt(txtCena.getText()) );
try {
polozka.setMnozstvi( Integer.parseInt(txtMnozstvi.getText()) );
} catch (NumberFormatException ex) {
polozka.setMnozstvi(1);
}
setVisible(false); // "odbrzdi" se program v zobrazNovouPolozku
}
});
pack();
}
public Polozka zobrazNovouPolozku() {
setVisible(true); // zobrazi okno, pocka, az bude okno zavreno
return polozka;
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import model.Polozka;
import model.TableModelPolozky;
import javax.swing.table.TableRowSorter;
public class EvidenceAutGui extends JFrame {
private JTable tabPolozky;
private TableModelPolozky tblModel;
private JButton btnPridat = new JButton("Pridat");
private JButton btnSmazat = new JButton("Smazat");
private JButton btnSeradit = new JButton("Seradit");
private JButton btnUlozit = new JButton("Ulozit");
private JButton btnNacist = new JButton("Nacist");
public EvidenceAutGui() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
tblModel = new TableModelPolozky();
tblModel.addPolozka(new Polozka("Opel Astra", 10010, 120000, false));
tblModel.addPolozka(new Polozka("Škoda Fabia", 200000, 54990, false));
tabPolozky = new JTable(tblModel);
JPanel toolbar = new JPanel();
JPanel toolbar2 = new JPanel();
add(toolbar, BorderLayout.NORTH);
add(toolbar2, BorderLayout.SOUTH);
toolbar.add(btnPridat);
toolbar.add(btnSmazat);
toolbar.add(btnSeradit);
toolbar2.add(btnUlozit);
toolbar2.add(btnNacist);
btnPridat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pridat();
}
});
btnSmazat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
remove();
}
});
btnSeradit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
seradit();
}
});
add(new JScrollPane(tabPolozky), BorderLayout.CENTER);
pack();
}
private void seradit() {
tblModel.getRowCount();
tblModel.fireTableDataChanged();
}
private void pridat() {
PolozkaDialog d = new PolozkaDialog();
Polozka p = d.zobrazNovouPolozku();
tblModel.addPolozka(p);
tblModel.fireTableDataChanged();
}
private void remove() {
int index=0;
boolean ok;
do {
try {
String cs = JOptionPane.showInputDialog("Zadejte cislo zaznamu");
index = Integer.valueOf(cs);
ok = true;
} catch (NumberFormatException ex) {
JOptionPane.showInputDialog("Nespravne, zadej znovu!!!");
ok = false;
ex.printStackTrace();
}
} while (!ok);
tblModel.removePolozka(index);
tblModel.fireTableDataChanged();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new EvidenceAutGui().setVisible(true);
}
});
}
}