Я пытаюсь поместить JLabel и JTextArea в JPanel. Есть несколько требований для этого JTextArea:
1. JTextArea должен иметь постоянную ширину (приблизительно 680 пикселей).
2. Высота должна быть относительно содержания. Контент может быть разным.
JPanel compilationErrorInfoPanel = new JPanel();
compilationErrorInfoPanel.setBackground(this.getBackground().darker());
compilationErrorInfoPanel.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, this.getBackground()));
JTextArea jTextArea = new JTextArea(errorInfo);
jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
jTextArea.setWrapStyleWord(true);
jTextArea.setBackground(compilationErrorInfoPanel.getBackground());
jTextArea.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
jTextArea.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, jTextArea.getBackground()));
FontMetrics metrics = getFontMetrics(jTextArea.getFont());
int columnWidth = metrics.charWidth('m');
jTextArea.setColumns(680/columnWidth);
JLabel errorLabel = new JLabel(new ImageIcon(getClass().getResource(Configuration.getProperties().getString("jresource.icon"))));
compilationErrorInfoPanel.add(errorLabel);
compilationErrorInfoPanel.add(jTextArea);
compilationErrorInfoPanel.setPreferredSize(compilationErrorInfoPanel.getPreferredSize());
compilationErrorInfoPanel.setMaximumSize(compilationErrorInfoPanel.getPreferredSize());
compilationErrorInfoPanel.setMinimumSize(compilationErrorInfoPanel.getPreferredSize());
Проблема, с которой я столкнулся:
если какая-либо строка содержимого длиннее ширины области, JTextArea выползет из JPanel.
Я использую BoxLayout.
Есть какое-то решение?