title: Printing selected rows from JTable
body: <p>I'm making a java swing application, I'm also a newbie programer (1 and a half years programing).</p>
  
  <p>I'm trying to code inside a button a way to print (in a printer, not in console) the preselected rows (by a user) of a jTable, which has been printed (in the window) by a Query before.</p>
  
  <p>This is my code (inside the button), I know it's worng. I thought , the way to do this was copying in a temp jTable all the selected rows, and then print the temp jTable, but i can't achieve this.</p>
  
  <pre><code>    MessageFormat header = new MessageFormat("- {TABLE} -"); 
          try {
              DefaultTableModel viewModel = (DefaultTableModel) jTable.getModel();
              DefaultTableModel printModel = new DefaultTableModel(viewModel.getRowCount(), viewModel.getColumnCount());
              JTable toPrint = new JTable(printModel);
  
              int[] selectedRows = jTable.getSelectedRows(); 
              for (int r=0;r&lt;selectedRows.length;r++){
                  toPrint.addRowSelectionInterval(r, r);
              }
  
  
              toPrint.print(JTable.PrintMode.FIT_WIDTH,header,null);
  
          } catch (java.awt.print.PrinterException e1) {
               System.err.format("Cannot print %s%n", e1.getMessage()); 
          }
  </code></pre>
  
  <p>To sum up, I just want to print (in a printer) the selected rows of an existing jTable.</p>
  
  <p>I also tryed this -> (<a href="https://stackoverflow.com/questions/26580954/how-to-print-selected-rows-jtable">how to print selected rows JTable</a>) and other possible solutions, but I can't make them work for me.</p>