KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > TablePanel


1 /* ManagerTablePanel.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11
12 package org.enhydra.shark.corbaclient;
13
14 import java.awt.*;
15 import java.util.*;
16
17 import javax.swing.*;
18 import javax.swing.border.*;
19 import javax.swing.table.*;
20
21 /**
22 * Creates a table panel and implements the methods to add
23 * and remove data from the table.
24 */

25 public class TablePanel extends JPanel {
26
27    private JTable allItems;
28
29    public TablePanel (Vector columnNames, boolean isFirstColumnVisible) {
30       super();
31
32       int emptyBorderHSize=10;
33       int emptyBorderVSize=10;
34       Border emptyb=BorderFactory.createEmptyBorder(emptyBorderVSize,emptyBorderHSize,
35          emptyBorderVSize,emptyBorderHSize);
36       Border inb=BorderFactory.createMatteBorder(1,1,1,1,Color.darkGray);
37       inb=BorderFactory.createTitledBorder(inb,"");
38
39       setBorder(BorderFactory.createCompoundBorder(emptyb,inb));
40       setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
41
42       // creating a table which do not allow cell editing
43
allItems=new JTable(new Vector(),columnNames) {
44          public boolean isCellEditable(int row, int col) {
45             return false;
46          }
47       };
48
49       if (!isFirstColumnVisible) {
50          TableColumnModel tcm=allItems.getColumnModel();
51          TableColumn column;
52          // setting the first column (object column) to be invisible
53
column = allItems.getColumnModel().getColumn(0);
54          column.setMinWidth(0);
55          column.setMaxWidth(0);
56          column.setPreferredWidth(0);
57          column.setResizable(false);
58       }
59
60       // setting some table properties
61
allItems.setColumnSelectionAllowed(false);
62       allItems.setRowSelectionAllowed(true);
63       allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
64       allItems.getTableHeader().setReorderingAllowed(false);
65
66       // creates panel
67
JScrollPane allItemsPane=new JScrollPane();
68       allItemsPane.setViewportView(allItems);
69       //allItems.setPreferredScrollableViewportSize(new Dimension(tDim));
70

71       add(allItemsPane);
72       add(Box.createVerticalGlue());
73    }
74
75    public JTable getTable () {
76       return allItems;
77    }
78
79    public Object JavaDoc getColumnValueOfSelectedRow (int columnNo) {
80       int row=allItems.getSelectedRow();
81       Object JavaDoc frw=null;
82       if (row>=0) {
83          try {
84             frw=allItems.getValueAt(row,columnNo);
85          } catch (Exception JavaDoc ex) {
86             return null;
87          }
88       }
89       return frw;
90    }
91
92    public void setColumnValueOfSelectedRow (int columnNo,Object JavaDoc val) {
93       int row=allItems.getSelectedRow();
94       if (row>=0) {
95          try {
96             allItems.setValueAt(val,row,columnNo);
97          } catch (Exception JavaDoc ex) {
98          }
99       }
100    }
101
102    public void addElement(Vector v) throws Exception JavaDoc {
103       DefaultTableModel dtm=(DefaultTableModel)allItems.getModel();
104       try {
105          dtm.addRow(v);
106       } catch (Exception JavaDoc ex) {
107          System.out.println("Cannot add row to the table");
108       }
109    }
110
111    /**
112     * Removes the table element which column values corresponds to the
113     * one given in the given array. Only the first 'n' colums are checked
114     * for equality, where 'n' is the number of elements in given array.
115     */

116    public void removeElement(Object JavaDoc[] columnValues) {
117       if (columnValues==null || columnValues.length==0) return;
118       int rowCnt=allItems.getRowCount();
119       DefaultTableModel dtm=(DefaultTableModel)allItems.getModel();
120       for (int row=0; row<rowCnt; row++) {
121          boolean canRemove=true;
122          for (int col=0; col<columnValues.length; col++) {
123             canRemove=canRemove && allItems.getValueAt(row,col).equals(columnValues[col]);
124             if (!canRemove) break;
125          }
126          if (canRemove) {
127             dtm.removeRow(row);
128             break;
129          }
130       }
131    }
132
133    public void removeAll () {
134       DefaultTableModel dtm=(DefaultTableModel)allItems.getModel();
135       while (true) {
136          try {
137             dtm.removeRow(0);
138          } catch (Exception JavaDoc ex) {
139             break;
140          }
141       }
142    }
143
144 }
145
146 /* End of TablePanel.java */
147
Popular Tags