Autoresizing JtextPane after Font Size Change - java

I load a text in a JTextPane and automatically every line takes the max number of words it can. When I decrease the Font size text just zoom out and all lines are half empty. How can I auto resize the text so every line is filled no matter what size of Font I use?
The code for changing font is
JComboBox cb =(JComboBox)evt.getSource();
String Selection =(String)cb.getSelectedItem();
int n = Integer.parseInt(Selection);
Font font = new Font("Monospaced", Font.PLAIN, n);
editPane.setFont(font);

Related

Resize JLabel text size with collections in Java Swing

I'm trying to change my JLabel text size using this code:
my_font = my_label.getFont();
my_font = my_font.deriveFont(
Collections.singletonMap(
TextAttribute.SIZE, TextAttribute.20));
There is a compiler warning on this line:
TextAttribute.20;
This code works perfectly and changes the text weight:
font_bold = numero_fattura.getFont();
font_bold = font_bold.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD));
Question: How do I use the same code to change the text size?
As I understand your question, your answer is similar to this below said thread first answer, just follow this link -
How to change the size of the font of a JLabel to take the maximum size
Font labelFont = label.getFont();
String labelText = label.getText();
int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
int componentWidth = label.getWidth();
// Find out how much the font can grow in width.
double widthRatio = (double)componentWidth / (double)stringWidth;
int newFontSize = (int)(labelFont.getSize() * widthRatio);
int componentHeight = label.getHeight();
// Pick a new font size so it will not be larger than the height of label.
int fontSizeToUse = Math.min(newFontSize, componentHeight);
// Set the label's font size to the newly determined size.
label.setFont(new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse));
hope this will help you, thanks.

How to define the length of a Text in SWT depending on the text to display?

I have a Text which will display different data of different length. Initially I was using a Textbox of fixed length. But, obviously It was not working.
Then, I tried to get the length of the text, using that length as a width of the Text , like this -
textName = new Text(composite, SWT.BORDER);
String myText = tree.getSelection()[0].getText();
int length = myText.length();
textName.setBounds(76, 28, length, 21);
textName.setText(myText);
But, I found that the length of a string and the width of the Text field in SWT, are quite different.
So, is there any solution for this purpose? Obviously, I can set a higher value to the width.. But, I think, that will not be a good solution.
You can compute string size with current font size and type by:
GC gc = new GC(textName);
Point textSize = gc.textExtent(myText);
gc.dispose();
textSize gives you size of Your text in pixels. Then you could determine size of Text control to set.
You can let the Text object calculate its default size.
First set the text, then call computeSize which computes the screen pixel size of the text string:
Text textName = new Text(composite, SWT.BORDER);
String myText = tree.getSelection()[0].getText();
textName.setText(myText);
Point size = textName.computeSize(SWT.DEFAULT, SWT.DEFAULT);
textName.setBounds(76, 28, size.x, 21);

How to identify if the selected text in JTextPane are of different sizes?

I am developing a editor where I have a combobox for font size. User can use it to alter the text size.
I have also implemented the caret listener which tells me the size of selected text and updates the combobox accordingly. Now if the user selects the text of two different sizes I want to populate blank value in Combobox. But I am unable to do that with Caret listener as It gives me size of the first text.
Eg: If my text is "HI". Here H is of size 12 and I is of size 22. Now when I select "HI" caret listener gives me the value of either 12 or 22.
Here is the sample code:
StyledDocument doc = pane.getStyledDocument();
MutableAttributeSet fontSizeStyle = ((StyledEditorKit)pane.getEditorKit()).getInputAttributes();
int fontSize = StyleConstants.getFontSize(fontSizeStyle);
Below code would loop through the selected text in Jtextpane and get each character's size.
String selectedText = pane.getSelectedText();
int k = pane.getSelectionStart();
for(int i=0; i< selectedText.length(); i++) {
AttributeSet fontSize = doc.getCharacterElement(k).getAttributes();
System.out.println("fontSize:"+StyleConstants.getFontSize(fontSize));
k++;
}

