Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. public class Sport {
  2. private Integer id;
  3. private String name;
  4.  
  5. public Sport(String name){
  6. this.name = name;
  7. }
  8.  
  9. public Integer getId() {
  10. return id;
  11. }
  12.  
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. public String toString() {
  26. return this.name;
  27. }
  28. }
  29.  
  30.  
  31.  
  32. public class TableRenderDemo extends JPanel {
  33. private boolean DEBUG = false;
  34.  
  35. public TableRenderDemo() {
  36. super(new GridLayout(1,0));
  37.  
  38. JTable table = new JTable(new MyTableModel());
  39. table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  40. table.setFillsViewportHeight(true);
  41.  
  42. //Create the scroll pane and add the table to it.
  43. JScrollPane scrollPane = new JScrollPane(table);
  44.  
  45. //Set up column sizes.
  46. initColumnSizes(table);
  47.  
  48. //Fiddle with the Sport column's cell editors/renderers.
  49. setUpSportColumn(table, table.getColumnModel().getColumn(2));
  50.  
  51. //Add the scroll pane to this panel.
  52. add(scrollPane);
  53. }
  54.  
  55. /*
  56. * This method picks good column sizes.
  57. * If all column heads are wider than the column's cells'
  58. * contents, then you can just use column.sizeWidthToFit().
  59. */
  60. private void initColumnSizes(JTable table) {
  61. MyTableModel model = (MyTableModel)table.getModel();
  62. TableColumn column = null;
  63. Component comp = null;
  64. int headerWidth = 0;
  65. int cellWidth = 0;
  66. Object[] longValues = model.longValues;
  67. TableCellRenderer headerRenderer =
  68. table.getTableHeader().getDefaultRenderer();
  69.  
  70. for (int i = 0; i < 5; i++) {
  71. column = table.getColumnModel().getColumn(i);
  72.  
  73. comp = headerRenderer.getTableCellRendererComponent(
  74. null, column.getHeaderValue(),
  75. false, false, 0, 0);
  76. headerWidth = comp.getPreferredSize().width;
  77.  
  78. comp = table.getDefaultRenderer(model.getColumnClass(i)).
  79. getTableCellRendererComponent(
  80. table, longValues[i],
  81. false, false, 0, i);
  82. cellWidth = comp.getPreferredSize().width;
  83.  
  84. if (DEBUG) {
  85. System.out.println("Initializing width of column "
  86. + i + ". "
  87. + "headerWidth = " + headerWidth
  88. + "; cellWidth = " + cellWidth);
  89. }
  90.  
  91. column.setPreferredWidth(Math.max(headerWidth, cellWidth));
  92. }
  93. }
  94.  
  95. public void setUpSportColumn(JTable table,
  96. TableColumn sportColumn) {
  97. //Set up the editor for the sport cells.
  98. JComboBox comboBox = new JComboBox();
  99. comboBox.addItem("Snowboarding");
  100. comboBox.addItem("Rowing");
  101. comboBox.addItem("Knitting");
  102. comboBox.addItem("Speed reading");
  103. comboBox.addItem("Pool");
  104. comboBox.addItem("None of the above");
  105. sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
  106.  
  107. //Set up tool tips for the sport cells.
  108. DefaultTableCellRenderer renderer =
  109. new DefaultTableCellRenderer();
  110. renderer.setToolTipText("Click for combo box");
  111. sportColumn.setCellRenderer(renderer);
  112. }
  113.  
  114. class MyTableModel extends AbstractTableModel {
  115. private String[] columnNames = {"First Name",
  116. "Last Name",
  117. "Sport",
  118. "# of Years",
  119. "Vegetarian"};
  120. private Object[][] data = {
  121. {"Kathy", "Smith",new Sport("Snowboarding"),
  122. new Integer(5), new Boolean(false)},
  123. {"John", "Doe",
  124. new Sport("Rowing"), new Integer(3), new Boolean(true)},
  125. {"Sue", "Black",
  126. new Sport("Knitting"), new Integer(2), new Boolean(false)},
  127. {"Jane", "White",
  128. new Sport("Speed reading"), new Integer(20), new Boolean(true)},
  129. {"Joe", "Brown",
  130. new Sport("Pool"), new Integer(10), new Boolean(false)}
  131. };
  132.  
  133. public final Object[] longValues = {"Jane", "Kathy",
  134. "None of the above",
  135. new Integer(20), Boolean.TRUE};
  136.  
  137. public int getColumnCount() {
  138. return columnNames.length;
  139. }
  140.  
  141. public int getRowCount() {
  142. return data.length;
  143. }
  144.  
  145. public String getColumnName(int col) {
  146. return columnNames[col];
  147. }
  148.  
  149. public Object getValueAt(int row, int col) {
  150. return data[row][col];
  151. }
  152.  
  153. /*
  154. * JTable uses this method to determine the default renderer/
  155. * editor for each cell. If we didn't implement this method,
  156. * then the last column would contain text ("true"/"false"),
  157. * rather than a check box.
  158. */
  159. public Class getColumnClass(int c) {
  160. return getValueAt(0, c).getClass();
  161. }
  162.  
  163. /*
  164. * Don't need to implement this method unless your table's
  165. * editable.
  166. */
  167. public boolean isCellEditable(int row, int col) {
  168. //Note that the data/cell address is constant,
  169. //no matter where the cell appears onscreen.
  170. if (col < 2) {
  171. return false;
  172. } else {
  173. return true;
  174. }
  175. }
  176.  
  177. /*
  178. * Don't need to implement this method unless your table's
  179. * data can change.
  180. */
  181. public void setValueAt(Object value, int row, int col) {
  182. if (DEBUG) {
  183. System.out.println("Setting value at " + row + "," + col
  184. + " to " + value
  185. + " (an instance of "
  186. + value.getClass() + ")");
  187. }
  188.  
  189. data[row][col] = value;
  190. fireTableCellUpdated(row, col);
  191.  
  192. if (DEBUG) {
  193. System.out.println("New value of data:");
  194. printDebugData();
  195. }
  196. }
  197.  
  198. private void printDebugData() {
  199. int numRows = getRowCount();
  200. int numCols = getColumnCount();
  201.  
  202. for (int i=0; i < numRows; i++) {
  203. System.out.print(" row " + i + ":");
  204. for (int j=0; j < numCols; j++) {
  205. System.out.print(" " + data[i][j]);
  206. }
  207. System.out.println();
  208. }
  209. System.out.println("--------------------------");
  210. }
  211. }
  212.  
  213. /**
  214. * Create the GUI and show it. For thread safety,
  215. * this method should be invoked from the
  216. * event-dispatching thread.
  217. */
  218. private static void createAndShowGUI() {
  219. //Create and set up the window.
  220. JFrame frame = new JFrame("TableRenderDemo");
  221. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  222.  
  223. //Create and set up the content pane.
  224. TableRenderDemo newContentPane = new TableRenderDemo();
  225. newContentPane.setOpaque(true); //content panes must be opaque
  226. frame.setContentPane(newContentPane);
  227.  
  228. //Display the window.
  229. frame.pack();
  230. frame.setVisible(true);
  231. }
  232.  
  233. public static void main(String[] args) {
  234. //Schedule a job for the event-dispatching thread:
  235. //creating and showing this application's GUI.
  236. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  237. public void run() {
  238. createAndShowGUI();
  239. }
  240. });
  241. }
  242. }
  243.  
  244. public void setUpSportColumn(JTable table,
  245. TableColumn sportColumn) {
  246. //Set up the editor for the sport cells.
  247. JComboBox<Sport> comboBox = new JComboBox();
  248. comboBox.addItem(new Sport("Snowboarding"));
  249. comboBox.addItem(new Sport("Rowing"));
  250. comboBox.addItem(new Sport("Knitting"));
  251. comboBox.addItem(new Sport("Speed reading"));
  252. comboBox.addItem(new Sport("Pool"));
  253. comboBox.addItem(new Sport("None of the above"));
  254. sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
  255.  
  256. //Set up tool tips for the sport cells.
  257. DefaultTableCellRenderer renderer =
  258. new DefaultTableCellRenderer();
  259. renderer.setToolTipText("Click for combo box");
  260. sportColumn.setCellRenderer(renderer);
  261. // sportColumn.setCellEditor(new MyCellEditor());
  262. }
  263.  
  264. @Override
  265. public boolean equals(Object object)
  266. {
  267. Sport sport = (Sport)object;
  268. return this.name.equals( sport.getName() );
  269. }
  270.  
  271. @Override
  272. public int hashCode()
  273. {
  274. return name.hashCode();
  275. }
  276.  
  277. comboBox.addItem(new Sport("Rowing"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement