Reputation: 12283
I want to restrict the height of a JEditorPane inside a JScrollPane to have a maximum value, but still allow the width to expand to fit the content.
My reasoning for this is that I want a single line of text that allows me to embed components. I really want something like a JTextField with embeddable components, but since JTextField cannot do this, I'm using JEditorPane (JTextPane would be fine, too).
I'm trying to mimic the StackOverflow tag behavior in a desktop Swing application. So I will embed clickable "tags" in the JEditorPane and they'll all appear in one line, just like SO does.
As it stands, the JEditorPane expands vertically, but not horizontally with this SSCCE:
import javax.swing.*;
import java.awt.BorderLayout;
public class Tags {
/*
* This is the method I want to modify to implement this behavior:
*/
public static JComponent buildTagsComponent() {
JScrollPane scrollPane = new JScrollPane();
JEditorPane editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
return scrollPane;
}
/*
* The remainder of this code is just to make the example complete.
*/
public static JFrame buildFrame() {
JFrame frame = new JFrame("Tags example");
JPanel panel = new JPanel();
JPanel tagsPanel = new JPanel();
JLabel tagsLabel = new JLabel("Tags:");
JLabel mainContent = new JLabel("Main Content Goes Here");
tagsPanel.add(tagsLabel);
tagsPanel.add(buildTagsComponent());
panel.setLayout(new BorderLayout());
panel.add(tagsPanel,BorderLayout.SOUTH);
panel.add(mainContent,BorderLayout.CENTER);
frame.setContentPane(panel);
return frame;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
JFrame frame = buildFrame();
frame.pack();
frame.setVisible(true);
}
}
);
}
}
Note also that I plan to disable the scrollbars and require scrolling with a gesture or moving the cursor position with the cursor keys. When I added a "NEVER" for the scrollbar policies, that just made things not scrollable at all. I'm not looking for a solution for the scrolling problem right now, I just want the answers to take into account that I will be setting the scrollbar policy to NEVER for horizontal and vertical.
I've tried explicitly setting the height and width (minimum, preferred, and maximum) to something like 12 and Integer.MAX_VALUE
, respectively.
UPDATE
After some research I believe the solution I'm looking for has to do with word wrapping. I want the JEditorPane to not wrap paragraphs when there is no line break (line feed).
Upvotes: 2
Views: 851
Reputation: 4277
Currently, the editor pane does not resize horizontally because of the Layout Manager. The default for JPanel
is FlowLayout
, which sizes components at their fixed preferred size.
Try a BoxLayout
:
JPanel tagsPanel = new JPanel();
tagsPanel.setLayout(new BoxLayout(tagsPanel, BoxLayout.LINE_AXIS));
// then add components
Extra tip: To improve the look of your panel, you can add invisible components (that put space between other components), and/or an empty border (that creates a margin with the other components:
tagsPanel.add(tagsLabel);
tagsPanel.add(Box.createRigidArea(new Dimension(10,0)));
tagsPanel.add(buildTagsComponent());
tagsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JEditorPane
, unlike in a JTextArea
), a solution is proposed here (referenced from this post).
Basically you have to extend a StyledEditorKit
and set it to your JEditorPane
.
JScrollPane
, maybe a solution is to use the AS_NEEDED
default setting, but customize the UI of the scroll bars to make their size equal to zero. This way you have the scrolling behaviour without the scroll bars themselves.
Upvotes: 2
Reputation: 1612
Try this in the JScrollPane initialization:
JScrollPane scrollBar = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Upvotes: 0