aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Aligning text vertically within a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Aligning text vertically within a JButton" Watch "Aligning text vertically within a JButton" New topic
Author

Aligning text vertically within a JButton

Deepak Raj
Greenhorn

Joined: Jul 22, 2001
Posts: 4
How do I align text vertically on a JButton? i.e. How do I get the text "Button" display like a top-down instead of left-right? Here's a part of my code...
JButton1.setText("Button");
getContentPane().add(JButton1);
JButton1.setBounds(84,72,38,190);
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
You can create your own custom Button which extends JButton to do your job. The only thing is positioning the button to display text which your setBounds() should do it.
Heres one custom button that could help u..
class VerticalButton extends JButton
{
private int charBufferWidth = 2;
private int charBufferHeight = 2;

public VerticalButton(String text)
{
setText(text);
}

protected void paintComponent(Graphics graphics)
{
String buttonText = getText();
int stringLength = buttonText.length();

//here you tinker with the font
FontMetrics fontMetrics = graphics.getFontMetrics();
int maxAscent = fontMetrics.getMaxAscent();
int maxDescent = fontMetrics.getMaxDescent();
int charHeight = maxAscent + maxDescent;

int xPosition = 1;
int yPosition = 1 + maxAscent;
String tmpString = null;

//this is where you draw vertically & increment/decrement
//position of point
for(int i=0;i<stringLength;i++) >
{
tmpString = buttonText.substring(i,i+1);
graphics.drawString(tmpString,xPosition,yPosition);
yPosition = yPosition + charHeight + charBufferHeight;
}
}

public Dimension getPreferredSize()
{
int height = getVerticalStrLength();
int width = getHorizontalStrLength();
return new Dimension(height, width);
}
private int getVerticalStrLength()
{
FontMetrics fontMetrics = getGraphics().getFontMetrics();
String text = getText();
int stringLength = text.length();
int verticalStrLength = fontMetrics.stringWidth(text) +
(stringLength * charBufferHeight);
return verticalStrLength;
}

private int getHorizontalStrLength()
{
FontMetrics fontMetrics = getGraphics().getFontMetrics();
String text = getText();
int horizontalStrLength = fontMetrics.getMaxAscent() +
charBufferWidth;
return horizontalStrLength;
}

}


- Vinod<br />-------<br />SCJP2
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
You can create your own custom Button which extends JButton to do your job. The only thing is positioning the button to display text which your setBounds() should do it.
Heres one custom button that could help u..
class VerticalStringButton extends JButton
{
private int charBufferWidth = 2;
private int charBufferHeight = 2;

public VerticalStringButton(String text)
{
setText(text);
}

protected void paintComponent(Graphics graphics)
{
String buttonText = getText();
int stringLength = buttonText.length();

//here you tinker with the font
FontMetrics fontMetrics = graphics.getFontMetrics();
int maxAscent = fontMetrics.getMaxAscent();
int maxDescent = fontMetrics.getMaxDescent();
int charHeight = maxAscent + maxDescent;

int xPosition = 1;
int yPosition = 1 + maxAscent;
String tmpString = null;

//this is where you draw vertically & increment/decrement
//position of point
for(int i=0;i<stringLength;i++) >
{
tmpString = buttonText.substring(i,i+1);
graphics.drawString(tmpString,xPosition,yPosition);
yPosition = yPosition + charHeight + charBufferHeight;
}
}

public Dimension getPreferredSize()
{
int height = getVerticalStrLength();
int width = getHorizontalStrLength();
return new Dimension(height, width);
}
private int getVerticalStrLength()
{
FontMetrics fontMetrics = getGraphics().getFontMetrics();
String text = getText();
int stringLength = text.length();
int verticalStrLength = fontMetrics.stringWidth(text) +
(stringLength * charBufferHeight);
return verticalStrLength;
}

private int getHorizontalStrLength()
{
FontMetrics fontMetrics = getGraphics().getFontMetrics();
String text = getText();
int horizontalStrLength = fontMetrics.getMaxAscent() +
charBufferWidth;
return horizontalStrLength;
}

}
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
sorry for reposting again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Aligning text vertically within a JButton