How to know html label string rendering length

I use html label to display parameters and their values ​​on buttons . Some buttons contain one parameter, and some have 2 or 3 parameters. "/" is used to separate values. There is an empty string between the name and value. But in the case of long values, I want to use this empty line to display a value similar to the " H / V Table " button.

I'm trying to use the length of a string of values ​​to determine if an empty string is needed or not. This does not work properly, because the character number does not reflect the drawing length of this line. The button size is fixed.

I ask , how can I find out when I will have another "<br>" that represents this empty string? Or , how can I know when a string of values ​​will be wrapped in another string?

This is my code:

private static String getBtnDisplayStr(String name, String value) { StringBuilder sBuilder = new StringBuilder(); sBuilder.append("<html><center><b>"); sBuilder.append(name); if(value.length() <= 12) //add one empty line for short value string { sBuilder.append("</center></b><br><br><font size=\"2\">"); } else { sBuilder.append("</center></b><br><font size=\"2\">"); } sBuilder.append(value); sBuilder.append("</font></html>"); return sBuilder.toString(); } 

here are the buttons: enter image description here

+4
source share
2 answers
  • by default there is no reason to calculate PreferredSize for Html <=3.2 and Swing JComponents

  • leave this to the LayoutManager , for example, GridLayout calculates the screen size from the largest element

enter image description here

 import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class HtmlAndJButton { final String buttonText = " Whatever words, <br> but nothing wise"; final String buttonText1 = " Whatever words, <br> but nothing wise, " + "<br> plus 1st. line, "; final String buttonText2 = " Whatever words, <br> but nothing wise, " + "<br> plus 1st. line, <br> plus 2nd. line,"; private JButton btn1 = new JButton("Toggle"); private JButton button = new JButton(buttonText); private JButton button1 = new JButton("Toggle"); private JButton button2 = new JButton("Toggle"); public HtmlAndJButton() { btn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { button.setText("<html><font color=" + (button.isEnabled() ? "blue" : "red") + ">" + buttonText + "</font></html>"); button.setEnabled(!button.isEnabled()); button1.setText("<html><font color=" + (button1.isEnabled() ? "red" : "green") + ">" + buttonText1 + "</font></html>"); button1.setEnabled(!button1.isEnabled()); button2.setText("<html><font color=" + (button2.isEnabled() ? "green" : "yellow") + ">" + buttonText2 + "</font></html>"); button2.setEnabled(!button2.isEnabled()); } }); button.setText("<html><font color=red>" + buttonText + "</font></html>"); button1.setText("<html><font color=green>" + buttonText1 + "</font></html>"); button2.setText("<html><font color=yellow>" + buttonText2 + "</font></html>"); JFrame f = new JFrame("ButtonTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new GridLayout(2, 2)); f.add(button); f.add(button1); f.add(button2); f.add(btn1); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { HtmlAndJButton t = new HtmlAndJButton(); } }); } } 
+3
source

Adjust the text on the button, then check the "preferred width" of the button. If it is too large, adjust the text accordingly.

Here is an example with two buttons at the top:

 private static int BUTTON_WIDTH = 90; // Adjust this as necessary public static void main(String[] args) { JFrame frame = new JFrame(); JPanel pnl = new JPanel(); JButton btn1 = new JButton(getBtnDisplayStr("Table H/V",null,"909.0 / 500.0")); if (btn1.getPreferredSize().width > BUTTON_WIDTH) { btn1.setText(getBtnDisplayStr("Table H/V", "909.0 /", "500.0")); } JButton btn2 = new JButton(getBtnDisplayStr("Acq/ISD",null,"1 / 1")); if (btn2.getPreferredSize().width > BUTTON_WIDTH) { btn2.setText(getBtnDisplayStr("Acq/ISD","1 /","1")); } pnl.add(btn1,BorderLayout.LINE_START); pnl.add(btn2,BorderLayout.LINE_END); frame.add(pnl); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private static String getBtnDisplayStr(String name, String valueLine1, String valueLine2) { StringBuilder sBuilder = new StringBuilder(); sBuilder.append("<html><center><b>"); sBuilder.append(name); sBuilder.append("</b><br/>"); if (valueLine1 != null) { sBuilder.append("<font size=\"2\">"); sBuilder.append(valueLine1); sBuilder.append("</font>"); } sBuilder.append("<br/>"); if (valueLine2 != null) { sBuilder.append("<font size=\"2\">"); sBuilder.append(valueLine2); sBuilder.append("</font>"); } sBuilder.append("<br/>"); sBuilder.append("</center></html>"); return sBuilder.toString(); } 
0
source

All Articles