Changing the amount you "tab" java? - java

As above. I have a modal JOptionPane that uses a text area to display a slightly larger than normal message. The Pane works fine and is currently roughly 300px sq. The problem is I am trying to output something similar to the following:
Amt (TAB HERE) x (TAB HERE) Type (TAB HERE) Price (TAB HERE)
Again I have no problem with the actual Panel at all it's displaying at the size I want but for some reason it "tabs" really far. Like 1/3 of the JOptionPane such that it cuts off after Type and I lose half my text. Is there any way I can specify the size of the tab I want? I've tried aligning manually using spaces but as you can imagine not all letters are the same width and it's just too damn hard to get it to line up properly so I NEED to tab. I don't want to mess around with new layout managers either. I am simply concatonating a string in a method returning it to a JTextArea and then putting that inside my JOptionPane so I'd like a solution that works doing this. If I output to the command line the tab looks normal, like maybe 5 odd characters but doing that in this manner it's more like 2 words long....
Worse case scenario I make a bigger JOptionPane but I would prefer not to for aesthetics.

You can put an HTML table in your JTextArea like they show here. Either a JTextArea or a JTable can go in a JOptionPane.

Related

How can I prevent JTextPane to grow horizontally...?

I am building an application with several tabs containing JPanels with JTextPanes. While testing, I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that? Also, how can I force that the text, once the line has been completed, jumps to the next line on the JTextPane?
Thanks!
UPDATE: I opted for the first option that Julien Gauthier mentioned, which is using JTextArea and enabling the Wrapping. It worked neatly. Given that the size of my text boxes (or areas) is more than enough for the limit I established as text capture (800 characters), I did not need to add Scrolls to the text areas. And also, I did not had any trouble with my save-text-to-file functions with JTextAreas instead of JTextPanes. Thanks a lot for the help!
I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that?
Add the text pane to a JScrollPane and then add the scroll pane to the frame. Then the text will wrap and scrollbars will appear when required.
Read the section from the Swing tutorial on Text Component Features for a working example of a text pane.
You have several possibilities :
You can first replace the JTextpane by a JTextArea, and use jTextArea.setLineWrap(boolea wrap).
You can use an editorKit with your JTextPane, wich will wrap the text. Please look this example : http://java-sl.com/tip_html_letter_wrap.html
You can use a JScrollPane, but the result will be ugly with long sentences.
Good luck.

Turn fonts to Monospaced

I am trying to make a bizarre text editor for people with reading problems with Netbeans. You load the text you like and the editor starts highlighting it word by word with bold letters. The change from plain to bold constantly change the word dimensions and moves the line. One solution was the Monospaced Font but I would like to add a few more fonts available for the user to choose. Is there any way to do this with Arial for example by giving some orders to the JTextPane?
You can manually split the String with <br/> by counting characters and splitting at the right spot to keep the width under your desired character width. Give some leeway so if you get a big word, it won't still go to the next line.
Alternatively, you could use a JList to display your lines (instead of using <br/>). That way, there's no way the line would split to the next line. However, if you do it that way, the user will click on the list like a list and not be able to select text like in a normal text pane.

Text is not fully shown in JTextField

They say a single image is worth 1000 words:
I'll just note that the size is set to default. (build in NetBeans)
any idea how do I fix this?
Adam.
Without you showing code, I'd say that your JTextField width is not set to be wide enough. You can resize it to be large enough for the number of characters you anticipate.
However, this does not guarantee that the user will not type in more characters, which would show the text cutoff as well.
You can extend the Document that JTextField uses to add the maximum character restriction, as shown at
http://www.rgagnon.com/javadetails/java-0198.html
what are the lengths of your data,it seems you changed the layout and that's causing that problem as the border seems also occupying half of the character.
They say a single image is worth 1000
words:
Actually its not. When posting a question a SSCCE is worth 1000 words.
Stuff like that usually happens when you don't use a layout manager. Assuming (which is all we can do since you didn't post any code) that you are using a proper layout manager then your basic code for creating a text field to display 3 characters is:
JTextField width = new JTextField(3);
The reason is the LNF that was assigned to the frame, once I've changed that, it all works fine.

