You can create your own custom JButton by extending JButton and overriding its paint method.So, to do what you want to do, you have a few choices: We had to dig a bit, but now it's fairly obvious why JButton won't wrap text around a new line. If we dig a little further, you'll see that this method further delegates the actual drawing to the drawString() method in BasicGraphicsUtils: public static void drawString(Graphics g,String text,int underlinedChar,int x,int y)ĭrawString() does a little processing and then simply calls the Graphics-based drawString() method. PaintText() does all the string drawing dirty work. If you're brave enough to look at the BasicButtonUI's code, you'll notice the method: protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) One implementation of ButtonUI that a JButton may use is the BasicButtonUI. The ComponentUI allows us to change the look and feel of a Java Swing GUI on the fly by simply swapping in new ComponentUI renderers into our JComponents. Instead of putting all of the rendering code in the JButton's paint method, the JButton delegates its display to a ButtonUI (an extension of ComponentUI). The JButton does not intrinsically have the ability to wrap long lines or start a new line when the text contains a \n.Īll JFC components are rendered through a corresponding ComponentUI. Do you have any suggestions?Ī: The short answer: you can't do it without a bit of work on your part. I tried to create it with: new JButton("Your Q: Suppose I want to create the button with a label as shown below:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |