JScrollBar: Horizontal scrollbar not showing - java

I have JtabbedPane where every tab has a JtextPane wrapped inside a JScrollPane.
The Vertical scroll bar is showing up when necessary, but the horizontal isn't, and instead of making a horizontal bar it cuts the text and continues it in next line, which is not a point.
I have tried to resize the tab size and declaring the size of the scrollpane larger than tab's, but the result the same.
tabs = new TabbedPane();
tabs.setBounds(0, 0, windowWidth - 70, windowHight - 105);
for (Responses orgData: GUI.getOriginalRequest().getArrayOfResponses())
{
sc = StyleContext.getDefaultStyleContext();
style = sc.addStyle("ConstantWidth", null);
str = formatter.format((formatter.unformat(orgData.getXMLData())));
ksd = new KeywordStyledDocument(style, sc, s);
ksd.insertString(0, str, style);
ksd.refreshDocument();
pane = new JTextPane(ksd);
pane.setFont(new Font("Courier New", Font.PLAIN, 12));
panel = new JScrollPane(pane);
panel.getHorizontalScrollBar().addAdjustmentListener(null);
panel.setPreferredSize(new Dimension(windowWidth - 170, windowHight - 135));
orgData.setXMLData(formatter.unformat(str));
listener.addKeyAdapter(pane);
tabs.addTab(orgData.getSystemName() + " (Original Request)", panel);
}
What am I missing here?
Thank you.
Update:
After implementing the LineWrape by extending the JTextPane and adding pane.setLineWrap(false) that was sugested by Thanasis, the problem was resolved and the textPane which had a text that exceeded the bounds of the field, was wrapped and had a horizontal scroll bar, but textPane which text was less than the bounds of textPane was wrapped to the last character, and thus, the field became less than a space that is allocated to it, and it looks like panel split to 2 parts, textPane and panel.
How do I resolve this?
Please suggest.
TextPane ends at the last character image

Related

Swing labels won't align properly in BoxLayout

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

Horisontal scrollbar not working for JScrollPane

