別のオプションはデフォルトJLabel
切り捨て機能を使用するために、代わりにJTextField
のCardLayout
とフォーカス可能JLabel
を使用することです:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextAreaExpandTest {
private static final String TEXT =
"The text can be anything, so when the text is too long," +
" automatically add '...' at the end.";
public JComponent makeUI() {
CardLayout cardLayout = new CardLayout();
JPanel cp = new JPanel(cardLayout);
JTextArea textArea = new JTextArea(TEXT, 5, 10) {
@Override public void updateUI() {
super.updateUI();
setLineWrap(true);
setWrapStyleWord(true);
setMargin(new Insets(1, 1, 1, 1));
}
};
JLabel textField = new JLabel(" ") {
@Override public void updateUI() {
super.updateUI();
setOpaque(true);
setFocusable(true);
setBackground(UIManager.getColor("TextField.background"));
setForeground(UIManager.getColor("TextField.foreground"));
setBorder(UIManager.getBorder("TextField.border"));
}
};
textArea.addFocusListener(new FocusAdapter() {
@Override public void focusLost(FocusEvent e) {
String text = textArea.getText();
textField.setText(text.isEmpty() ? " " : text);
cardLayout.show(cp, "TextField");
}
});
textField.addFocusListener(new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
cardLayout.show(cp, "TextArea");
textArea.requestFocusInWindow();
}
});
textField.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
cardLayout.show(cp, "TextArea");
textArea.requestFocusInWindow();
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(textField, BorderLayout.NORTH);
JScrollPane scroll = new JScrollPane(
textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
cp.add(panel, "TextField");
cp.add(scroll, "TextArea");
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(32, 32, 32, 32));
p.add(cp, BorderLayout.NORTH);
p.add(new JButton("focus dummy"), BorderLayout.SOUTH);
return p;
}
// //TEST: JTextArea"setRows(...)
// public JComponent makeUI2() {
// JPanel p = new JPanel(new BorderLayout());
// JTextArea textArea = new JTextArea("", 1, 10);
// textArea.setLineWrap(true);
// textArea.addFocusListener(new FocusListener() {
// @Override public void focusGained(FocusEvent e) {
// JTextArea ta = (JTextArea) e.getComponent();
// ta.setRows(8);
// p.revalidate();
// }
// @Override public void focusLost(FocusEvent e) {
// JTextArea ta = (JTextArea) e.getComponent();
// ta.setRows(1);
// p.revalidate();
// }
// });
// JScrollPane scroll = new JScrollPane(
// textArea,
// ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// p.add(scroll, BorderLayout.NORTH);
// p.add(new JButton("focus dummy"), BorderLayout.SOUTH);
// p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// return p;
// }
public static void main(String... args) {
EventQueue.invokeLater(() -> {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TextAreaExpandTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
はい、おそらく、 'JTextArea'の' setRows'メソッドを見ることができます。 'JTextPane'はもっと難しいでしょう – MadProgrammer