Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ztp2;
- import javax.swing.*;
- import javax.swing.table.*;
- import java.awt.event.*;
- import java.util.*;
- import static ztp2.TableData.rnd;
- class Database extends AbstractTableModel {
- private List<TableHeader> headers;
- private List<List<TableData>> data;
- public Database() {
- headers = new ArrayList<TableHeader>();
- data = new ArrayList<List<TableData>>();
- }
- public void addRow() {
- List<TableData> row = new ArrayList<TableData>();
- for(TableHeader col:headers)
- row.add(col.returnType()); // wywołanie metody fabrykującej
- data.add(row);
- fireTableStructureChanged();
- }
- public void addCol(TableHeader type) {
- headers.add(type);
- for(List<TableData> row:data)
- row.add(type.returnType()); // wywołanie metody fabrykującej
- fireTableStructureChanged();
- }
- public int getRowCount() { return data.size(); }
- public int getColumnCount() { return headers.size(); }
- public String getColumnName(int column) {
- return headers.get(column).toString();
- }
- public Object getValueAt(int row, int column) {
- return data.get(row).get(column);
- }
- }
- interface TableData extends Cloneable{
- final static Random rnd = new Random();
- public abstract void rerand();
- public abstract Object clone();
- }
- class TableDataInt implements TableData
- {
- private int data;
- public TableDataInt() { data = rnd.nextInt(100); }
- public void rerand() { data = rnd.nextInt(100); }
- public String toString() { return Integer.toString(data);}
- public Object clone(){
- Object o=null;
- try { o= super.clone(); }
- catch (CloneNotSupportedException e) { return null; }
- ((TableData) o).rerand();
- return o;
- }
- }
- class TableDataDouble implements TableData
- {
- private double data;
- public TableDataDouble() { data = rnd.nextFloat(); }
- public void rerand(){data = rnd.nextFloat(); }
- public String toString() { return Double.toString(data); }
- public Object clone(){
- Object o=null;
- try { o= super.clone(); }
- catch (CloneNotSupportedException e) { return null; }
- ((TableData) o).rerand();
- return o;
- }
- }
- class TableDataChar implements TableData
- {
- private char data;
- public TableDataChar() { data = (char)(rnd.nextInt(50)+'a'); }
- public void rerand(){ data = (char)(rnd.nextInt(50)+'a');}
- public String toString() { return String.valueOf(data); }
- public Object clone(){
- Object o=null;
- try { o= super.clone(); }
- catch (CloneNotSupportedException e) { return null; }
- ((TableData) o).rerand();
- return o;
- }
- }
- class TableDataBoolean implements TableData
- {
- private boolean data;
- public TableDataBoolean() { data = rnd.nextBoolean();}
- public void rerand(){data = rnd.nextBoolean();}
- public String toString() { return Boolean.toString(data); }
- public Object clone() {
- Object o=null;
- try { o= super.clone(); }
- catch (CloneNotSupportedException e) { return null; }
- ((TableData) o).rerand();
- return o;
- }
- }
- /* ... */
- class TableHeader
- {
- TableData dat;
- private String type;
- public TableHeader(String type,TableData dat) { this.type = type;this.dat=dat; }
- public String toString() { return type; }
- public TableData returnType(){
- return (TableData)dat.clone();
- }
- }
- public class ZTP2 {
- public static void main(String[] args) {
- final JFrame frame = new JFrame("Baza danych");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- final Database database = new Database();
- JTable table = new JTable(database);
- JMenuBar bar = new JMenuBar();
- JButton row = new JButton("Dodaj Wiersz");
- JButton col = new JButton("Dodaj Kolumnę");
- bar.add(row);
- bar.add(col);
- frame.add(new JScrollPane(table));
- frame.setJMenuBar(bar);
- frame.pack();
- frame.setVisible(true);
- row.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev)
- {
- database.addRow();
- }
- });
- col.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ev)
- {
- Object option = JOptionPane.showInputDialog(
- frame,
- "Podaj typ kolumny",
- "Dodaj Kolumnę",
- JOptionPane.QUESTION_MESSAGE,
- null,
- new TableHeader[] {
- new TableHeader("INT",new TableDataInt()),
- new TableHeader("DOUBLE",new TableDataDouble()),
- new TableHeader("CHAR",new TableDataChar()),
- new TableHeader("BOOLEAN", new TableDataBoolean()),
- }, null);
- if(option == null)
- return;
- database.addCol((TableHeader)option);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement