Printable not working properly

前端 未结 1 1295
余生分开走
余生分开走 2020-12-12 06:52

I have a system where the user can register students, subjects, and the student grade in each subject, together with the topic and the date. You can search a particular stud

1条回答
  •  青春惊慌失措
    2020-12-12 07:15

    With your newly modify JTable, you should only need to call it's print method.

    DefaultTableModel dtm = new DefaultTableModel(new String[] { "Column 1" }, 1);
    JTable jTable2 = new JTable(dtm) {
        @Override
        public Printable getPrintable(PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) {
            return new TablePrintable(this, printMode, headerFormat, footerFormat);
        }
    };
    
    try{
        jTable2.print(JTable.PrintMode.NORMAL, header, null);
    }
    catch(java.awt.print.PrinterException e){
        System.out.println("error");
    }
    

    Because you've overridden the getPrintable method to return your own implementation, this is what will be used to physically print the table...

    Updated

    The header text needs to be separated by a \n, for example...

    MessageFormat header = new MessageFormat("Testing, 01\n02\n03");
    

    Which can produce...

    enter image description here

    Updated

    As near as I can tell, without been able to fully run the code, your print code should look something like...

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    
        Vector vetColuna = new Vector();
        Vector vetLinhas = new Vector();
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(str_conn, usuario, senha);
            stmt = con.createStatement();
            sql = "select topico, nota, datanota from notas where notas.cod_aluno =" + jTextField1.getText() + " and notas.cod_curso =" + jTextField2.getText() + " order by notas.datanota";
            rs = stmt.executeQuery(sql);
            if (rs == null) {
                return;
            }
            ResultSetMetaData rsmd;
            rsmd = rs.getMetaData();
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                vetColuna.add(rsmd.getColumnLabel(i + 1));
            }
    
            while (rs.next()) {
                Vector vetLinha = new Vector();
                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    vetLinha.add(rs.getObject(i + 1));
                }
                vetLinhas.add(vetLinha);
            }
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Erro\nNão foi possível carregar o driver.");
            System.out.println("Nao foi possivel carregar o driver");
            ex.printStackTrace();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro\nCertifique-se de que todos os\ncampos estejam preenchidos corretamente.");
            System.out.println("Problema com o SQL");
            ex.printStackTrace();
        }
    
        MessageFormat header = new MessageFormat("Ficha Pedagógica - " + jComboBox1.getSelectedItem() + "\nNome do Aluno - " + jTextField1.getText());
    
        DefaultTableModel dtm = new DefaultTableModel(vetLinhas, vetColuna);
        JTable jTable2 = new JTable(dtm) {
            @Override
            public Printable getPrintable(PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) {
                return new TablePrintable(this, printMode, headerFormat, footerFormat);
            }
        };
        try {
            jTable2.setSize(jTable2.getPreferredSize());
            JTableHeader tableHeader = jTable2.getTableHeader();
            tableHeader.setSize(tableHeader.getPreferredSize());
            jTable2.print(JTable.PrintMode.FIT_WIDTH);
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题