Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. package ztp2;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.table.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7. import static ztp2.TableData.rnd;
  8.  
  9. class Database extends AbstractTableModel {
  10. private List<TableHeader> headers;
  11. private List<List<TableData>> data;
  12.  
  13. public Database() {
  14. headers = new ArrayList<TableHeader>();
  15. data = new ArrayList<List<TableData>>();
  16. }
  17. public void addRow() {
  18. List<TableData> row = new ArrayList<TableData>();
  19. for(TableHeader col:headers)
  20. row.add(col.returnType()); // wywołanie metody fabrykującej
  21. data.add(row);
  22. fireTableStructureChanged();
  23. }
  24. public void addCol(TableHeader type) {
  25. headers.add(type);
  26. for(List<TableData> row:data)
  27. row.add(type.returnType()); // wywołanie metody fabrykującej
  28. fireTableStructureChanged();
  29. }
  30.  
  31. public int getRowCount() { return data.size(); }
  32. public int getColumnCount() { return headers.size(); }
  33. public String getColumnName(int column) {
  34. return headers.get(column).toString();
  35. }
  36. public Object getValueAt(int row, int column) {
  37. return data.get(row).get(column);
  38. }
  39. }
  40.  
  41. interface TableData extends Cloneable{
  42. final static Random rnd = new Random();
  43. public abstract void rerand();
  44. public abstract Object clone();
  45.  
  46. }
  47.  
  48. class TableDataInt implements TableData
  49. {
  50. private int data;
  51. public TableDataInt() { data = rnd.nextInt(100); }
  52. public void rerand() { data = rnd.nextInt(100); }
  53. public String toString() { return Integer.toString(data);}
  54. public Object clone(){
  55. Object o=null;
  56. try { o= super.clone(); }
  57. catch (CloneNotSupportedException e) { return null; }
  58. ((TableData) o).rerand();
  59. return o;
  60. }
  61. }
  62. class TableDataDouble implements TableData
  63. {
  64. private double data;
  65. public TableDataDouble() { data = rnd.nextFloat(); }
  66. public void rerand(){data = rnd.nextFloat(); }
  67. public String toString() { return Double.toString(data); }
  68. public Object clone(){
  69. Object o=null;
  70. try { o= super.clone(); }
  71. catch (CloneNotSupportedException e) { return null; }
  72. ((TableData) o).rerand();
  73. return o;
  74. }
  75. }
  76. class TableDataChar implements TableData
  77. {
  78. private char data;
  79. public TableDataChar() { data = (char)(rnd.nextInt(50)+'a'); }
  80. public void rerand(){ data = (char)(rnd.nextInt(50)+'a');}
  81. public String toString() { return String.valueOf(data); }
  82. public Object clone(){
  83. Object o=null;
  84. try { o= super.clone(); }
  85. catch (CloneNotSupportedException e) { return null; }
  86. ((TableData) o).rerand();
  87. return o;
  88. }
  89.  
  90. }
  91. class TableDataBoolean implements TableData
  92. {
  93. private boolean data;
  94. public TableDataBoolean() { data = rnd.nextBoolean();}
  95. public void rerand(){data = rnd.nextBoolean();}
  96. public String toString() { return Boolean.toString(data); }
  97. public Object clone() {
  98. Object o=null;
  99. try { o= super.clone(); }
  100. catch (CloneNotSupportedException e) { return null; }
  101. ((TableData) o).rerand();
  102. return o;
  103. }
  104. }
  105. /* ... */
  106. class TableHeader
  107. {
  108. TableData dat;
  109. private String type;
  110. public TableHeader(String type,TableData dat) { this.type = type;this.dat=dat; }
  111. public String toString() { return type; }
  112. public TableData returnType(){
  113. return (TableData)dat.clone();
  114. }
  115. }
  116.  
  117. public class ZTP2 {
  118. public static void main(String[] args) {
  119. final JFrame frame = new JFrame("Baza danych");
  120. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  121.  
  122. final Database database = new Database();
  123.  
  124. JTable table = new JTable(database);
  125. JMenuBar bar = new JMenuBar();
  126.  
  127. JButton row = new JButton("Dodaj Wiersz");
  128. JButton col = new JButton("Dodaj Kolumnę");
  129.  
  130. bar.add(row);
  131. bar.add(col);
  132.  
  133. frame.add(new JScrollPane(table));
  134. frame.setJMenuBar(bar);
  135.  
  136. frame.pack();
  137. frame.setVisible(true);
  138.  
  139. row.addActionListener(new ActionListener(){
  140. public void actionPerformed(ActionEvent ev)
  141. {
  142. database.addRow();
  143. }
  144. });
  145. col.addActionListener(new ActionListener(){
  146. public void actionPerformed(ActionEvent ev)
  147. {
  148. Object option = JOptionPane.showInputDialog(
  149. frame,
  150. "Podaj typ kolumny",
  151. "Dodaj Kolumnę",
  152. JOptionPane.QUESTION_MESSAGE,
  153. null,
  154. new TableHeader[] {
  155. new TableHeader("INT",new TableDataInt()),
  156. new TableHeader("DOUBLE",new TableDataDouble()),
  157. new TableHeader("CHAR",new TableDataChar()),
  158. new TableHeader("BOOLEAN", new TableDataBoolean()),
  159. }, null);
  160. if(option == null)
  161. return;
  162. database.addCol((TableHeader)option);
  163. }
  164. });
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement