I'm using JTextPane and JButton
If I click the Button, I hope every characters in JTextPane will have a dot under it
Is there any way to do this?
The big problem is how to add dots under every characters :(
You can make use of javax.swing.text.DefaultHighlighter, which handles character spacing of a text component in order to do background painting on that component.
public class DotHighlighter extends DefaultHighlighter {
// implementation
}
Implementing a custom highlighter is a moderate sized amount of work, but here's a nice code example of an underlining highlighter implementation; you should be able to modify it to draw dots.
http://java-sl.com/tip_colored_strikethrough.html you can use the example of colored strikethrough.
All you need is to draw your line under letters. Just set Stroke to your Graphics2D instance. (See BasicStroke and dash pattern)
Are you using a monospaced font? If so, it would be possible to create a new label and display a string that is composed only of periods (of the same length as the string in the original label), and display that label a bit lower than the first.
I.E., if your label says 2446, then you could make a second label that's 5 or 10 pixels lower, which says .....
Again, this will only work with a monospaced font- for any other fonts, it would still be possible, but it would involve a more complicated solution (looping through each character, finding its location, and then managing to display a dot beneath it), I believe.
Related
When I underlined a custom font it used the default single line underline. However, I want the underline to be the same style as the font which cannot be done. Then I noticed that underscore character can be used as an underline by drawing a string of underscores on top of the text I want underlined.
Now when trying to draw string, I'am having hard time to find the coordinates and I don't want to hardcode coordinates, so I need get the coordinates of the text I want to draw onto.
Apparently the text I'am drawing onto is of JButton and JLabel, and there is no way to get the coordinates of its text.
Any help on getting the coordinates or help on using the style of font on underline would be appreciated.
Edit: By style of the font for the underline, I mean this:
button[currRow][currCol].setBackground(Color.RED);
button[currRow][currCol].setContentAreaFilled(true);
button[currRow][currCol].setOpaque(true);
That's what I have right now for my connect four game to denote a red player's move.
At the moment, it only colors the background and if I change my code to button[currRow][currCol].setForeground(Color.RED) then the whole thing just appears to not change. How would I fix this?
This is not easily achievable. The problem is that the pluggable look and feel paints the button content, and it does so in whatever way it sees fit. For instance, some L&F might paint a gradient which does not use the background color.
I suggest for a case such as yours to use a custom image (JButton.setIcon()) and no content area (JButton.setContentAreaFilled(false)).
Alternatively, you could create a custom component which draws the element itself, overriding JComponent.paintComponent().
My users found that if the last character in a JTextPane is a newline, the cursor is smaller. Debug statements showed the same cursor and the same font no matter where I clicked. I downloaded the Oracle demo for JTextPane and can see the same behaviour, so it appears to be out of the box behaviour.
Anyone know a way around this?
My users found that if the last character in a JTextPane is a newline, the cursor is smaller
The caret represents the height of the largest Font used on the line. Since there is no text a smaller caret is used. Is this really an issue to worry about?
I downloaded the Oracle demo for JTextPane and can see the same behaviour
Add the following line of code to the TextComponentDemo:
Rectangle caretCoords = textPane.modelToView(dot);
System.out.println(caretCoords); // added
You will see that the height changes. The height of the Rectangle is used by the DefaultCaret class to paint the caret.
I guess you could override the modelToView(...) method of JTextPane to return a minimum height based on the FontMetrics of the Font of the text pane. Not sure what other functionality of the text pane that might affect.
Or you could override the paint() method of the DefaultCaret to use a minimum height, again based on the FontMetrics.
I am making a customised swing component and I have hit a problem. I want a simple text display (like a jlabel) that changes colour over time. Currently I have extended JPanel and overridden paintComponent(Graphics) to create a component that can draw the text it's given and cycle the colours as required. However, I do not know how to make it always draw the text in the centre of the component, nor how to make it calculate its own default preferred size (as I do not know the exact dimensions of the text) e.g. a JLabel with text set automatically returns the right value for getPreferredSize and the text always fits neatly into the label, this is the functionality I am looking to emulate. Does anyone know how to achieve this?
The FontMetrics class is used to determine the geometry of rendered text with a specific font. This will tell you how much space you need to render your text.
Take a look at the getLineMetrics methods.
I have to create a special TextFieldUI that draws an image as the background. That image contains some alpha components. However, whenever a character is written in that text field, first it redraws the background and then draws the character. This is fine when the background contains no alpha components, but after a few characters have been typed, the alpha areas sum up to become black.
The only way I can see around this is in the paintBackground method of TextfieldUI (which I'm overriding), I have to first sample the color of the background at that location, paint the entire graphics component that color, and then paint my background.
Does anyone know how to sample the color of a pixel when all I have access to is the Graphics object?
Is there a better way to draw a custom image as the textfield background other than overriding paintBackground in TextfieldUI?
Thanks
I haven't tried it before, but Swing is built on top of AWT, and the Robot class had a way of sampling specific pixels in the AWT
Well, I don't know what your custom code looks like in the paintBackground method, but I would make sure you fill in the text field background before you draw the image.
I'll let you decide if its "better" or not, but you can use the Background Panel which allows you to add an image to a panel. Then you add the text field to the panel (the text field is automatically made non-opaque so the image shows through). Then you add the panel to the GUI.
If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.
When you override paintBackground, you're calling the superclass version first, right? It already lays down a background-color rectangle that would give your image a fresh-start.
Rather than 'sampling' the background color, it's probably already correct (the superclass paintBackground code gets it from the parent component if not locally set). If that default is not correct, set it in initial interface construction. (Your field isn't being overlaid on other complicated arbitrary interface of unknown solid colors, is it?)