同时使用单元格渲染器和编辑器,但滚动条仍然无效 请提出任何有效的建议 滚动条根本没有移动
类MultiRowCell扩展AbstractCellEditor实现TableCellEditor,TableCellRenderer {
private JScrollPane scrollPane;
private JTextArea textArea;
public MultiRowCell() {
textArea = new JTextArea();
textArea.setEditable(true);
scrollPane = new JScrollPane(textArea);
}
@Override
public boolean isCellEditable(EventObject e) {
System.out.println(e);
return true;
}
@Override
public Object getCellEditorValue() {
return textArea.getText();
}
protected void setCellValue(Object value) {
if (value == null) {
textArea.setText(null);
} else {
textArea.setText(value.toString());
}
textArea.setCaretPosition(0);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
setCellValue(value);
return scrollPane;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setCellValue(value);
return scrollPane;
}
}
//TextTableCellRenderer tr=new TextTableCellRenderer();
//table.getColumnModel().getColumn(1).setCellEditor(tr);
MultiRowCell re=new MultiRowCell();
table.getColumnModel().getColumn(1).setCellRenderer(re);
table.getColumnModel().getColumn(1).setCellEditor(re);
//table.getColumnModel().getColumn(1).setCellRenderer(r);
//table.getColumnModel().getColumn(1).setCellEditor(e);
//table.getColumnModel().getColumn(2).setCellRenderer(r);
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.setLayout(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.NORTH);
return panel;
答案 0 :(得分:1)
从概念上讲,您需要一个单元格渲染器和单元格编辑器才能使其正常工作,例如......
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
String text = loadText();
String[][] data = new String[1][1];
data[0][0] = text;
DefaultTableModel model = new DefaultTableModel(data, new String[]{"Example"});
JTable table = new JTable(model);
TableColumnModel columnModel =table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
MultiRowCell multiRowCell = new MultiRowCell();
column.setCellEditor(multiRowCell);
column.setCellRenderer(multiRowCell);
int height = multiRowCell.getTableCellRendererComponent(table, "Test", true, true, 0, 0).getPreferredSize().height;
table.setRowHeight(height);
add(new JScrollPane(table));
}
protected String loadText() {
StringBuilder sb = new StringBuilder(1024);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("Script.txt")))) {
String text = null;
while ((text = reader.readLine()) != null) {
sb.append(text).append(System.lineSeparator());
}
} catch (IOException ex) {
ex.printStackTrace();
}
return sb.toString();
}
}
public class MultiRowCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private JScrollPane scrollPane;
private JTextArea textArea;
public MultiRowCell() {
textArea = new JTextArea(25, 50);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
}
@Override
public boolean isCellEditable(EventObject e) {
System.out.println(e);
return true;
}
@Override
public Object getCellEditorValue() {
return textArea.getText();
}
protected void setCellValue(Object value) {
if (value == null) {
textArea.setText(null);
} else {
textArea.setText(value.toString());
}
textArea.setCaretPosition(0);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
setCellValue(value);
return scrollPane;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setCellValue(value);
return scrollPane;
}
}
}
注意,我从本地资源中读取了整个Rouge One脚本的示例,您需要提供自己的文本
警告:此示例将要求您单击单元格以切换到编辑模式,因为包裹JTable
的滚动窗格使用了鼠标滚轮事件