当前位置:和仁网 > 休闲 > 正文

java swing jtable 打印+打印预览(求代码)

2022-10-02 28

JDK 1.4+

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;

public class TextAreaRenderer extends JTextArea
implements TableCellRenderer {
private final DefaultTableCellRenderer adaptee =
new DefaultTableCellRenderer();
/** map from table to map of rows to map of column heights */
private final Map cellSizes = new HashMap();

public TextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}

public Component getTableCellRendererComponent(//
JTable table, Object obj, boolean isSelected,
boolean hasFocus, int row, int column) {
// set the colours, etc. using the standard for that platform
adaptee.getTableCellRendererComponent(table, obj,
isSelected, hasFocus, row, column);
setForeground(adaptee.getForeground());
setBackground(adaptee.getBackground());
setBorder(adaptee.getBorder());
setFont(adaptee.getFont());
setText(adaptee.getText());

// This line was very important to get it working with JDK1.4
TableColumnModel columnModel = table.getColumnModel();
setSize(columnModel.getColumn(column).getWidth(), 100000);
int height_wanted = (int) getPreferredSize().getHeight();
addSize(table, row, column, height_wanted);
height_wanted = findTotalMaximumRowSize(table, row);
if (height_wanted != table.getRowHeight(row)) {
table.setRowHeight(row, height_wanted);
}
return this;
}

private void addSize(JTable table, int row, int column,
int height) {
Map rows = (Map) cellSizes.get(table);
if (rows == null) {
cellSizes.put(table, rows = new HashMap());
}
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) {
rows.put(new Integer(row), rowheights = new HashMap());
}
rowheights.put(new Integer(column), new Integer(height));
}

/**
* Look through all columns and get the renderer. If it is
* also a TextAreaRenderer, we look at the maximum height in
* its hash table for this row.
*/
private int findTotalMaximumRowSize(JTable table, int row) {
int maximum_height = 0;
Enumeration columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn tc = (TableColumn) columns.nextElement();
TableCellRenderer cellRenderer = tc.getCellRenderer();
if (cellRenderer instanceof TextAreaRenderer) {
TextAreaRenderer tar = (TextAreaRenderer) cellRenderer;
maximum_height = Math.max(maximum_height,
tar.findMaximumRowSize(table, row));
}
}
return maximum_height;
}

private int findMaximumRowSize(JTable table, int row) {
Map rows = (Map) cellSizes.get(table);
if (rows == null) return 0;
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) return 0;
int maximum_height = 0;
for (Iterator it = rowheights.entrySet().iterator();
it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
int cellHeight = ((Integer) entry.getValue()).intValue();
maximum_height = Math.max(maximum_height, cellHeight);
}
return maximum_height;
}
}

import javax.swing.*;

public class TextAreaEditor extends DefaultCellEditor {
public TextAreaEditor() {
super(new JTextField());
final JTextArea textArea = new JTextArea();
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(null);
editorComponent = scrollPane;

delegate = new DefaultCellEditor.EditorDelegate() {
public void setValue(Object value) {
textArea.setText((value != null) ? value.toString() : "");
}
public Object getCellEditorValue() {
return textArea.getText();
}
};
}
}

import javax.swing.*;
import javax.swing.table.*;

public class TextAreaRendererTest extends JFrame {
private final JTable table = new JTable(10, 4);

public TextAreaRendererTest() {
super(System.getProperty("java.vm.version"));

// We use our cell renderer for columns 1, 2, 3
TableColumnModel cmodel = table.getColumnModel();
TextAreaRenderer textAreaRenderer = new TextAreaRenderer();

cmodel.getColumn(1).setCellRenderer(textAreaRenderer);
cmodel.getColumn(2).setCellRenderer(new TextAreaRenderer());
// I am demonstrating that you can have several renderers in
// one table, and they communicate with one another in
// deciding the row height.
cmodel.getColumn(3).setCellRenderer(textAreaRenderer);
TextAreaEditor textEditor = new TextAreaEditor();
cmodel.getColumn(1).setCellEditor(textEditor);
cmodel.getColumn(2).setCellEditor(textEditor);
cmodel.getColumn(3).setCellEditor(textEditor);

String test = "The lazy dog jumps over the quick brown fox";

for (int column = 0; column < 4; column++) {
table.setValueAt(test, 0, column);
table.setValueAt(test, 4, column);
}
test = test + test + test + test + test + test + test + test;
table.setValueAt(test, 4, 2);

getContentPane().add(new JScrollPane(table));
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TextAreaRendererTest();
}
}

我的例子 自己看看使用JTABLE做出和打印预览一样的分页效果然后PAINT出来就可以了!不懂啦。。。。。。这个很多都发过帖子了 你可以搜搜问题解决了么?
本周热门