Projekt z NetBeans. Ma rysować tabelkę. na razie nie rysuje jeszcze danych, ale tylko krawędzie tabeli, a raczej powinno rysowac krawędzie tabeli, bo nie chce ich rysować. Ma ktoś pomysł dlaczego?
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.print.*;
import javax.print.attribute.*;
/**
* Okno dialogowe. Służy do drukowania dokumentu.
*/
public class JPrintDialog extends javax.swing.JDialog implements Printable
{
/** Creates new form JPrintDialog */
public JPrintDialog(java.awt.Frame parent, LabTableModel tableModel)
{
super(parent);
setModal(true);
initComponents();
this.tableModel = tableModel;
try
{
for (Doctor doctor : Config.getInstance().loadDoctors())
doctorBox.addItem(doctor);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e, "Błąd!", JOptionPane.ERROR_MESSAGE);
}
doctorBox.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
actualizeTable();
}
}
);
actualizeTable();
}
public void actualizeTable()
{
DefaultTableModel defaultModel =
new DefaultTableModel()
{
public boolean isCellEditable(int row, int column)
{
return false;
}
};
defaultModel.setColumnIdentifiers(new Object[]{"Imię", "Nazwisko", "PESEL", "Badanie", "Cena", "Dzień"});
// Wiersze
for (int i = 0; i < tableModel.getRowCount(); i++)
{
Row row = tableModel.getRow(i);
if (!row.doctor.equals(doctorBox.getSelectedItem())) continue;
// Tworzy nowy wiersz dla poszczególnych badań
for (int j = 0; j < row.tests.length; j++)
{
Object[] vector = new Object[6];
for (int k = 0; k < vector.length; k++)
vector[k] = "";
// Dla pierszego wiersza wypisuje dane pacjenta
if (j == 0)
{
vector[0] = row.name;
vector[1] = row.surname;
vector[2] = row.PESEL;
vector[5] = row.day + "";
}
// Dla nastepnych tylko badania
vector[3] = row.tests[j];
vector[4] = row.prices[j];
defaultModel.addRow(vector);
}
}
table.setModel(defaultModel);
}
public int drawTable(Graphics2D g2, Dimension dimension, JTable table, Object[] summary, String header, String footer) throws PrinterException
{
if (isDone) return Printable.NO_SUCH_PAGE;
if (table.getRowCount() == 0)
throw new PrinterException();
Font font = g2.getFont();
FontRenderContext fontContext = g2.getFontRenderContext();
float fontHeight = font.getLineMetrics("A", fontContext).getHeight();
double[] columnWidths = new double[table.getColumnCount()];
// Kolumny
for (int i = 0; i < table.getColumnCount(); i++)
{
double[] widths = new double[table.getRowCount()];
// Wiersze
for (int j = 0; j < table.getRowCount(); j++)
{
widths[j] = font.getStringBounds(table.getValueAt(j, i) + "", fontContext).getWidth();
}
Arrays.sort(widths);
columnWidths[i] = widths[0];
}
double tableWidth = 0;
for (double value : columnWidths)
tableWidth += value;
if (tableWidth > dimension.width)
throw new PrinterException();
columnWidths[table.getColumnCount() - 1] += dimension.width - tableWidth;
// TODO Rysuje nagłówek
float headerHeight = 0;
float yPoint = headerHeight + FREE_SPACE;
float currHeight = 0;
// Wiersze
for (; currRow < table.getRowCount(); currRow++)
{
if (currHeight + fontHeight > dimension.height)
break;
if (currRow == 0)
g2.drawLine(0, (int)yPoint, dimension.width, (int)yPoint);
yPoint += fontHeight;
for (int j = 0; j < table.getColumnCount(); j++)
{
g2.drawLine((int)columnWidths[j], (int)(yPoint - fontHeight), (int)columnWidths[j], (int)yPoint);// Boczna linia
}
g2.drawLine(0, (int)(yPoint - fontHeight), 0, (int)yPoint);// Pierwsza linia z lewej
g2.drawLine(0, (int)yPoint, dimension.width, (int)yPoint);// Dolna linia
currHeight += fontHeight;
}
if (currRow == table.getRowCount())
isDone = true;
// Rysuje podsumowanie
// TODO Rysuje stopkę
return Printable.PAGE_EXISTS;
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setFont(new Font("Dialog", Font.PLAIN, 12));
g2.translate(pf.getImageableX(), pf.getImageableY());
return drawTable(g2, new Dimension((int)pf.getImageableWidth(), (int)pf.getImageableHeight()), table, null, "", "");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents()
{
doctorLabel = new javax.swing.JLabel();
doctorBox = new javax.swing.JComboBox();
cancelButton = new javax.swing.JButton();
printButton = new javax.swing.JButton();
scrollPane = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
doctorLabel.setText("Lekarz:");
cancelButton.setText("Anuluj");
cancelButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
cancelAction(evt);
}
});
printButton.setText("Drukuj");
printButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
printAction(evt);
}
});
table.setModel(new DefaultTableModel());
scrollPane.setViewportView(table);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(doctorLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(doctorBox, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(printButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(doctorLabel)
.addComponent(doctorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(printButton)
.addComponent(cancelButton))
.addContainerGap())
);
pack();
}// </editor-fold>
private void printAction(java.awt.event.ActionEvent evt)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
try
{
if (job.printDialog())
job.print();
}
catch (Exception e)
{
System.out.println(e);
}
}
private void cancelAction(java.awt.event.ActionEvent evt)
{
setVisible(false);
}
// Variables declaration - do not modify
private javax.swing.JButton cancelButton;
private javax.swing.JComboBox doctorBox;
private javax.swing.JLabel doctorLabel;
private javax.swing.JButton printButton;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTable table;
// End of variables declaration
private LabTableModel tableModel;
private int currRow;
private boolean isDone;
private final int FREE_SPACE = 0;
}