And what does a timer class have to do with a jButton???
Main Topics
Browse All TopicsOk, I have a jButton with a ICON and some text, I want the text to be aligned to the LEFT, and the icon to align to the RIGHT.. How would I go about doing this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can
get the Button size with getSize(),
also get the size of an icon,
get the borders, insets or any other gaps within the button (e.g. these depend on Look and Feel),
and in the worst case, count the size of text width using this code:
String change = "Pick a font, size, and style to change me";
FontMetrics metrics = g2.getFontMetrics(); //where g2 is a Graphics2D object.
int width = metrics.stringWidth( change );
Having all the sizes You can decide on the gap between the text and icon within the button itself.
This code works. I really did nothing more than what StillUnAware recommended, but there were a few tricks required.
The primary one being that you have to set the setIconTextGap AFTER the button has been initially painted; thereofre, it is done in an invokeLater. Also, the left/right margins of the buttons had to set or ellipse appeared int he button text.
This sample has five buttons, each with different text and icons. All icons are right aligned, and all text is left aligned.
Keep in mind that if the user resizes something that changes the size of the button(s), then you must have a listener for that event and call the setButtonGap() method for each button again (probably via invokeLater).
Here is the code:
package com.yourcompany.yourprodcu
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants
import javax.swing.SwingUtilities
public class Testola extends JPanel {
private static final long serialVersionUID = 1L;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
ImageIcon myIcon = new ImageIcon("C:/calendar.gif
ImageIcon myIcon1 = new ImageIcon("C:/Iso.gif");
ImageIcon myIcon2 = new ImageIcon("C:/older.gif");
ImageIcon myIcon3 = new ImageIcon("C:/folderopen.g
ImageIcon myIcon4 = new ImageIcon("C:/History.gif"
/**
* This is the default constructor
*/
public Testola() {
super();
initialize();
// InvokeLater or buttonWidth() returns zero
SwingUtilities.invokeLater
public void run() {
setButtonGap(getJButton())
setButtonGap(getJButton1()
setButtonGap(getJButton2()
setButtonGap(getJButton3()
setButtonGap(getJButton4()
}
});
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(5);
this.setLayout(gridLayout)
this.setSize(300, 200);
this.add(getJButton(), null);
this.add(getJButton1(), null);
this.add(getJButton2(), null);
this.add(getJButton3(), null);
this.add(getJButton4(), null);
}
/**
* Given a JButton, setIconTextGap such that icon is right justified.
* @param button
*/
private void setButtonGap(JButton button) {
Insets myInsets = button.getInsets();
Insets myMargins = button.getMargin();
myMargins.right = 0; // zero out of get ellipse (...) in button text
myMargins.left = 0; // zero out of get ellipse (...) in button text
FontMetrics metrics = button.getFontMetrics(butt
int textWidth = metrics.stringWidth(button
int buttonWidth = button.getWidth();
System.out.println("button
int gapSize = buttonWidth - (button.getIcon().getIconW
myInsets.left +
myInsets.right +
myMargins.left +
myMargins.right +
textWidth);
button.setIconTextGap(gapS
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if(jButton == null) {
jButton = new JButton();
jButton.setText("Button Text");
jButton.setHorizontalTextP
jButton.setHorizontalAlign
jButton.setIcon(myIcon);
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if(jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("Differen
jButton1.setHorizontalText
jButton1.setHorizontalAlig
jButton1.setIcon(myIcon1);
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if(jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("Only Text");
jButton2.setHorizontalText
jButton2.setHorizontalAlig
jButton2.setIcon(myIcon2);
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if(jButton3 == null) {
jButton3 = new JButton();
jButton3.setText("Button Text");
jButton3.setHorizontalText
jButton3.setHorizontalAlig
jButton3.setIcon(myIcon3);
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if(jButton4 == null) {
jButton4 = new JButton();
jButton4.setText("Short");
jButton4.setHorizontalText
jButton4.setHorizontalAlig
jButton4.setIcon(myIcon4);
}
return jButton4;
}
}
Business Accounts
Answer for Membership
by: StillUnAwarePosted on 2006-09-06 at 11:38:32ID: 17465268
sorry, and again, wrong wrong wrong, totally missed ;)
.5.0/docs/ api/java/u til/Timer. html
for You, I'd suggest to look at this Timer class:
http://java.sun.com/j2se/1
All You need, is to implement TimerTask and pass it to Timer's shedule method.