Donnerstag, 15. November 2007

How to change the selectionColor of a JMenuItem

So you just created some colored JMenuItems, but you are unsatisfied, that everytime you move your mouse over the MenuItem, the nice color is changed to an ugly blue.

Use this MenuItemUI:

class myMenuItemUI extends BasicMenuItemUI{
public myMenuItemUI(Color c){
super();
this.selectionBackground = c;
}
}


Usage:
myMenuItem.setUI(new myMenuItemUI(Color.RED))

Montag, 5. November 2007

How to set the column width of a JTable?

If you want your JTable to display a fixed columnwidth which the user cannot change, you have to set the minWidth and the maxWidth of a column.

To set all columns to a fixed width of 10, use this code or modify it to your needs.



for (int i = 0; i < table.getColumnCount(); i++){ table.getColumn(table.getColumnName(i)).setMaxWidth(10); table.getColumn(table.getColumnName(i)).setMinWidth(10);
}

Mittwoch, 24. Oktober 2007

java.net.MalformedURLException: unknown protocol: c

This error message is quite hard to understand.
It often happens, when a File or it's path contains whitespaces. A quick solution is, to add "file:/" to the Filename.

For example:

String f = new String("test.xml");
// this can cause the error:
readFile(f); 
// this will work fine:
readFile("file:///"+f);

Mittwoch, 17. Oktober 2007

Comparing a Float to another Float is not working

Recently I changed my code, so now I am not anymore working with floats, but with Floats.

Somewhere in my code, I used to compare the float values with the "==" Operator. But now this won't work anymore.

Float is not the same as a float. Float is a Wrapper for floats. Because of this, you should compare two Float values with the compareTo method. Using the "==" Operator, you are comparing the Float Object and not the value.

Check out this code:

 

public class CompareFloatsExample {

Float f = new Float(20);

Float f1 = new Float(20);

public CompareFloatsExample(){
System.out.println(new Float(20) == new Float(20));
if (f == f1){
printEqual();
}
else {
printNotEqual();
}
if (f.compareTo(f1) == 0){
printEqual();
}
else {
printNotEqual();
}
}
public void printEqual(){
System.out.println("the value of f ("+f+") is equal to the value of f1("+f1+")");
}
public void printNotEqual(){
System.out.println("the value of f ("+f+") is not equal to the value of f1("+f1+")");
}
/**
* @param args
*/
public static void main(String[] args) {
new CompareFloatsExample();

}

}







Montag, 8. Oktober 2007

How to make a JPanel transparent?

The easy way is to change the alpha of the background Color:


public class TransparentPanel extends JPanel {


public TransparentPanel(){
}
/**
* Set the Transparency of the background color in %
*/
public void setTransparency(int d){
int newValue = (int) (d*2.55);
if (getBackground() != null ){
setBackground(new Color(getBackground().getRed(),getBackground().getGreen(),getBackground().getBlue(),newValue));
}
if (getParent() != null){
getParent().repaint();
}
}
public void setTransParent(){
setTransparency(100);
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
final TransparentPanel p = new TransparentPanel();
p.setBackground(Color.RED);
p.setPreferredSize(new Dimension(200, 100));
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(p,BorderLayout.NORTH);
final JSlider slider = new JSlider(0,100,1);
frame.getContentPane().add(slider,BorderLayout.SOUTH);
slider.addChangeListener(new ChangeListener(){

@Override
public void stateChanged(ChangeEvent e) {
p.setTransparency(slider.getValue());
}
});
slider.setValue(100);
frame.setSize(200,200);
frame.setVisible(true);

}

}



Mittwoch, 31. Januar 2007

How to set the ToolTip initialDelay / Dismiss Delay?

Problem:
"I want to change the  initDelay / dismissDelay of the ToolTips. How do I do this?"

Solution:

There is a very simple way:

// ToolTips without initial delay?

ToolTipManager.sharedInstance().setInitialDelay(0);

// Show Tooltips for 10 seconds

ToolTipManager.sharedInstance().setDismissDelay(10000);





 

How to load an Image from a jar-file?

Problem:
"I've got my images in a jar file. Every time I try to load them, they are not loaded. How can I access the images in the jar the right way?"


Solution:

If you want to load an ImageIcon from your jar file, you can use this code:

ImageIcon image = new ImageIcon();
image = new ImageIcon("yourclass".class.getResource("/imagefolder/" + "image.gif" ));



If you want to load an Image from your jar file you can use this code:

Image image = Toolkit.getDefaultToolkit().getImage("yourclass" .class.getResource("/imagefolder/" + "image.gif"));


Be sure to replace "yourclass" with your own class and imagefolder with the imagefolder inside your jar file. And of course you need to replace image.gif with the image you want to load!