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!

Dienstag, 30. Januar 2007

How to use Java 6 SystemTray?

Problem:
"I heard about some coole new Java 6 Systemtray features. Whats that all about?"


Solution:

Using a TrayIcon you can add your Application to the SystemTray. A TrayIcon always needs an Icon, a Name and a PopupMenu.

Have a look at this Demo-Application:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SystemtrayTest implements Runnable {

public static void main(String[] args) {
EventQueue.invokeLater(new SystemtrayTest());
}

public void run() {
// first check, if the current OS supports SystemTray
if (SystemTray.isSupported()) {
final PopupMenu menu = new PopupMenu();
final MenuItem helloMenu = new MenuItem("Hello World");
final MenuItem exitMenu = new MenuItem("Exit");

helloMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello World !");
}
});

exitMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

menu.add(helloMenu);
menu.addSeparator();
menu.add(exitMenu);

// Attention! Replace this "arrow_down.gif" with your icon or the TrayIcon will be empty
final Image icon = Toolkit.getDefaultToolkit().getImage("arrow_down.gif");

final TrayIcon trayIcon = new TrayIcon(icon, "SystemTrayTest", menu);

try {
// add the trayIcon to the system tray
SystemTray.getSystemTray().add(trayIcon);
// show an information message in a bubble
trayIcon.displayMessage("Hello", "This is just an information", TrayIcon.MessageType.INFO);

} catch (AWTException ex) {
ex.printStackTrace();
}

} else {
System.err.println("Tray not supported");
}
}
}



How to set the ToolTip Color of a single Component?

Problem:
"Using the
UIManager I can only set the ToolTip Color for all Components. I want to set the Color for a single Components like a Button etc. How do I do this?"



Solution:
You need to overwrite theComponents
createToolTip method. For example like this:

public JToolTip createToolTip() {
JToolTip jt = new JToolTip();
jt.setForeground(Color.RED);
jt.setBackground(Color.BLACK);
return jt;
}

How to rotate / shear / scale a JButton, JComponent

Problem:
"I want to rotate a JSlider or another Component. How can I do this?"

Solution:
Using JXtransformer, this is very simple.
Check this Website:
http://weblogs.java.net/blog/alexfromsun/archive/2006/07/jxtransformer_t.html

Montag, 29. Januar 2007

How to set the background olor or foreground color of a ToolTip / JToolTip ?

Problem:
 "How can I change the background or foreground Color of a Tooltip?"



Solution:
If you want to change the global Color for all Components, use this code:

UIManager.put("ToolTip.foreground",
new ColorUIResource(Color.red));
UIManager.put("ToolTip.background",
new ColorUIResource(Color.black));





 

Sonntag, 28. Januar 2007

SelectionListener is called twice on a JList

Problem:
"I am using a Jlist. To control the selected Item I am using a SelectionListener. But the method
valueChanged(ListSelectionEvent) is allways called twice when I select an Item from the List."

Solution:
There are two events fired. One for a select and one for a deselect. Use this code to execute only if the Item is selected:
ListSelectionEvent e;
if(! e.getValueIsAdjusting())







JDialog blocks everything

Problem: 
"When using a JDialog, this Dialog is blocking all other windows. How can I change this behaviour?"

Solution:
Use the JDialog.setModal(boolean) method and set it to false.



Get the password from JPasswordField

Problem:

"I have a problem with the JPasswordField. Every time Iwant to get the entered password using
passwordfield.getPassword() I just get something like [C@a32b.."

Solution:

JPasswordField doesn't return a String or a char. It returns a char array. To get your password as a string use this code:

String password = String.valueOf(passwordfield.getPassword());