Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Aligning text vertically within a JButton

 
Deepak Raj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for reposting again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic