Guest User

Untitled

a guest
Aug 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. Java swing: Jtable with many models and custom renderer
  2. resultTable = new javax.swing.JTable(){
  3. private Border outside = new MatteBorder(1, 0, 1, 0, Color.BLACK);
  4. private Border inside = new EmptyBorder(0, 1, 0, 1);
  5. private Border highlight = new CompoundBorder(outside, inside);
  6. public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
  7. Component c = super.prepareRenderer(renderer, row, column);
  8. JComponent jc = (JComponent) c;
  9. // Color row based on a cell value
  10. if (!isRowSelected(row)) {
  11. c.setBackground(getBackground());
  12. int modelRow = convertRowIndexToModel(row);
  13. if (getStatus().equals("status1")) {
  14. myFirstTableModel model = (myFirstTableModel ) resultTable.getModel();
  15. if ((model.getObjectAtRow(modelRow).getMsg().getRegNumIn() == 3)) {
  16. c.setBackground(new Color(255, 244, 148));//YELLOW - needs attension
  17. }
  18. } else if (getStatus().equals("status2")) {
  19. mySecondTableModel model = (mySecondTableModel) resultTable.getModel();
  20.  
  21. if (model.getObjectAtRow(modelRow).getMsg().getTask() == 2) {
  22. c.setBackground(new Color(210, 245, 176));//GREEN - got attension
  23. }
  24. }
  25. } else if (isRowSelected(row)) {
  26. jc.setBorder(highlight);
  27. c.setBackground(new Color(201, 204, 196));
  28. }
  29. return c;
  30. }
  31. };
  32.  
  33. final WaitDialog dialog = new WaitDialog(new javax.swing.JFrame(), true);
  34. dialog.addWindowListener(new java.awt.event.WindowAdapter() {
  35. });
  36. SwingWorker worker = new SwingWorker() {
  37. @Override
  38. protected Object doInBackground() throws Exception {
  39. setStatus("status2");
  40. Refresh();
  41. return 0;
  42. }
  43. @Override
  44. public void done() {
  45. dialog.dispose();
  46. }
  47. };
  48.  
  49. worker.execute();
  50. dialog.setVisible(true);
  51.  
  52. if (getMainFrameStatus().equals("status2")) {
  53. @Override
  54. public void run() {
  55. //Update the model here
  56.  
  57. resultTable.setModel(new mySecondTableModel(data));
  58. }
  59. });
  60.  
  61. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: myFirstTableModel cannot be cast to mySecondTableModel at mySecondTableModel model = (mySecondTableModel) resultTable.getModel();
  62.  
  63. public interface StatusAware {
  64.  
  65. enum Status {
  66.  
  67. NORMAL,
  68. GOT_ATTENTION,
  69. NEEDS_ATTENTION,
  70. ...
  71.  
  72. }
  73. public Status getStatus(int modelIndex);
  74. }
  75.  
  76. public class MyFirstTableModel extends AbstractTableModel implements StatusAware {
  77.  
  78. public Statuc getStatus(int modelRow) {
  79. boolean needsAttention = getObjectAtRow(modelRow).getMsg().getRegNumIn() == 3;
  80. return needsAttention ? NEEDS_ATTENTION : NORMAL;
  81. }
  82.  
  83. ....
  84. }
  85.  
  86.  
  87. public class MySecondTableModel extends AbstractTableModel implements StatusAware {
  88.  
  89. public Statuc getStatus(int modelRow) {
  90. return // the status of the given row
  91. }
  92.  
  93. ....
  94. }
  95.  
  96. public class MyTable extends JTable { // if you insist on not using JXTable
  97.  
  98.  
  99. public Component prepareRenderer(...) {
  100. Component comp = super(...)
  101. if (getModel() instanceof StatusAware {
  102. Status status = ((StatusAware) getModel()).getStatus(convertRowIndexToModel(row));
  103. if (NEEDS_ATTENTION == status) {
  104. ...
  105. } else if (...) {
  106. ...
  107. }
  108. }
  109. return comp;
  110. }
  111. }
  112.  
  113. HighlightPredicate feverWarning = new HighlightPredicate() {
  114. int temperatureColumn = 10;
  115.  
  116. public boolean isHighlighted(Component component, ComponentAdapter adapter) {
  117. return hasFever(adapter.getValue(temperatureColumn));
  118. }
  119.  
  120. private boolean hasFever(Object value) {
  121. if (!value instanceof Number)
  122. return false;
  123. return ((Number) value).intValue() > 37;
  124. }
  125. };
  126.  
  127. Highlighter hl = new ColorHighlighter(feverWarning, Color.RED, null);
  128. table.addHighlighter(hl);
  129.  
  130. if (adapter.getComponent() instanceof JTable) {
  131. JTable table = (JTable) adapter.getComponent();
  132. TableModel model = table.getModel();
  133. if (model instanceof MyModel) {
  134. int modelRow = adapter.convertRowIndexToModel(adapter.row);
  135. MyObject object = ((MyModel).getRowObjectAt(modelRow));
  136. ... // check the object
  137. }
  138. }
  139.  
  140. SwingUtilities.invokeLater(...)
  141.  
  142. SwingUtilities.invokeAndWait(...)
Add Comment
Please, Sign In to add comment