This is my plugin to the application with Miglayout:
This is MigLayout initialistion code, layout occupies 100% space of main panel:
resPanel.setLayout(new BoxLayout(resPanel, BoxLayout.Y_AXIS));
mainPanel.setLayout(new MigLayout("", "[][grow][grow][]", "[][][][grow]"));
mainPanel.add(new JLabel("Class expression"), "wrap");
mainPanel.add(owlDescriptionEditor, "growx,span 3");
mainPanel.add(calcButton, "wrap");
mainPanel.add(new JLabel("Definitions found"), "span 2");
mainPanel.add(new JLabel("Target signature"), "span 2,wrap");
JScrollPane jsp = new JScrollPane(resPanel);
mainPanel.add(jsp, "growy, growx, span 2");
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
mainPanel.add(new JScrollPane(deltaList), "growx,growy,span 2");
The problem is not working horizontal scrollbar of the left list, which is JPanel with BoxLayout, even though I call setVerticalScrollBarPolicy for it. JPanel is populated dynamically with this loop:
static void updateList(JPanel panel, Collection<OWLClassExpression> list) {
panel.removeAll();
for (int i = 0; i < 3; i++) {
for (OWLClassExpression p : list) {
String name = ((OWLNamedObject) p).getIRI().getShortForm();
JEditorPane jep = new JEditorPane("text/html", name+"QWQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
jep.setPreferredSize(new Dimension(200,20));
jep.setEditable(false);
jep.setOpaque(false);
panel.add(jep);
}
}
panel.add( Box.createVerticalStrut(400) );
}
I had to add last line to get vertical scrollbar working. If I don't add it, then its also missing, even though it has side effect of extra empty space in the bottom. How can I make scrollbars work properly?
jep.setPreferredSize(new Dimension(200,20));
Don't hardcode preferred sizes.
Each component will dynamcially determine its own preferred size based on the properties of the component (text, font etc.).
The layout manager can then dynamically determine the size of the panel based on the preferred size of each component and scrollbars will appear when required.
Also after you are finished adding all the components to the panel you need to use:
panel.revalidate();
To invoke the layout manager so the new size of the panel can be calculated.

Java JTextField view from the Right

I have a JTextField that I'm trying to get to automatically view from the Right (Not align to the right), so if the text is to long for the JTextField it will display the last characters in the String instead of the beginning.
Ive been searching for ages trying to locate an answer but keep coming up with aligning.
The 2 images below show what i get and what I'm after, the text is "123456789_123", the JTextField is only big enough to contain the "123456789" but i want to see the "56789_123" instead without having to focus on the field. (i can use something other than a JTextField if needed, tried a JTextArea but had the same issue).
What i Get
What I'm after
I can not just make the Field bigger as I'm restricted by other Objects in my program. Usually the text fits fine but every now and then its too big.
Found a work around.
you create a JScrollPane, make its vertical and horizontal bars invisible by setting there dimensions to 0,0. then scroll to end using 'setValue' to max.
hope this helps anyone trying to do something similar.
JTextArea editArea2 = new JTextArea(5,5);
editArea2.setText("123456789_12345");
editArea2.setAlignmentX(CENTER_ALIGNMENT);
asd = new Dimension();
asd.height = 20;
asd.width = 70;
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(editArea2);
scrollPane.setMinimumSize(asd);
scrollPane.setMaximumSize(asd);
scrollPane.getVerticalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getHorizontalScrollBar().setPreferredSize (new Dimension(0,0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMinimumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getVerticalScrollBar().setMaximumSize(new Dimension(0, 0));
scrollPane.getHorizontalScrollBar().setValue( scrollPane.getHorizontalScrollBar().getMaximum() );

JScrollPane acts funny inside of a JOptionsPane

I have tried to add a scroll view to a JOptionsPane, so that the information window can handle more text. It does add a scroll pane to the window. However, it acts funny on scrolling. The first visible text is shown clearly, but when you start scrolling, the text parts will overlap each other, until the text area is all black.
Do you have an explanation of how this can be and maybe a solution to the problem?
My code looks like this:
public void showInfoNoTranslation(String info) {
frame.requestFocusInWindow();
// create a JTextArea
JTextArea textArea = new JTextArea(info, 6, 25);
textArea.setEditable(false);
textArea.setBackground(new Color(255, 255, 255, 0));
textArea.setBorder(BorderFactory.createEmptyBorder());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
// if (textArea.getLineCount() > 5) {
JScrollPane scrollPane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(_frame, scrollPane, "title", JOptionPane.INFORMATION_MESSAGE);
}
Call textArea.setOpaque(false); instead of setting it's background-color to fully transparent and it will work.
From the docs:
public void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
The default value of this property is false for JComponent. However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree) is look-and-feel dependent.

When typing into JTextArea, text in JLabel moves

I'm making a simple GUI and have a problem.
This is my code :
JFrame jFrame = new JFrame();
jFrame.setTitle("Simple Editor");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocation(50,50);
jFrame.setResizable(true);
Box box = new Box(BoxLayout.Y_AXIS);
JTextArea jTextArea = new JTextArea();
jTextArea.setPreferredSize(new Dimension(470,500));
JLabel jLabel = new JLabel();
box.add(jTextArea);
box.add(jLabel);
jLabel.setText("Font type : " + Main.fontType + " font size : " + Main.size
+ " background color : " + Main.backgroundColor
+ " font color : " + Main.fontColor);
jFrame.setContentPane(box);
jFrame.pack();
jFrame.setVisible(true);
When I typing something in JTextArea, text in JLabel is moving. I can't figure out how to solve this. Maybe some component between them? Any advice and help is welcome.
This looks like an artifact of how the Box is calculating sizes and locations. Note that some components and layout managers do not use setPreferredSize, or only take it as a hint, or use it as only one part of a computation, or etc. so it cannot be depended upon as a reliable method to set the size of a component.
In this case, I would hypothesize what is going on is something like: BoxLayout generally uses minimum/maximum sizes, not preferred sizes, and the min/max of a JTextArea is computed based on its text content. As the text changes, the size is recalculated so the layout changes too.
In general if you have a text area, you should put it in a JScrollPane instead:
Box box = new Box(BoxLayout.Y_AXIS);
JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jScrollPane.getViewport().setPreferredSize(new Dimension(470,500));
JLabel jLabel = new JLabel();
box.add(jScrollPane);
box.add(jLabel);
This way when the text content changes in the JTextArea it can simply do its thing, recalculating its size, and flow out the side of the scroll pane.
Also see How to Use Scroll Panes, How to Use Text Areas.
Per Andrew's comment, here are a couple ways to set the initial size of the scroll pane which are perhaps more reliable than setting the viewport's preferred size explicitly:
// specify rows & columns
JTextArea jTextArea = new JTextArea(20, 20);
// specify preferred scrollable viewport size
JTextArea jTextArea = new JTextArea() {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(470,500);
}
};
jTextArea.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent e) {
jLabel.setText(jTextArea.getText());
}
});
where,
jTextArea - your name object of JTextArea class
jLabel - your name object of JLabel class
You add text in the textarea and text in the label is changing. I think, this code help you to decide your problem.

Categories