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

JTABLE中有合并的单元格,要考虑到合并单元格分页打印的情况!... JTABLE中有合并的单元格,要考虑到合并单元格分页打印的情况! 展开
 我来答
ToDooo
2012-09-26 · TA获得超过1237个赞
知道小有建树答主
回答量:671
采纳率:100%
帮助的人:207万
展开全部
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();
}
}

我的例子 自己看看
ttyaomm
2012-09-29
知道答主
回答量:23
采纳率:0%
帮助的人:20万
展开全部
使用JTABLE做出和打印预览一样的分页效果然后PAINT出来就可以了!
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
由马水方乎c
2012-09-26
知道答主
回答量:22
采纳率:0%
帮助的人:2.1万
展开全部
不懂啦。。。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
丨好好学习time
2012-09-26 · TA获得超过323个赞
知道答主
回答量:32
采纳率:0%
帮助的人:6.3万
展开全部
这个很多都发过帖子了 你可以搜搜
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
gwzh159
2020-05-20
知道答主
回答量:1
采纳率:0%
帮助的人:616
展开全部
问题解决了么?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 5条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式