JTextPane resizing on wordwrap - java

I'm creating a chat program, similar to IRC. With my client though, I have the problem that when text is added to a JTextPane (using a GridBagLayout), it resizes instead of wordwraps. Well, it actually will wordwrap eventually, but it shouldn't be resizing. Here is what I mean:
I could set the JTextPane dimension to an exact number, but I want the user to be able to resize the window, and the parts inside as needed. How can I get to put as much text in without it resizing?

I ended up fixing it by adding setting it into a JScrollPand and using .setPreferredSize().

Related

Trying to recreate Skype conversation panel with Swing

I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue

JPanel issue when font is increased

My friend is facing an issue where in he has a Swing Dialog and it has several text fields, combo boxes and radio buttons. Also it has a JPanel which holds the search results if at all the user wants to perform any search.
It looks fine with normal font size. But once the font size is increased to say 150% or even 200%, then the text boxes are not growing and the text in them is growing. So, they are not fitting in and are getting clip-ed.
He managed to overcome this limitation by using the setPreferredSize method on the UI components. Now it seems that he is able to control the behaviour in case of text boxes, combo boxes etc. But the search panel is still an issue.
Could some one please point out what the issue could be?
UPDATE:
They also have a JTable where the search results are displayed. Now, the thing is, they are hardcoding the height of each row in that JTable using the call setRowHeight. And due to this, if the font size is increased, the row height still remains the same. Is there any method call that resolves this.
We honestly think that they should not have done that hardcoding. Is there any solution for this? Please share.
Thanks,
Pavan.
Which layout is your friend using? Choosing a suitable layout may help.
Try pack() it will automatically adjust the Window to fit the preferred size of the components.

How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

How exactly is word-wrapping implemented in JTextPane?
I'm trying to understand exactly how it works so that I can modify the behavior. Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.
This article by Lapitsky says that LabelView.getBreakWeight() returns View.ExcellentBreakWeight for labels with spaces and View.GoodBreakWeight for labels without spaces (and the code in GlyphView.java seems to confirm this), so why doesn't it break? Is it somehow returning BadBreakWeight instead of GoodBreakWeight? Or is there some layout problem? Or is there a bug?
Here's some code (for your viewing pleasure):
//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);
Note that it still doesn't wrap if I take out the scroll pane and just use the text pane (it just gets clipped as it goes outside the window).
The javadocs for Swing don't seem to go into enough detail on how some objects (like JTextPane, View's, and related objects) work together. Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together? Is it just not publicly available? (Or am I the only one having trouble with things like this? Or is the insufficient documentation limited to things which aren't expected to be dealt with by a typical developer?)
The link about custom wrap (forced wrap and no wrap).
http://java-sl.com/wrap.html
The link about letter wrap
http://java-sl.com/tip_html_letter_wrap.html

Lightweight Component over Heavyweight Component problem

The code pretty huge and involves a lot of different class/methods:
But here is the gist:
There is a main frame : A_Main
Selecting something in the main frame A: opens a JDialog B_Dialog
This B_Dialog has a JPanel on it: C_Panel
This C_Panel comprises of a textfield and a button
On clicking on the textfield/button: opens a tooltip and another JPanel: D_Panel
Now, the problem is:
1) The tooltip overflows the size of B_Dialog and therefore gets truncated
2) D_Panel however; even if its outside the boundary of B_Dialog gets displayed fully
2.1) There are some texfields and drop down menus in this D_Panel
2.2) The mouse events function correctly in this D_Panel items (drop down menus)
2.3) But Keyboard events do not function correct (Textfield)
I would be glad if you could help!
Thanks!
This can only be done in newer versions of the JDK.
See, Mixing Heavyweight and Lightweight Components.
Now, when I try to enter something in the JAR JPanel's text field, I am not able to do so as this pops out of the border of the main JDialog that contains it.
Add a JScrollPane around the JPanel, and allow it to expand both horizontally and vertically. If this doesn't work, you may need a customized Layout Manager, or use one of the default ones like GridBagLayout.
Also, you'll need to gain focus before you can enter text, but that doesn't seem to be the problem here.

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories