I'm trying to get a JTextPane
to adjust its height according to whatever content I feed it. All I can do is to set a fixed height in pixels using Dimension.
我试图让JTextPane根据我提供的任何内容来调整它的高度。我所能做的就是使用Dimension以像素为单位设置固定高度。
How do I make the JTextPane
collapse/expand so it will fit to contents?
如何使JTextPane折叠/展开以使其适合内容?
I might add that I use this in a GridBagLayout
'ed JPanel
that has been added to a JScrollPane
.
我可以补充一点,我在添加到JScrollPane的GridBagLayout'ed JPanel中使用它。
2 个解决方案
#1
0
I don't know any direct way to do it, but this may give you some ideas:
我不知道有什么直接的方法,但这可能会给你一些想法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextPanePerfectSize extends JFrame
{
JTextField textField;
JTextPane textPane;
public TextPanePerfectSize()
{
textField = new JTextField(20);
textField.setText("add text");
getContentPane().add(textField, BorderLayout.NORTH );
textField.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Document doc = textPane.getDocument();
doc.insertString(doc.getLength(), " " + textField.getText(), null);
textField.setText("");
Dimension d = textPane.getPreferredSize();
Rectangle r = textPane.modelToView( textPane.getDocument().getLength() );
d.height = r.y + r.height;
textPane.setPreferredSize( d );
getContentPane().validate();
// pack();
}
catch(Exception e2) {}
}
});
JLabel label = new JLabel("Hit Enter to Add Text to Text Pane");
getContentPane().add(label);
JPanel south = new JPanel();
textPane = new JTextPane();
textPane.setText("Some text");
textPane.setEditable( false );
textPane.setPreferredSize( new Dimension(120, 23) );
south.add( textPane );
getContentPane().add(south, BorderLayout.SOUTH);
}
public static void main(String[] args)
{
JFrame frame = new TextPanePerfectSize();
frame.setSize(200, 200);
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
#2
0
JTextPane tp = new JTextPane();
setLayout(new BorderLayout()); // set layout on parent
add(tp, BorderLayout.CENTER);
This works for me.
这对我有用。
#1
0
I don't know any direct way to do it, but this may give you some ideas:
我不知道有什么直接的方法,但这可能会给你一些想法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextPanePerfectSize extends JFrame
{
JTextField textField;
JTextPane textPane;
public TextPanePerfectSize()
{
textField = new JTextField(20);
textField.setText("add text");
getContentPane().add(textField, BorderLayout.NORTH );
textField.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Document doc = textPane.getDocument();
doc.insertString(doc.getLength(), " " + textField.getText(), null);
textField.setText("");
Dimension d = textPane.getPreferredSize();
Rectangle r = textPane.modelToView( textPane.getDocument().getLength() );
d.height = r.y + r.height;
textPane.setPreferredSize( d );
getContentPane().validate();
// pack();
}
catch(Exception e2) {}
}
});
JLabel label = new JLabel("Hit Enter to Add Text to Text Pane");
getContentPane().add(label);
JPanel south = new JPanel();
textPane = new JTextPane();
textPane.setText("Some text");
textPane.setEditable( false );
textPane.setPreferredSize( new Dimension(120, 23) );
south.add( textPane );
getContentPane().add(south, BorderLayout.SOUTH);
}
public static void main(String[] args)
{
JFrame frame = new TextPanePerfectSize();
frame.setSize(200, 200);
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
#2
0
JTextPane tp = new JTextPane();
setLayout(new BorderLayout()); // set layout on parent
add(tp, BorderLayout.CENTER);
This works for me.
这对我有用。