Přesun TableModelu do GUI balíčku.
1 package cz.frantovo.rozsireneAtributy.gui;
3 import cz.frantovo.rozsireneAtributy.Atribut;
4 import java.io.IOException;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
7 import javax.swing.JOptionPane;
8 import javax.swing.event.ListSelectionEvent;
9 import javax.swing.event.ListSelectionListener;
15 public class Panel extends javax.swing.JPanel {
17 private static final Logger log = Logger.getLogger(Panel.class.getSimpleName());
19 private Atribut vybranýAtribut;
21 public Panel(Model model) {
24 tabulka.setModel(model);
26 /** Výběr aktuálního atributu v tabulce */
27 tabulka.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
29 public void valueChanged(ListSelectionEvent e) {
30 int řádek = tabulka.getSelectedRow();
32 vybranýAtribut = null;
33 tlačítkoSmazat.setEnabled(false);
35 vybranýAtribut = getModel().getAtribut(řádek);
36 tlačítkoSmazat.setEnabled(true);
42 private Model getModel() {
46 private void zobrazChybovouHlášku(String hláška, Throwable chyba) {
47 JOptionPane.showMessageDialog(this, hláška, "Chyba", JOptionPane.ERROR_MESSAGE);
48 log.log(Level.WARNING, hláška, chyba);
51 @SuppressWarnings("unchecked")
52 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
53 private void initComponents() {
55 posuvnýPanel = new javax.swing.JScrollPane();
56 tabulka = new javax.swing.JTable();
57 tlačítkoPřidat = new javax.swing.JButton();
58 tlačítkoSmazat = new javax.swing.JButton();
59 tlačítkoZnovuNačíst = new javax.swing.JButton();
61 tabulka.setModel(new javax.swing.table.DefaultTableModel(
63 {null, null, null, null},
64 {null, null, null, null},
65 {null, null, null, null},
66 {null, null, null, null}
69 "Title 1", "Title 2", "Title 3", "Title 4"
72 tabulka.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
73 posuvnýPanel.setViewportView(tabulka);
75 tlačítkoPřidat.setMnemonic('p');
76 tlačítkoPřidat.setText("Přidat atribut");
77 tlačítkoPřidat.addActionListener(new java.awt.event.ActionListener() {
78 public void actionPerformed(java.awt.event.ActionEvent evt) {
79 tlačítkoPřidatActionPerformed(evt);
83 tlačítkoSmazat.setMnemonic('s');
84 tlačítkoSmazat.setText("Smazat atribut");
85 tlačítkoSmazat.setEnabled(false);
86 tlačítkoSmazat.addActionListener(new java.awt.event.ActionListener() {
87 public void actionPerformed(java.awt.event.ActionEvent evt) {
88 tlačítkoSmazatActionPerformed(evt);
92 tlačítkoZnovuNačíst.setMnemonic('z');
93 tlačítkoZnovuNačíst.setText("Znovu načíst");
94 tlačítkoZnovuNačíst.addActionListener(new java.awt.event.ActionListener() {
95 public void actionPerformed(java.awt.event.ActionEvent evt) {
96 tlačítkoZnovuNačístActionPerformed(evt);
100 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
101 this.setLayout(layout);
102 layout.setHorizontalGroup(
103 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
104 .addGroup(layout.createSequentialGroup()
106 .addComponent(tlačítkoPřidat)
107 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
108 .addComponent(tlačítkoSmazat)
109 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
110 .addComponent(tlačítkoZnovuNačíst)
111 .addContainerGap(186, Short.MAX_VALUE))
112 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
114 layout.setVerticalGroup(
115 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
116 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
117 .addComponent(posuvnýPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
118 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
119 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
120 .addComponent(tlačítkoPřidat)
121 .addComponent(tlačítkoSmazat)
122 .addComponent(tlačítkoZnovuNačíst))
125 }// </editor-fold>//GEN-END:initComponents
127 private void tlačítkoPřidatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoPřidatActionPerformed
128 model.přidejAtribut(new Atribut());
129 }//GEN-LAST:event_tlačítkoPřidatActionPerformed
131 private void tlačítkoSmazatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoSmazatActionPerformed
133 model.odeberAtribut(vybranýAtribut);
134 } catch (IOException e) {
135 zobrazChybovouHlášku("Nepodařilo se smazat atribut.", e);
137 }//GEN-LAST:event_tlačítkoSmazatActionPerformed
139 private void tlačítkoZnovuNačístActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tlačítkoZnovuNačístActionPerformed
141 model.načtiAtributy();
142 } catch (IOException e) {
143 zobrazChybovouHlášku("Nepodařilo se načíst atributy", e);
145 }//GEN-LAST:event_tlačítkoZnovuNačístActionPerformed
146 // Variables declaration - do not modify//GEN-BEGIN:variables
147 private javax.swing.JScrollPane posuvnýPanel;
148 private javax.swing.JTable tabulka;
149 private javax.swing.JButton tlačítkoPřidat;
150 private javax.swing.JButton tlačítkoSmazat;
151 private javax.swing.JButton tlačítkoZnovuNačíst;
152 // End of variables declaration//GEN-END:variables