Restrict number of lines in html JLabel

I have a JLabel that needs to display some html-formatted text. However, I want to restrict this to being 4 lines long (and if so, provide a button to see everything).
So far, I've tried setting the maximum size manually or via a layout manager. However, both of these solutions can cause part of a line to be displayed.
edit: To add a little more details, I need to force 4 lines even when respecting line wrapping correctly, resizing components, and changing font sizes. I've considered handling resize/fontsize changes by replacing the label with a new one that fits correctly.
JLabel seems to handle incomplete tags well, so I could probably do something like a binary search on the input string finding which character would cause it to go over the 4 line limit (using FontMetric to determine how many pixels 4 lines would be), and then replacing an existing label with the new one. The big downside to this approach is that I need to run the computation every time the user resizes the panel or changes fonts (and it feels like a dirty dirty hack).
Add the JLabel to a JScrollPane as set the scrollpane with a reasonable preferred size. Scrollbars will appear a necessary.
I don't know of any absolute solution to the questions since I doubt you can define what a "line" is. One line of text may be font 12 and another 24. I don't know of any way to calculate the height of each given line.
Even if you did use a ComponentListener to handle the componentResized() event I'm not sure you can come up with a reasonable algorithm to to calculate the exact width/height of of a 4 line display.
I would try running through the String of text and removing all text after the third "\n"
String shortenText(String oldtext){
String newText = "";
for(int i=0;i<3;i++){
newText += oldtext.substring(0,oldtext.indexOf("\n"));//adds one line to String
oldtext = oldtext.substring(indexOf("\n")+1);//shorten old string to prepare for next iteration
}
return newText;
}
You may also want to try the same algorithm, except strip of <p> and <br> tags as well...
If you know the values of the possible tags just switch the text from "\n" to "<br>" or any tag you need
Hey, I found a way that works. The framework I'm working with allows me to create a listener for font size changes. In this listener, I determine what the new max size of the label is (getFontMetrics(font).getHeight() * 4) and then re-set the maximum height on the label to this and then relayout everything. This even handles the word wrap case well. I'm guessing that someone could do nasty things with silly HTML input, but this covers the 99% case pretty well.

JRadioButton border/padding/insets/margin... whatever you call it

In my Java app I am trying to create a very simple form with a label and a set of controls on each row of the form. Imagine something like this crude ASCII diagram:
Result 1: (*) pass ( ) fail
Result 2: ( ) pass (*) fail
Error Count: [10______]
Explanation: [Operator overload___]
Annoyingly the JRadioButtons don't line up with the rest of the controls as they have a large amount of padding all around, pushing them to the right a couple of pixels and adding a lot of space between lines. I end up with something like this:
Result 1: (*) pass ( ) fail
Result 2: ( ) pass (*) fail
Error Count: [10______]
Explanation: [Operator overload___]
How can I get the radio buttons to stop having so much empty space so they can line up nicely with everything else? If it matters this is using the GTK L&F; I haven't tried running the program under Windows.
It looks like there are two culprits:
The mini-JPanel containing the two radio buttons has a FlowLayout which defaults to adding 5 pixels of padding around each component.
Doing radioButton.setBorder(null) eliminates another pixel's worth of space around the buttons. It also screws up the dotted line drawn around them when they have focus, though.
Use a GridBagLayout, and make sure to anchor cells (each label and checkbox would have its own cell) towards the left or towards the right as needed. The labels would be right-justified, the checkboxes would be left-justified.
Since customizing GridBagLayouts by hand is a hassle, I recommend using the NetBeans GUI builder and adjusting them using its graphical "customize" tool.
Another solution can be to change margin (radionbuttons's setMargin method). This should do the job. The only downside is that margins/insets will be different for different LAFs.

Categories

Resources