Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. table = new JTable();
  2.  
  3. table.addMouseListener(new MouseAdapter() {
  4. @Override
  5. public void mousePressed(MouseEvent e) {
  6. if (e.getClickCount() == 1)
  7. {
  8. String selectedData = null;
  9.  
  10. JTable target = (JTable)e.getSource();
  11.  
  12. int selectedRow = target.getSelectedRow();
  13. int selectedColumn = target.getSelectedColumn();
  14.  
  15. System.out.println("Selected row "+ " "+ selectedRow);
  16. System.out.println("Selected column"+ " "+ selectedColumn);
  17.  
  18. Object selectedCellValue=target.getValueAt(selectedRow, selectedColumn);
  19. System.out.println("selectedCellValue "+" "+selectedCellValue);
  20. }
  21. }
  22. });
  23. table.setBounds(10, 44, 0, 0);
  24. getContentPane().add(table);
  25. }
  26.  
  27. {
  28. {
  29. table.setBounds(10, 11, 560, 311);
  30.  
  31. }
  32.  
  33. package matheus.arruda.lab.wn.tratermik;
  34.  
  35. import java.awt.BorderLayout;
  36.  
  37. public class abrirospop extends JDialog {
  38. private JTable table;
  39. private JTextField txtpesquisar;
  40.  
  41. Vector originalTableModel;
  42.  
  43. /**
  44. * Launch the application.
  45. */
  46. public static void main(String[] args) {
  47. try {
  48. abrirospop dialog = new abrirospop();
  49. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  50. dialog.setVisible(true);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. /**
  57. * Create the dialog.
  58. * @throws ClassNotFoundException
  59. */
  60. public abrirospop() throws ClassNotFoundException {
  61. setIconImage(Toolkit.getDefaultToolkit().getImage(abrirospop.class.getResource("/matheus/arruda/lab/wn/tratermik/icones/Application-Form-Magnify.png")));
  62. setTitle("Abrir OS");
  63. setBounds(100, 100, 596, 420);
  64. getContentPane().setLayout(null);
  65. {
  66. table = new JTable();
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. table.addMouseListener(new MouseAdapter() {
  74. @Override
  75. public void mousePressed(MouseEvent e) {
  76. if (e.getClickCount() == 1)
  77. {
  78. String selectedData = null;
  79.  
  80. JTable target = (JTable)e.getSource();
  81.  
  82. int selectedRow = target.getSelectedRow();
  83. int selectedColumn = target.getSelectedColumn();
  84.  
  85. System.out.println("Selected row "+ " "+ selectedRow);
  86. System.out.println("Selected column"+ " "+ selectedColumn);
  87.  
  88. Object selectedCellValue=target.getValueAt(selectedRow, selectedColumn);
  89. System.out.println("selectedCellValue "+" "+selectedCellValue);
  90. }
  91. }
  92. });
  93. table.setBounds(10, 44, 0, 0);
  94. getContentPane().add(table);
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101. {
  102. {
  103. table.setBounds(10, 11, 560, 311);
  104.  
  105. }
  106.  
  107. txtpesquisar = new JTextField();
  108.  
  109. txtpesquisar.getDocument().addDocumentListener(new DocumentListener() {
  110. public void changedUpdate(DocumentEvent e) {
  111. try {
  112. warn();
  113. } catch (ClassNotFoundException e1) {
  114. // TODO Auto-generated catch block
  115. e1.printStackTrace();
  116. }
  117. }
  118. public void removeUpdate(DocumentEvent e) {
  119. try {
  120. warn();
  121. } catch (ClassNotFoundException e1) {
  122. // TODO Auto-generated catch block
  123. e1.printStackTrace();
  124. }
  125. }
  126. public void insertUpdate(DocumentEvent e) {
  127. try {
  128. warn();
  129. } catch (ClassNotFoundException e1) {
  130. // TODO Auto-generated catch block
  131. e1.printStackTrace();
  132. }
  133. }
  134.  
  135.  
  136.  
  137. public void warn() throws ClassNotFoundException {
  138. String valor = txtpesquisar.getText();
  139.  
  140. String colunas[] = {"OS #N","Cliente","Responsável","Data O.S","Prazo de Entrega"};
  141.  
  142. try {
  143. Class.forName("org.sqlite.JDBC");
  144. Connection con = DriverManager.getConnection("jdbc:sqlite:C:\Tratermikos\banco de dados\tratermik.db");
  145. Statement stat = con.createStatement();
  146.  
  147. ResultSet rs;
  148.  
  149. rs = stat.executeQuery("select _id,cliente,responsavel_relatorio,data_inicio,data_fim from tratermik where cliente like ""+valor+"%"+""");
  150.  
  151. ResultSetMetaData rsmd = rs.getMetaData();
  152. int columnCount = rsmd.getColumnCount();
  153.  
  154. DefaultTableModel tm = (DefaultTableModel) table.getModel();
  155. tm.setColumnCount(0);
  156.  
  157. for(int i = 1; i <= columnCount; i++) {
  158. tm.addColumn(rsmd.getColumnName(i));
  159.  
  160. }
  161. tm.setRowCount(0);
  162. tm.addRow(colunas);
  163. while(rs.next()) {
  164. String[] a = new String[columnCount];
  165. for(int i = 0; i < columnCount; i++) {
  166. a[i] = rs.getString(i+1);
  167.  
  168. }
  169.  
  170. tm.addRow(a);
  171. }
  172. tm.fireTableDataChanged();
  173.  
  174.  
  175.  
  176.  
  177.  
  178. rs.close();
  179. con.close();
  180. } catch (SQLException sqle) {
  181. JOptionPane.showMessageDialog(new JFrame(), sqle + "n"+"Por favor contate o desenvolvedor ou o administrador do sistema.","Error",JOptionPane.ERROR_MESSAGE);
  182.  
  183. sqle.printStackTrace();
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. }});
  194.  
  195. txtpesquisar.setBounds(10, 350, 161, 20);
  196. getContentPane().add(txtpesquisar);
  197. txtpesquisar.setColumns(10);
  198.  
  199. JLabel lblFiltrarPorCliente = new JLabel("Filtrar por Cliente");
  200. lblFiltrarPorCliente.setBounds(38, 333, 99, 17);
  201. getContentPane().add(lblFiltrarPorCliente);
  202. String colunas[] = {"OS #N","Cliente","Responsável","Data O.S","Prazo de Entrega"};
  203.  
  204. table.setPreferredScrollableViewportSize(Toolkit.getDefaultToolkit().getScreenSize());
  205. try {
  206. Class.forName("org.sqlite.JDBC");
  207. Connection con = DriverManager.getConnection("jdbc:sqlite:C:\Tratermikos\banco de dados\tratermik.db");
  208. Statement stat = con.createStatement();
  209.  
  210. ResultSet rs;
  211.  
  212. //rs = stat.executeQuery("select _id,cliente, responsavel_relatorio,data_inicio,data_fim from tratermik");
  213. rs = stat.executeQuery("select * from clientes");
  214. ResultSetMetaData rsmd = rs.getMetaData();
  215. int columnCount = rsmd.getColumnCount();
  216.  
  217. DefaultTableModel tm = (DefaultTableModel) table.getModel();
  218. tm.setColumnCount(0);
  219.  
  220. for(int i = 1; i <= columnCount; i++) {
  221. tm.addColumn(rsmd.getColumnName(i));
  222.  
  223. }
  224. tm.setRowCount(0);
  225. tm.addRow(colunas);
  226. while(rs.next()) {
  227. String[] a = new String[columnCount];
  228. for(int i = 0; i < columnCount; i++) {
  229. a[i] = rs.getString(i+1);
  230.  
  231. }
  232.  
  233. tm.addRow(a);
  234. }
  235. tm.fireTableDataChanged();
  236.  
  237.  
  238.  
  239.  
  240.  
  241. rs.close();
  242. con.close();
  243. } catch (SQLException sqle) {
  244. JOptionPane.showMessageDialog(new JFrame(), sqle + "n"+"Por favor contate o desenvolvedor ou o administrador do sistema.","Error",JOptionPane.ERROR_MESSAGE);
  245.  
  246. sqle.printStackTrace();
  247. }
  248.  
  249.  
  250.  
  251. }
  252.  
  253.  
  254. }
  255.  
  256. private class HighlightRenderer extends DefaultTableCellRenderer {
  257.  
  258. @Override
  259. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  260.  
  261. // everything as usual
  262. super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  263.  
  264. // added behavior
  265. if(row == table.getSelectedRow()) {
  266.  
  267. // this will customize that kind of border that will be use to highlight a row
  268. setBorder(BorderFactory.createMatteBorder(2, 1, 2, 1, Color.BLACK));
  269. }
  270.  
  271. return this;
  272. }
  273. }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement