Archive

Posts Tagged ‘swing’

Swing tip: Move by dragging

July 6, 2009 2 comments

A frame can be moved by dragging it from the title bar. But if you have a window without the title bar or you want to move it by dragging from wherever you want, this is the tip for you.
The MouseListener and MouseMotionListener will do the trick. They will be added to the JWindow that we want to move by dragging.

import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.*;

public class DragMove implements MouseListener, MouseMotionListener {

public static DragMove install(Component c) {
DragMove x = new DragMove();
c.addMouseListener(x);
c.addMouseMotionListener(x);
return x;
}

private Point start;

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
start = e.getPoint();
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
Point p = e.getLocationOnScreen();
Component c = e.getComponent();
c.setLocation((int)(p.getX() - start.getX()), (int)(p.getY() - start.getY()));
c.repaint();
}

public void mouseMoved(MouseEvent e) {
}
}


Just a few explanations. When the mouse button is pressed, then the starting location relative to the window is stored. Then as the window is dragged, its location is moved on the screen relative to this starting point. And the window is repainted.
Pretty fast and simple enough.

Categories: Software Tags: ,

Internationalization for Swing standard components

July 30, 2007 5 comments

Internationalization is a very hot topic, especially on desktop applications. Java offers very nice, easy-to-use and comprehensive tools for this. And, unlike Microsoft, there are not undocumented features. On what I’ll go a little bit deeper in here is not an “undocumented” feature, but a little bit harder to find documentation for. As said, localizing a Java desktop application (developed on Swing) is pretty easy and straightforward. But what is a little bit harder is to localize the Swing standard components, like a file chooser dialog or a confirmation dialog.

For changing the localized texts on this components is fairly easy: simply set a key on the UIManager.

UIManager.put(name, value);

You must do this before creating the component or call SwingUtilities.updateComponentTreeUI(rootComponent) after.

What is more difficult is to know the right names for each component. Below I described some of the most important keys, the associated Swing components and their meaning. I also attached a screenshot with a file chooser dialog to easily identify its components (on the description you will also find a number to refer to the component on the given screenshot). Please keep in mind that these are LnF dependent.

JFileChooser components

Key name Associated component Description Default value
FileChooser.openDialogTitleText JFileChooser The text on the title bar (open dialog) Open
FileChooser.saveDialogTitleText JFileChooser The text on the title bar (save dialog) Save
FileChooser.lookInLabelText JFileChooser The text for the label in front of the folder selection box (open dialog) – 1 Look In:
FileChooser.saveInLabelText JFileChooser The text for the label in front of the folder selection box (save dialog) – 1 Save In:
FileChooser.upFolderToolTipText JFileChooser The tooltip of the button for navigating to the parent folder – 2 Up One Level
FileChooser.homeFolderToolTipText JFileChooser The tooltip of the button for navigating to the home folder – 3 Desktop
FileChooser.newFolderToolTipText JFileChooser The tooltip of the button used to create a new folder – 4 Create New Folder
FileChooser.listViewButtonToolTipText JFileChooser The tooltip of the button for switching to list view – 5 List
FileChooser.detailsViewButtonToolTipText JFileChooser The tooltip of the button for switching to detailed view – 6 Details
FileChooser.fileNameHeaderText JFileChooser The text placed on the header column displaying the file name in the detailed view – 7 Name
FileChooser.fileSizeHeaderText JFileChooser The text placed on the header column displaying the file size in the detailed view – 8 Size
FileChooser.fileTypeHeaderText JFileChooser The text placed on the header column displaying the file type in the detailed view – 9 Type
FileChooser.fileDateHeaderText JFileChooser The text placed on the header column displaying the last modified date of the file in the detailed view – 10 Date Modified
FileChooser.fileAttrHeaderText JFileChooser The text placed on the header column displaying the file attributes in the detailed view Attributes
FileChooser.fileNameLabelText JFileChooser The text of the label in front of the textfield containing the selected file name – 11 File Name:
FileChooser.filesOfTypeLabelText JFileChooser The text for the label in front of the filter selection box – 12 Files of Type:
FileChooser.openButtonText JFileChooser The text on the button used to select the file on an open type dialog – 13 Open
FileChooser.openButtonToolTipText JFileChooser The tooltip text for the button used to select the file on an open type dialog – 13 Open selected file
FileChooser.saveButtonText JFileChooser The text on the button used to select the file on a save type dialog – 13 Save
FileChooser.saveButtonToolTipText JFileChooser The tooltip text for the button used to select the file on a save type dialog – 13 Save selected file
FileChooser.directoryOpenButtonText JFileChooser The text on the button used to open a folder while browsing – 13 Save
FileChooser.directoryOpenButtonToolTipText JFileChooser The tooltip text for the button used to open a folder while browsing – 13 Save selected file
FileChooser.cancelButtonText JFileChooser The text on the button used to cancel the file selection dialog – 14 Cancel
FileChooser.cancelButtonToolTipText JFileChooser The tooltip text for the button used to cancel the file selection dialog – 14 Abort file chooser dialog
FileChooser.updateButtonText JFileChooser
FileChooser.updateButtonToolTipText JFileChooser
FileChooser.helpButtonText JFileChooser
FileChooser.helpButtonToolTipText JFileChooser
FileChooser.newFolderErrorText JFileChooser The error text to appear when an error occurs while creating a new folder Error creating new folder
FileChooser.acceptAllFileFilterText JFileChooser The description, to appear in the filter combobox, for the filter used in the file chooser to accept all the files. All Files
OptionPane.yesButtonText JOptionPane The button caption for confirmation in an option dialog Yes
OptionPane.noButtonText JOptionPane The button caption for denying an option dialog No
OptionPane.cancelButtonText JOptionPane The button caption for aborting an option dialog Cancel
ProgressMonitor.progressText ProgressMonitor The text for the progress monitor Please wait

If you want to see all the keys defined in the UIManager that paste the code below that will output them.

UIDefaults defaults = UIManager.getDefaults(); 
System.out.println(defaults.size()+ " properties"); 
for (Enumeration e = defaults.keys(); 
    e.hasMoreElements();) { 
  Object key = e.nextElement(); 
  System.out.println(key + " = " + defaults.get(key)); 
}

This code doesn’t display the properties in the above table. Those properties are subject to localization and they could be displayed using UIManager.get(propertyName) or UIManager.getString(propertyName, locale).

Categories: Software Tags: , , ,