I am working on my own little text editor. Files can be edited in a JScrollPane, now if a line of text is longer than the window, you can scroll to the right as it is ment to be. But when the Blinker(or whatever it's called) is at the very end of the line, it's not visible because it seems to be covered by the border.
//the JTextArea is inside the JScrollPane of course
Border scrollPaneBorder = new LineBorder(interfaceColor, 8, true);
Border textAreaBorder = new LineBorder(backgroundColor, 4, true);
Setting both borders to 0 won't change anything. Has anyone got a way to deal with this problem?
The most elegant solution in my opinion is the one in Notepad++. There it somehow puts some space between the text and the border as soon as you get close to the border. But I don't know how/if this is possible in java.
Whatever solution, thanks for the help guys.
From the documentation of setMargin:
Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).
It’s a bit strange that Swing’s text component will move the cursor up to a location that is covered by the painted Border if there is no margin, but it explains why the problem occurs when you set a custom Border but not with the default border as the default value for the margin property is non-zero.
You can create a custom border that reads the margin property and implements an unpainted inner region of that size or, if you don’t need support for different values for the margin property, you can just combine your border with an empty border to get a similar effect (of a hardcoded margin space):
Border textAreaBorder=BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(backgroundColor, 4, true),
BorderFactory.createEmptyBorder(2, 2, 2, 2)
);
Related
I'm trying to set up a few JLabels to use as buttons inside a BoxLayout, stacked on top of each other. The layout is fine, but I'm finding that I can't resize the labels to the dimensions I want. I'm using the following code to size them:
JLabel fileAddBtn = new JLabel("Add File", SwingConstants.CENTER);
fileAddBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileAddBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileAddBtn.getMinimumSize().height));
and
JLabel fileRemBtn = new JLabel("Remove File", SwingConstants.CENTER);
fileRemBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileRemBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileRemBtn.getMinimumSize().height));
As of now I have two labels, with one being longer than the other. They are both taking the width of the longer label, which is good, but the labels are hugging the edges of the text right to the nearest pixel. Is there any way to make the labels a little bigger so that there is a bit of a border around the labels? I've tried using setSize() but it doesn't take. I've also added straight values into the above code, but it doesn't change them either. I tried adding an EmptyBorder() around them, which worked for sizing, but it hid my line border which surrounds them. Any thoughts?
Is there any way to make the labels a little bigger so that there is a bit of a border around the labels?
Sure. Add an EmptyBorder.
But since the code is already adding a border to the labels, to retain that line border, make a CompoundBorder consisting of the empty border and the line border, and set the compound border to the label.
See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
I have a column of JPanel instances that has content in it, that when it is clicked, the selected Panel is set to have a border (in order to distinguish it), and only 1 at a time has the border.
The problem is that when it sets the border, it sets the outer section of the panel to the border, and shrinks the content inside. Although it seems minor it is not very professional, and I would much rather have it add more like an overlay, where the content will not shrink.
I am thinking maybe there is some method of graphics that will let me do this? I haven't been able to find any way of doing this.
Start by setting all the components to have a EmptyBorder set to a single pixel inset.
When you select a panel, simply set the newly selected panel's border as you are (presumably using a LineBorder) and the set the previously selected panel's border to the single pixel EmptyBorder.
If you're clever, you could get away with a single instance of EmptyBorder ;)
I have a TextArea in LWUIT that I am having an issue manipulating. I have the following two issues:
Even though I call setIsScrollVisible(true), I do not seem to have a
scrollbar and cannot scroll when the output goes below the visible
area of the container.
How do I tell the TextArea to automatically scroll to the bottom
programmatically?
My code for initializing the TextArea looks like this:
myTextArea = new TextArea(20, Display.getInstance().getDisplayWidth());
myTextArea.setEditable(false);
myTextArea.setEnabled(true);
myTextArea.setIsScrollVisible(true);
myTextArea.setGrowByContent(false);
System.out.println(myTextArea.isScrollableY());
isScrollableY() returns true. Any ideas? Is there something I am missing? There doesn't seem to be anything visible in the API that lets me explicitly enable or disable scrolling. Thanks in advance for any assistance on this.
The width of the text area is in columns NOT pixels as you have in your code.
Setting the scroll to visible won't cause it to appear since LWUIT scrollbars are always "as needed" which means a scrollbar will only appear when necessary, setting this value to false would just hide the scrollbar regardless of necessity.
To have the text area grab the entire width just place it within a box layout Y container/form and the layout manager will stretch it on the X axis.
You can use scrollRectToVisible() to scroll the text area to the bottom or alternatively you can derive text area and use setScrollY(int) with the appropriate value (see the source code of text area for how this is used to scroll the text area.
Try a simple textArea.setFocusable(false). This worked for me.
The picture shows a thin (1 pixel?) white margin (marked in blue) inside the border of a JTable.
How can the margin be eliminated, or the colour be changed?
I have tried:
Changing the JTable
Changing the ScrollPane
I set both to opaque without success.
Thanks for help.
The sides there are simply the border set by whatever JComponent its contained in. Remove the border (or change it to a line border for instance) and that style will go away. :)
Using TextLayout and LineMeasurer (The only way to draw multiline center-aligned text in java), How does one go about this?
Use a JTextPane. You can set the paragraph attributes to "centered" and every line will be centered within the text pane. After you create the text pane you can do something like:
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
Hey camickr, I don't think this solution properly handles the auto-resizing of fonts to fit within bounds.
I am essentially drawing text to a JScreen (Windowless form of a Jframe) for text projection reasons, and I need whatever text that is in there to conform to the bounds of the screen, no matter what font size was initially set, so it auto-adjusts font size as neccessary.
Center alignment really isn't the problem here. I can do center aligned on my own without the JTextPane. The problem is the font fitting within bounds.
I didn't want to have to loop through and check textlayout bounds each time after a get from linemeasurer and minimize font size till it's just right, but it seems would be the only way to go about it.
So I created a wrapper class around LineBreakMeasurer, which immediately populates an arraylist of TextLayouts upon instantiation of the wrapper class. While generating this array list of layouts, I got the lineheight of each layout and added that incrementally to a total line height. I can then externally query this wrapper for the total lineheight of all it's text and if it is greater than my vertical bounds, I rederive the font with a lesser font size and repopulate the arraylist of layouts....which can then be gotten from the arraylist using some getNextLayout() method.
I also had this wrapper class force '\n' to be respected by lineBreakMeasurer by doing a String [] lines = text.split("\n")
And then running linebreakMeasurer on each of those lines as I populated the arraylist of layouts.