I have a JTextArea in my java code, I want to increase the size of the cursor which points the writing position/spot on the JTextArea, I am not talking about the cursor which points the position of mouse, but the cursor/pointer which indicates the spot of writing on JTextArea. Please tell how I can increase its size?
The text cursor is called Caret. You can change the caret by setting a new caret with the JTextComponent.setCaret() method (JTextArea extends JTextComponent).
Carets are responsible rendering themselves, so you can paint as big caret as you would like to. Basically what you should do is extend DefaultCaret and override its paint() method.
Here's an example of a custom caret:
http://java-sl.com/tip_overwrite_mode.html
one way is to simply increase the font size. Is this what you want? or do you want to keep the font small?
Font font = new Font("Verdana", Font.BOLD, 30);
txt.setFont(font);
Related
I have JFrame with a JTextArea inside of it.
Font font = new Font("monospaced", Font.PLAIN, 14);
textarea.setFont(font);
Since the font is monospaced, all characters are the same width and height.
I'd like to know what this width and height is in pixels.
For this, I could use font.getStringBounds but I have no Graphics context to pass to it. frame.getGraphics() returns null.
How can I find the size of a character? Can it be done without a Graphics instance? I don't want an instance of it anyway. I just want to know how big my characters are.
You can use JFrame#getFontMetrics since one of JFrame's Superclass is Component.
If this does not work, you can also use BufferedImage to get a Graphics object:
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
You can use the image object to get an instance of Graphics.
FYI, I am using a JFrame/JTextarea to render a text based game, so I'll use this info for scaling the text and getting the dimensions of the window in units of characters
It's probably not the best approach, it would be better to simply use JTextArea#setColumns and JTextArea#setRows which will use the font based information to make determinations about it's preferred size automatically
You can then make use of the LayoutManager APIs and simply call pack on the JFrame which will pack the window around the contents, based on it's preferred size
This will also affect the preferred size of JScrollPane
I'm making a Pascal triangle and I want vertical text align.
I output everything in Text Panel, I tried many options here but none seems to work. There are some align options in this editor but there are too many, I don't know which one will do what I want, can't find anything about that anywhere.
When it gets to double/triple digits it's not aligned vertically anymore. I'm making it from left to right, not in triangle shape.
To vertically align text on adjacent lines, a fixed width Font (such as Courier) can be used. If using Swing, you can can set the Font of a JTextComponent using the appropriate method:
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Courier", Font.PLAIN, 12));
If rendering with html, you can use the <pre> tag.
The fixed width Font will let the nth character on one line be aligned with the nth character on all other lines.
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've got the following code:
JXBusyLabel label = new JXBusyLabel();
label.setBusy(true);
label.setSize(100, 100);
frame.add(label);
I need a circle indicator of loading, and this code is good. But I need to increase size of circle, because now it has got a very small size. But setSize method doesn't allow me to do what I want. Please, tell me, how can I do it?
From the swingx javadoc, you can pass a Dimension object to JXBusyLabel like this:
JXBusyLabel label = new JXBusyLabel(new Dimension(100,84));
How can I elongate sliders length and also the width if possible. Most importantly, the length.
Below is just pieces of codework on Jsliders.
slider = new JSlider(0,180);
slider.setMajorTickSpacing(30);
slider.setMinorTickSpacing(15);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setValue(0);
JPanel panel = new JPanel();
panel.add(slider);
I tried doing slider.setSize(100,100); something like that, but that does not seems to be working.
Any suggestions?
p.s (I was also wondering if the color encoded on the slider bar can be filled in with a different color like cyan, and if it exceeds some value, possibly change to red. Right now it is set to default, it gets filled with light blue.)
To change the size of a Swing component you use setPreferredSize(), not setSize(). The layout manager will either use the "suggested" preferred size or ignore it.
Some LAF will use the UIManager to determine the colors used for painting components. If you want to change the color for all JSliders then check out the UIManager Defaults for a list of properties you might be able to change.