private static void changeDefaultCharsetSet( String encoding ) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException { Classc = Charset.class; Field defaultCharsetField = ch.getDeclaredField("defaultCharset"); defaultCharsetField.setAccessible(true); defaultCharsetField.set(null, Charset.forName(encoding)); } changeDefaultCharset("UTF-8");
JButton applyButton = new JButton("OK"); applyButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { applyAction(); } } ); getRootPane().setDefaultButton(applyButton);
Old version
JButton cancelButton = new JButton(); Action cancelKeyAction = new AbstractAction("Cancel")) { public void actionPerformed(ActionEvent e) { cancelAction(); } }; cancelButton.setAction(cancelKeyAction); KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE); getRootPane().registerKeyboardAction(cancelKeyAction, cancelKeyStroke, JComponent.
New version
JButton cancelButton = new JButton(); Action cancelKeyAction = new AbstractAction("Cancel")) { public void actionPerformed(ActionEvent e) { cancelAction(); } }; cancelButton.setAction(cancelKeyAction); InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); ActionMap actionMap = getRootPane().getActionMap(); KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE,0); inputMap.put(cancelKeyStroke, "cancel"); actionMap.put("cancel", cancelKeyAction);
JTree tree = new JTree(); BasicTreUI ui= (BasicTreeUI)tree.getUI(); ui.setExpandedIcon(expandIcon); ui.setCollapsed(collapseddIcon);
JPanel jp = new JPanel(); ... BufferedImage area = (BufferedImage)this.createImage(jp.getWidth(),jp.getHeight()); if (area != null) { Graphics2D gc = area.createGraphics(); jp.paint(gc); try { File outputfile = new File("toto.png"); ImageIO.write(area, "png", outputfile); } catch (IOException e) { } }
Use the 'Class.this' variable:
public class X{ private class Y { public void f() { ... X.this ... } } }
Override the method:
public boolean getScrollableTracksViewportWidth ( ) { return false ; }
Because, the warning types of javac are non-standard, the only way to get the list is to use:
javac -X (see lint options)
For example, javac 1.6.0_11 returns:
The list of warning type with Eclipse can be get from the Eclipse server: http://help.eclipse.org
For example: The list of tokens of Eclipse version 3.3 is:
Use the annotation @SuppressWarnings{type} where type is a valid warning type (see the list above).
For example:
@SuppressWarnings("unchecked") LinkedList[] graphSimple = new LinkedList[n]; for(int i = 0; i < n; i++) graphSimple[i] = new LinkedList ();
The label string must begin with "<html>" (not "<HTML>"). Any HTML content is available, for example:
String labelText ="<html>This is an example of <FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> text.</html>";
public class Toto { private String _jarPath; Class> c = Toto.class; URL tmp= c.getResource(c.getSimpleName() + ".class"); String s = tmp.getFile(); int xi = s.indexOf('!'); // Jar file uses the ! meta character if (xi>0) s = s.substring(0,xi); try { URL u = new URL(s); _jarPath = new File( u.toURI() ).getParent(); } catch ( URISyntaxException e1 ) { _jarPath = new File( s).getParent(); } catch ( MalformedURLException e2 ) { _jarPath = new File( s).getParent(); } System.out.prinln("Jar directory: "+_jarPath); }
String homePath=System.getProperty("user.dir");
String homePath=System.getProperty("user.home");
This adjust the extension of the current edited file name in a JFileChhooser with respect to the current selected file filter. For example, if the user open the JFileChooser, the default is NoName.jpg becuase the file filter is JPEG, but if the user change the current file filter to png, the file name is changed to NoName.png.
fileChooser.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.FILE_FILTER_CHANGED_PROPERTY.equals(evt.getPropertyName())) { JFileChooser chooser = (JFileChooser)evt.getSource(); FileNameExtensionFilter f = ((FileNameExtensionFilter)chooser.getFileFilter()); if (f != null) { String currentName = ((BasicFileChooserUI)fileChooser.getUI()).getFileName(); if (currentName.lastIndexOf('.')>0) { currentName = currentName.substring(0, currentName .lastIndexOf(".")); } String extension = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0]; String fullFileName = currentName+"."+extension; fileChooser.setSelectedFile(new File(fullFileName)); } } } });