固定幅を指定したJTextPaneの推奨サイズ/高さを簡単に計算する方法について質問があります。私がこれまで行ってきたことは、JTextPaneをJScrollPaneに配置し、テキストを更新するたびにJScrollPaneのサイズを更新します。今、これはうまくいきます(私のコードはちょっとねじれていますが、うまくいきますが)。新しい行を追加すると、スクロールペインの高さを更新するコードを2回呼び出す必要があります。invokeLater
。私はinvokeLater()
を避ける方法を探しています。何でもなど、コンポーネントのイベントをディスパッチSwingメソッドをオーバーライド含めて、行います...ここで固定幅のJTextPaneの推奨サイズ
は、このすべてを示しsnipetです:
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Test {
private static MyEventQueue queue;
private static final class MyEventQueue extends EventQueue {
public boolean log = false;
@Override
protected void dispatchEvent(AWTEvent event) {
if (log) {
System.err.println(event.getClass().getName() + " " + event.getSource());
}
super.dispatchEvent(event);
}
}
public static class WrapApp extends JFrame {
JTextPane edit = new JTextPane() {
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
};
private JScrollPane comp;
protected void updateVPSize() {
updateSize(true);
}
protected void updateSize(boolean repeat) {
edit.setSize(150, 1000);
Dimension size = edit.getPreferredScrollableViewportSize();
System.err.println("Before " + size);
size.width = 150;
comp.setSize(size);
if (repeat) {
queue.log = true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
queue.log = false;
updateSize(false);
}
});
}
}
public WrapApp() {
super("Forced wrap/no wrap example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
edit.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
updateVPSize();
}
@Override
public void insertUpdate(DocumentEvent e) {
updateVPSize();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateVPSize();
}
});
comp = new JScrollPane(edit) {
@Override
public void setSize(int width, int height) {
super.setSize(width, height);
};
};
comp.setBorder(null);
comp.setLocation(0, 0);
comp.setViewportBorder(null);
comp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
edit.setText("Some long text that needs to be wrapped on several lines.\n\nBut this is not the end of it, it can go on and on and on and on...Some long text that needs to be wrapped on several lines.\n\nBut this is not the end of it, it can go on and on and on and on...");
getContentPane().add(comp);
setSize(300, 700);
setLocationRelativeTo(null);
updateVPSize();
}
}
public static void main(String[] args) {
Toolkit.getDefaultToolkit().getSystemEventQueue().push(queue = new MyEventQueue());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
WrapApp m = new WrapApp();
m.setVisible(true);
}
});
}
}
本当にに残されたすべてのstuffsを気にしないでください。そこに、私は
あなたがJScrollPaneのために設定したいですなぜ、重要な理由は、興味深い質問のためにところで+1、申し訳ありませんがあるが、私はない考えであることができるかどうください。 LayoutManagerまたはaboutについてJTextComponentの内容 – mKorbel
いいえ、私がスクロールペインなしでそれを行うことができれば、それは私にとっては大丈夫です。しかし、私はコンテンツをラップするためにJTextPaneが必要です。これまでのところ、これは私が見つけた醜いしかし実用的な解決策です。 –