I have a JTextPane, when there are too many lines, a vertical scroll bar appears, but when a line is too long, instead of appearing a horizontal scroll bar, the line breaks into two lines, how to make the horizontal bar appear instead of breaking into two lines, my jTextPane is added like this:
JScrollPane jScrollPane1 = new JScrollPane();
jScrollPane1.setViewportView(jTextPane1);
As presented here by our very own Rob Camick, you could try using something like...
JTextPane tp = new JTextPane() {
#Override
public boolean getScrollableTracksViewportWidth() {
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};
Which will stop line/word wrapping
Related
I have a JPanel with several JLabels in it and a JTextPane. I want them to be below each other (so no two two labels on the same line), and aligned to the left. I have tried several things:
Using a BoxLayout with BoxLayout.Y_AXIS works for properly getting all elements below each other. However, while the JTextPane correctly aligns to the left, the JLabels stay centered, even when calling several methods to try and get the alignment to the left (see code below).
Using a GridLayout will correctly put the elements below each other and align them to the left, but then the elements will be vertically spread all over the JPanel with huge spaces between the text lines. I want all elements to be in the top of the panel as far as possible.
private final void init() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JLabel("a"));
final JLabel label = new JLabel();
final JTextPane pane = new JTextPane();
add(label);
add(pane);
// these three lines seem to be ignored
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setAlignmentX(LEFT_ALIGNMENT);
label.setHorizontalTextPosition(SwingConstants.LEFT);
pane.setForeground(Color.RED);
pane.setEditable(false);
}
How can I get this right?
I want them to be below each other (so no two two labels on the same line), and aligned to the left.
You need to set the alignment on all components, even the JTextPane.
add(new JLabel("a"));
How do you expect to change the alignment of that label when you don't have a reference to it?
final JLabel label = new JLabel();
final JTextPane pane = new JTextPane();
add(label);
add(pane);
You don't set the alignment of the above components.
Read the section from the Swing tutorial on Fixing Alignment Problems
I am new to GWT and have made 3 textarea objects and have added them to a vertical panel, which is also added to my rootpanel. However, I cannot seem to input any text in these textareas. Any suggestions?
VerticalPanel panel = new VerticalPanel();
TextArea tb = new TextArea();
TextArea tb1 = new TextArea();
TextArea tb2 = new TextArea();
panel.add(tb);
panel.add(tb1);
panel.add(tb2);
RootPanel.get().add(panel);
I would try enabling them:
tb.setEnabled(true)
tb1.setEnabled(true)
tb2.setEnabled(true)
But I don't think that should be necessary.
There might be something small you are missing, I would compare all of your code to this. It seems to be a good working example that you could compare your code to and see if you missed a small step.
It seems you may need to add the TextArea objects to horizontal panels and then add those horizontal panels to the vertical panel.
The problem you describe maybe caused by adding another widget on top of your TextArea widgets. In this case TextArea widget may remain visible, but it will be unusable.
I don't see it in the code snippet that you provided, but maybe it's not all of your code.
Try this. It is the example straight from the GWT Javadoc.
Maybe you need to use setCharacterWidth(int size) and setVisibleLines(int size) before adding it.
public class TextBoxExample implements EntryPoint {
public void onModuleLoad() {
//Make an 80 x 50 TextArea
TextArea ta = new TextArea();
ta.setCharacterWidth(80);
ta.setVisibleLines(50);
// Add them to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.add(ta);
RootPanel.get().add(panel);
}
}
I am adding a JPanel in a JScrollPane in my project.
All is working fine, but there is one problem about mouse scroll using the mouse-Wheel in JPanel. It's speed is very slow on scrolling. How to make it faster?
My code is :
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
objCheckBoxList = new CheckBoxList();
BaseTreeExplorer node = (BaseTreeExplorer)projectMain.objCommon.tree.getLastSelectedPathComponent();
if (node.getObject() != null) {
cmbList.setSelectedItem(node.getParent().toString());
} else {
if (node.toString().equalsIgnoreCase("List of attributes")) {
cmbList.setSelectedIndex(0);
} else {
cmbList.setSelectedItem(node.toString());
}
}
panel.add(objCheckBoxList);
JScrollPane myScrollPanel = new JScrollPane(panel);
myScrollPanel.setPreferredSize(new Dimension(200, 200));
myScrollPanel.setBorder(BorderFactory.createTitledBorder("Attribute List"));
You can set your scrolling speed with this line of code myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);
Here is details.
This bug seems to occur because swing interprets the scroll speed in pixels instead of lines of text. If you are looking for a more accessible alternative to the accepted solution, you can use the following function to calculate and set the actual desired scroll speed in pixels:
public static void fixScrolling(JScrollPane scrollpane) {
JLabel systemLabel = new JLabel();
FontMetrics metrics = systemLabel.getFontMetrics(systemLabel.getFont());
int lineHeight = metrics.getHeight();
int charWidth = metrics.getMaxAdvance();
JScrollBar systemVBar = new JScrollBar(JScrollBar.VERTICAL);
JScrollBar systemHBar = new JScrollBar(JScrollBar.HORIZONTAL);
int verticalIncrement = systemVBar.getUnitIncrement();
int horizontalIncrement = systemHBar.getUnitIncrement();
scrollpane.getVerticalScrollBar().setUnitIncrement(lineHeight * verticalIncrement);
scrollpane.getHorizontalScrollBar().setUnitIncrement(charWidth * horizontalIncrement);
}
Note that swing does calculate the scroll speed correctly when it contains a single component like a JTable or JTextArea. This fix is specifically for when your scroll pane contains a JPanel.
JScrollBar _horizontalScroll;
_verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
_horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(true);
I have a code shown above, here vertical scroll bar is working fine, but horizontal scroll bar is not working (doesn't appear on my Swing GUI).
You never add your horizontal scrollbar.
Try adding the entire panel into the JScrollPane.
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
And you can add your listeners using
scrollPane.getHorizontalScrollBar().addAdjustmentListener(this);
I'm using Java Swing. I have a textarea in a panel. I don't need a horizontal scrollbar for that textArea, only a vertical scrollbar is needed. I disabled auto scrollbar options, but still the horizontal scrollbar is working. Please help me in thz.
ta.setLineWrap(true)
Sets the line-wrapping policy of the
text area. If set to true the lines
will be wrapped if they are too long
to fit within the allocated width. If
set to false, the lines will always be
unwrapped
Scroll bar comes into text area when you are making your text area too small. This is because your the default column number in netbeans text area is 20.
If you don't want the scroll bar being displayed , then take the textarea properties and change the column number to a value according to your size (say 10).
And the scrollbar will not be shown.
Two examples, the line wrap method and the scroll pane method:
public class Test {
public static void main(String[] args) {
// example text
String rep = "The quick brown fox jumps over the lazy dog.";
String all = rep;
for(int i = 0; i < 100; i++)
all += "\n" + rep;
// create the line wrap example
JTextArea first = new JTextArea(all);
first.setLineWrap(true);
// create the scroll pane example
JScrollPane second =
new JScrollPane(new JTextArea(all),
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// lay it out
JFrame f = new JFrame("Test");
f.setLayout(new GridLayout(1,2));
f.add(first);
f.add(second);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
JTextPane don't have a method to enable or disable line wrap, one best choice is:
private JTextPane noWrapTextPane = new JTextPane() {
#Override
public boolean getScrollableTracksViewportWidth() {
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};