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);
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); }
//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
posted
0
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); }
//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; }