Scale string to fit into label (Java, AWT)

I have to write app, which can scale font's size of label and buttons depending on size of entire frame. My idea is to increase size of font until whole text fits into label or button.
How can I check is all string fit to button/label?
Something like the following
JComponent c = ... // it can be label, button or any other component
FontMetrics fm = c.getFontMetrics(c.getFont()); // or another font
int strw = fm.stringWidth("My text");
I took this example from Getting string size in java (without having a Graphics object available)

Java: Linebreaks in JLabels?

I'm trying to make a Swing JLabel with multiple lines of text. It's added just fine, but the line breaks don't come through. How do I do this? Alternatively, can I just specify a maximum width for a JLabel and know that the text would wrap, like in a div?
private void addLegend() {
JPanel comparisonPanel = getComparisonPanel();
//this all displays on one line
JLabel legend = new JLabel("MMM FFF MMM FFFO O OOM M MMMM.\nMMM FFF MMM FFFO O OOM M MMMM.\nMMM FFF MMM FFFO O OOM M MMMM.\n");
comparisonPanel.add(legend);
}
Use HTML in setText, e.g.
myLabel.setText("<html><body>with<br>linebreak</body></html>");
You can get automatic line break if you set the paragraph width in html.
label.setText("<html><p style=\"width:100px\">"+paragraph+"</p></html>");
By default, Swing does not wrap text. If you specify a size on the JLabel it will only paint the part of the text that fits and then add "..." to the end.
As suggested you can use HTML to enable line wrapping. However, I've actually created a custom Swing UI delegate not long ago to achieve this and even more: MultiLineLabelUI.
It will wrap your text to fit the available space and also respect hard line breaks. If you choose to try it out, it is as simple as:
JLabel label = new JLabel("Text that'll wrap if necessary");
label.setUI(MultiLineLabelUI.labelUI);
Or alternatively use the custom MultiLineLabel class that in addition to wrapping text supports vertical and horizontal text alignment.
UPDATE
I lost the domain with the original code samples. It can now be viewed on github instead: https://github.com/sasjo/multiline
You can put HTML inside of a JLabel and use the linebreak tag to achieve this.
What about using the wrapping feature in a JTextArea?
String text = "some really long string that might need to"+
"be wrapped if the window is not wide enough";
JTextArea multi = new JTextArea(text);
multi.setWrapStyleWord(true);
multi.setLineWrap(true);
multi.setEditable(false);
JLabel single = new JLabel(text);
JPanel textpanel = new JPanel(new GridLayout(2,1));
textpanel.add(multi);
textpanel.add(single);
JFrame frame = new JFrame();
frame.add(textpanel);
frame.pack();
frame.setVisible(true);
Simple,use HTML. Java Swing components though does not provide a 'fantastic' support for the HTML, you can use it for such simple purposes.
label.setText("<html>This is first line.<br/>This is second line.</html>");
I did not manage to specify a maximum width for a label but you can specify a concrete width.
By measuring the current width of a JLabel we can only apply the new fixed width if the JLabels's width is higher that our maxWidth:
JLabel label = new JLabel("<html>" + myVeryLongMessage + "<html>");
int maxWidth = 400;
Dimension size = label.getPreferredSize();
if (size.width > maxWidth) {
// Estimate the number of lines
int lineCount = (int) Math.ceil(((double) size.width) / maxWidth);
lineCount += 1; // Add one extra line as reserve
size.width = maxWidth; // Apply the maximum width
// Increase the height so that all lines will be visible
size.height *= lineCount;
label.setPreferredSize(size);
}
You can use a JTextArea and disable the TextArea, this way, you will only display what you want, and the user won't be able to type in
JTextArea area = new JTextArea("Here \n\n you \n\n put \n\n your \n\n text");
area.setBounds(10, 11, 500, 143);
area.setEditable(false);
yourPannel.add(area);

Categories

Resources