Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. ListSelectionModel selectionModel;
  2. JTable table1;
  3. model = new DefaultTableModel();
  4. table = new JTable(model); table.setRowHeight(20);
  5. selectionModel = table.getSelectionModel();
  6. selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  7.  
  8. selectionModel.addListSelectionListener(new ListSelectionListener() {
  9. public void valueChanged(ListSelectionEvent e) {
  10.  
  11. stxtBox.setText("");
  12. ptxtBox.setText("");
  13. ntxtBox.setText("");
  14.  
  15. if (!e.getValueIsAdjusting()) {
  16.  
  17.  
  18.  
  19. model1 = table.getSelectionModel();
  20. int lead = model1.getLeadSelectionIndex();
  21. int columns = table.getColumnCount();
  22. String sip = "";
  23. String sport = "";
  24. String snoq = "";
  25. for (int col = 0; col < columns; col++) {
  26. Object o = table.getValueAt(lead, col);
  27.  
  28. if (col == 0) {
  29. sip += o.toString();
  30. stxtBox.setText(sip);
  31. selectedip = sip;
  32.  
  33. } else if (col == 1) {
  34.  
  35. sport += o.toString();
  36. ptxtBox.setText(sport);
  37. selectedport = sport;
  38. } else {
  39.  
  40. snoq += o.toString();
  41. ntxtBox.setText(snoq);
  42. }
  43. selectedreq = snoq;
  44.  
  45. }
  46.  
  47.  
  48.  
  49. }table.clearSelection();
  50. }
  51. });
  52.  
  53. int rowCount=0;
  54.  
  55. File file = new File("serverconfig.xml");
  56.  
  57. if (file.exists())
  58.  
  59. {
  60.  
  61. System.out.print("in load");
  62. int e = table.getRowCount();
  63.  
  64. if(e> 0)
  65. {
  66.  
  67. while (table.getRowCount() > 0) {
  68. ((DefaultTableModel) table.getModel()).removeRow(0);
  69. }
  70.  
  71. }
  72.  
  73. //here i will load table content from my xml file (that's working fine)
  74.  
  75. java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
  76. at java.util.Vector.elementAt(Unknown Source)
  77. at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
  78. at javax.swing.JTable.getValueAt(Unknown Source)
  79. at Testsample$18.valueChanged(Testsample.java:1810)
  80. at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
  81. at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
  82. at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
  83. at javax.swing.DefaultListSelectionModel.insertIndexInterval(Unknown Source)
  84. at javax.swing.JTable.tableRowsInserted(Unknown Source)
  85. at javax.swing.JTable.tableChanged(Unknown Source)
  86. at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
  87. at javax.swing.table.AbstractTableModel.fireTableRowsInserted(Unknown Source)
  88. at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
  89. at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
  90. at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
  91. at Testsample.loadtable(Testsample.java:577)
  92. at Testsample$10.actionPerformed(Testsample.java:1551)
  93. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  94. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  95. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  96. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  97. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  98.  
  99. import java.awt.*;
  100. import java.awt.event.ActionEvent;
  101. import java.awt.event.ActionListener;
  102. import javax.swing.*;
  103. import javax.swing.table.*;
  104.  
  105. public class RemoveAddRows extends JFrame {
  106.  
  107. private static final long serialVersionUID = 1L;
  108. private Object[] columnNames = {"Type", "Company", "Shares", "Price"};
  109. private Object[][] data = {
  110. {"Buy", "IBM", new Integer(1000), new Double(80.50)},
  111. {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},
  112. {"Sell", "Apple", new Integer(3000), new Double(7.35)},
  113. {"Buy", "Nortel", new Integer(4000), new Double(20.00)}
  114. };
  115. private JTable table;
  116. private DefaultTableModel model;
  117.  
  118. public RemoveAddRows() {
  119.  
  120. model = new DefaultTableModel(data, columnNames) {
  121.  
  122. private static final long serialVersionUID = 1L;
  123.  
  124. @Override
  125. public Class getColumnClass(int column) {
  126. return getValueAt(0, column).getClass();
  127. }
  128. };
  129. table = new JTable(model) {
  130.  
  131. private static final long serialVersionUID = 1L;
  132.  
  133. @Override
  134. public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
  135. Component c = super.prepareRenderer(renderer, row, column);
  136. int firstRow = 0;
  137. int lastRow = table.getRowCount() - 1;
  138. int width = 0;
  139. if (row == lastRow) {
  140. ((JComponent) c).setBackground(Color.red);
  141. } else if (row == firstRow) {
  142. ((JComponent) c).setBackground(Color.blue);
  143. } else {
  144. ((JComponent) c).setBackground(table.getBackground());
  145. }
  146. /*if (!isRowSelected(row)) {
  147. String type = (String) getModel().getValueAt(row, 0);
  148. c.setBackground("Buy".equals(type) ? Color.GREEN : Color.YELLOW);
  149. }
  150. if (isRowSelected(row) && isColumnSelected(column)) {
  151. ((JComponent) c).setBorder(new LineBorder(Color.red));
  152. }*/
  153. return c;
  154. }
  155. };
  156. table.setPreferredScrollableViewportSize(table.getPreferredSize());
  157. JScrollPane scrollPane = new JScrollPane(table);
  158. add(scrollPane);
  159. JButton button1 = new JButton("Remove all rows");
  160. button1.addActionListener(new ActionListener() {
  161.  
  162. public void actionPerformed(ActionEvent arg0) {
  163. if (model.getRowCount() > 0) {
  164. for (int i = model.getRowCount() - 1; i > -1; i--) {
  165. model.removeRow(i);
  166. }
  167. }
  168. System.out.println("model.getRowCount() --->" + model.getRowCount());
  169. }
  170. });
  171. JButton button2 = new JButton("Add new rows");
  172. button2.addActionListener(new ActionListener() {
  173.  
  174. public void actionPerformed(ActionEvent arg0) {
  175. Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)};
  176. model.addRow(data0);
  177. Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)};
  178. model.addRow(data1);
  179. Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)};
  180. model.addRow(data2);
  181. Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)};
  182. model.addRow(data3);
  183. System.out.println("model.getRowCount() --->" + model.getRowCount());
  184. }
  185. });
  186. JPanel southPanel = new JPanel();
  187. southPanel.add(button1);
  188. southPanel.add(button2);
  189. add(southPanel, BorderLayout.SOUTH);
  190. }
  191.  
  192. public static void main(String[] args) {
  193. RemoveAddRows frame = new RemoveAddRows();
  194. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  195. frame.pack();
  196. frame.setLocationRelativeTo(null);
  197. frame.setVisible(true);
  198. }
  199. }
  200.  
  201. model.setRowCount(0);
  202.  
  203. Object o = table.getValueAt(lead, col);
  204.  
  205. void updateTextBox() {
  206. if (selectionModel.getLeadSelectionIndex() >= table.getRowCount() ||
  207. selectionModel.getLeadSelectionIndex() < 0) return;
  208. .... // update text panel here
  209. }
  210.  
  211. public void valueChanged(...) {
  212. if (e.getValueIsAdjusting()) return;
  213. SwingUtilities.invokeLater(new Runnable() {
  214. public void run() {
  215. updateTextBox();
  216. }
  217. }));
  218. }
  219.  
  220. DefaultTableModel dm = (DefaultTableModel)table.getModel();
  221. dm.getDataVector().removeAllElements();
  222. dm.fireTableDataChanged();
  223.  
  224. String[] columnNames = {"First Name",
  225. "Last Name",
  226. "Sport",
  227. "# of Years",
  228. "Vegetarian"};
  229.  
  230. Object[][] data = {
  231. {"Kathy", "Smith",
  232. "Snowboarding", new Integer(5), new Boolean(false)},
  233. {"John", "Doe",
  234. "Rowing", new Integer(3), new Boolean(true)},
  235. };
  236.  
  237. JTable jTable = new JTable();
  238. jTable.setModel(new DefaultTableModel(data,columnNames));
  239.  
  240. DefaultTableModel model = (DefaultTableModel)table.getModel();
  241.  
  242. while(model.getRowCount() > 0){
  243. for(int i = 0 ; i < model.getRowCount();i++){
  244. model.removeRow(i);
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement