JTextPane insert component, faulty vertical alignment - java

I have a JTextPane, into which I need to insert a JComponent. I'm using
JTextPane.insertComponent(Component)
The item is indeed inserted, but the vertical positioning is too high. Instead of having the bottom of the component aligned with the baseline of the current line of text, the component is way above that position, blocking out/over-painting lines of text appearing above.
I have tried calling setAlignmentY(float) with various values, on both the inserted component and the JTextPane, but it doesn't affect the behavior at all.
My guess: there seems to be some state inside my JTextPane or its Document that I need to be changing. But I don't know what it is.

Have you tried calling setSize(width, height) on the JComponent before you insert it into the JTextPane? It should work for most components.

I know, this is a pretty old question, but your approach of using setAlignmentY is totally correct. Don't know what code caused it to not work, but the javadoc of JTextPane.insertComponent(Component) says the following about the alignment:
The component is placed relative to the text baseline according to the
value returned by Component.getAlignmentY. For Swing components this
value can be conveniently set using the method
JComponent.setAlignmentY. For example, setting a value of 0.75 will
cause 75 percent of the component to be above the baseline, and 25
percent of the component to be below the baseline
So using textPane.setAlignmentY(1.0f) has the desired effect.

I ran into the same problem and could not find a solution using JTextPane or JEditorPane. But I was able to use JavaFX/WebView/WebEngine/JFXPanel. You will need to update to Java 8 (JDK 1.8). I made my own class HTMLPaneType, an extension of JFXPanel, and use HTMLPaneType in place of a JTextPane.
A JTextPane requires adding a HyperlinkListener if you want to respond to href clicks. HTMLPaneType requires adding a listener if you want to NOT respond to href clicks or to respond differently. In my case, I wanted to launch an external browser on an href click. I was able to do that with both the JTextPane and the extended JFXPanel. See also
http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener/#.VK-JIHsueWN

Related

Size of cell-component is zero in Swing JTable

I’m currently writing a custom TableCellRenderer using a JEditorPane as cell-component for a JTable. I want to make hyperlinks clickable in that component.
Therefore, I wrote a tiny hack that forwards mouse events that are fired on the Table to the specific component. The problem is, that I cannot use the viewToModel function of the JEditorPane to determine the text under the mouse cursor, because the size of my editor pane is always zero and therefore viewToModel always returns -1.
Is there any way to get the components rendered in the table’s cells to have their correct sizes set? Do you have any suggestions on doing it in another way?
Some side information: I cannot use JTextPane since I'm using the scala programming language and there is currently no implementation for it. Furthermore, I have read that it may be possible to use the native event implementation by using custom cell editors. But I could not get this working in the first place. I want that, for instance, links are highlighted by hovering over. But, as far as I can see, that is not possible with the cell editor approach, since you have to click at the cell to start editing first.

Synthetica JButton Margins

So, I have a Calendar solution for my employers where I am using a custom look and feel (Synthetica) and each cell in the table holds a JPanel with a list of buttons and a PAGE_AXIS BoxLayout. I am trying to reduce the gaps between each button so that they abut each other, and I've tried setting the borders to null except that destroys the button appearance. I have used the following (as recommended by Oracle to view the actual size of the components) code;
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.red),
getBorder()));
And this is what I get;
Setting the margin does not work with whatever border version the buttons are using. So, is there any way to find the current border that it's using? So that I can set that border's insets manually, or something like that. Basically, I need the buttons to abut each other. Any ideas?
Clarification: I want the button's themselves to stay the same size, but I want the white space around them (highlighted inside of the red borders) to be gone,
Several possibilities should be considered:
Manipulate the button's bound properties: setBorderPainted(false), et al., as suggested here.
See if a suitable sizeVariant is available, as shown here.
Use a custom UI delegate based on BasicButtonUI, as shown here and here.
I tried to get the Synthetica L&F to have a look but seems as though you have to create an account which I did not want to do. Anyways, a quick look around the website and I found a page which indicates how you can see and configure some of the values set in the L&F. Might be worth having a look there.
http://www.jyloo.com/synthetica/customize/

Component for glass pane console-style text display

I'm trying to provide a progress report for a slow operation, in the form of text scrolling up from the bottom of the screen with details on what's going on - it's an effect you may have seen a few times in video games when they're loading maps, making network connections and suchlike.
Glass pane seems to be the way to get the text overlay, that much I have working. My problem is exactly what component to use for the actual text display.
JTextArea can display text, but as far as I can see, it can only do it from the top of the screen down - is there a way to make it scroll text up from the bottom of the screen?
JLabel by contrast can align the first line of text to the bottom of the screen, and even take appended text on that line, but when I add more lines separated by newline characters, it just seems to swallow them up even after calling repaint and validate. Is there a way to make it scroll up with the new text?
Or is there another component I should be using instead?
I really like JXLayer for effects layered over Swing components. JXLayer was at one point scheduled to be included in Java 7. Unfortunately the moving around that has been going on Java.net lost all the good content that the author had. There are still some other great resources around (Java 7 required for this one) on the web. I use JXLayer to provide panels with a busy state having a web-like spinner and greyed out appearance.
Another alternative (not as capable as JXLayer IMHO) is MigLayout has absolute positioning, which is maybe easier than the GlassPane.
JLabel would be the easiest. Otherwise you will have to override paintComponent to do anything fancy like animating the text movement.

Multiple viewports onto the same JTextArea?

There is a component I would like to make which shows all matches of a set of words in a JTextArea, along with some context (N lines, probably user-configurable.)
I already have the code for highlighting the matches so I know their offsets and can therefore determine their bounds. I know the line height of the text so I can determine the bounds of the context areas I want to paint.
But is there some class like JViewport only able to paint multiple views of the underlying component? Since JViewport is a normal Swing component, as soon as you put the same text area under another viewport, it gets detached from the first one.
Edit: Actually as it turns out, I can't seem to determine the bounds of the context areas as the lines-to-offset mapping JTextArea gives me doesn't count wrapped lines.
The problem here is that JViewport is a normal container, and treats its view component as a child component (and each component can only be child in one container). This is quite reasonable, given that always only one component can have the focus, and similar things.
I don't know of any JViewport-like class that would do what you want (painting a view of a component without being parent of it), but in your case, you can achieve most of the same by using two JTextAreas sharing the same Document. You then only would have to synchronize the caret movements (if so wished), I think.
There is nothing that exists as you describe. In fact it sounds like you want to customize the display of a JTextArea. In which case you can override and change the way JTextArea paints itself. Either painting more on top, or changing it completely.
Paint your JTextArea in an Image and use necessary fragments of the image. Keep the image's bounds in original JTextArea so clicking bu the image you can calculate click on the JTextArea to get proper position.

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.

Categories