JButtons with multiple lines

I am having trouble using the JButtons. I'm trying to create buttons that have two lines of text in them (a string with a newline character in it), but for some reason the text on the button always reads as one line, even though the string itself prints out as two.

Here is a sample of the code:

publicclass Windowextends JFrame{

private GridBagLayout layout;

private GridBagConstraints constraints;

public Window()

{

super("Sonic Point of Sale");

layout =new GridBagLayout();

setLayout(layout);

constraints =new GridBagConstraints();

constraints.fill = GridBagConstraints.BOTH;

constraints.weightx = 0;

constraints.weighty = 0;

JButton[] buttons =new JButton[320];

int count = 0;

String aString =new String("h \ni");

for(int i = 0; i < 16; i++)

{

for(int j = 0; j < 20; j++)

{

buttons[count] =new JButton(aString);

buttons[count].setBackground(Color.BLUE);

addComponent(buttons[count], i, j, 1, 1);

count++;

}

}

System.out.println(aString);

}

privatevoid addComponent( Component component,int row,

int column,int width,int height)

{

constraints.gridx = column;

constraints.gridy = row;

constraints.gridwidth = width;

constraints.gridheight = height;

layout.setConstraints(component, constraints);

add(component);

}

}

[2545 byte] By [Shmigglea] at [2007-11-26 12:52:27]
# 1
[url http://java.sun.com/docs/books/tutorial/uiswing/components/html.html]Using HTML in Swing Components[/url]
camickra at 2007-7-7 16:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 2
[nobr]String aString = new String("h <br>i");[/nobr]
watfora at 2007-7-7 16:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thank you.
Shmigglea at 2007-7-7 16:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 4

What about this one?

import javax.swing.*;

import java.awt.*;

public class Window extends JFrame{

public Window()

{

super("Sonic Point of Sale");

setDefaultCloseOperation(EXIT_ON_CLOSE);

String aString="line1 \nline2";

System.out.println(aString);

customButton cb=new customButton(aString);

cb.setPreferredSize(new Dimension(100,100));

getContentPane().add(cb,BorderLayout.NORTH);

setSize(500,500);

}

public static void main(String args[]){

new Window().setVisible(true);

}

class customButton extends JButton{

String str="";

customButton(){

super();

}

customButton(String s){

super();

str=s;

}

public void paintComponent(Graphics g){

if(!str.equals("")){int k=str.indexOf('\n');

String s=str.substring(0,k-1);

g.drawString(s,5,15);

s=str.substring(k,str.length());

g.drawString(s,5,35);

}

}

}

}

No Dukes.....!!!

pravintha at 2007-7-7 16:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 5
> What about this one?1) Why reinvent the wheel?2) What about the button changing when you click on it3) What about the focus indicator being painted when the button gains focus4) What about adding an icon to the button....
camickra at 2007-7-7 16:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

>>

1) Why reinvent the wheel?

2) What about the button changing when you click on it

3) What about the focus indicator being painted when the button gains focus

4) What about adding an icon to the button

....

I don't think his requirements are upto that level....

If he wants then he has to handle all other UIs also......

pravintha at 2007-7-7 16:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I don't think his requirements are upto that level....

> If he wants then he has to handle all other UIs also......

No idea what you mean. His requirement was to have a button with two lines of text. Plain and simple.

Your solution changes the button so it no longer looks or behaves like a button. I gave you some of the reasons why...

camickra at 2007-7-7 16:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 8

>>I gave you some of the reasons why...

Please.....

Do you mean the change in look & Feel...?

Check now only one modification.....

import javax.swing.*;

import java.awt.*;

import javax.swing.plaf.*;

public class Window extends JFrame{

public Window()

{

super("Sonic Point of Sale");

setDefaultCloseOperation(EXIT_ON_CLOSE);

String aString=new String("line1 \n line2");

System.out.println(aString);

customButton cb=new customButton(aString);

JButton jb=new JButton("Another normal");

cb.setPreferredSize(new Dimension(100,100));

getContentPane().add(cb,BorderLayout.NORTH);

getContentPane().add(jb,BorderLayout.SOUTH);

setSize(500,500);

}

public static void main(String args[]){

new Window().setVisible(true);

}

class customButton extends JButton{

String str="";

customButton(){

super();

}

customButton(String s){

super();

str=s;

}

public void paintComponent(Graphics g){

if(!str.equals("")){

super.paintComponent(g);

int k=str.indexOf('\n');

String s=str.substring(0,k-1);

g.drawString(s,5,15);

s=str.substring(k+1,str.length());

g.drawString(s,2,35);

}

}

}

}

pravintha at 2007-7-7 16:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 9
You also have to handle the text orientation. Using HTML tags seems simple enough for this requirement.
watfora at 2007-7-7 16:41:26 > top of Java-index,Desktop,Core GUI APIs...