Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Philipp Meier http://fnogol.de/

« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS 

Convert single object to List

Works with varargs in Java 1.6

   1  
   2  List<String> list = java.util.Arrays.asList("foo");

Enable custom FocusTraversalPolicy in Swing

Every container in Swing/AWT can be given a FocusTraversalPolicy with:
   1  public void setFocusTraversalPolicy(FocusTraversalPolicy policy)

Remember to enable the container as a FocusCycleRoot:
   1  setFocusCycleRoot(true)

Together it looks like:
   1  
   2  container.setFocusTraversalPolicy(myCustomPolicy);
   3  container.setFocusCycleRoot(true);

Array to list an vice veras

Convert an array to a list.

   1  
   2  List<Date> listOfDates = Arrays.asList(arrayOfDates);


On the fly list construction:

   1  
   2  List<Date> listOfDates = Arrays.asList(new Date[]{date1, date2});


Convert a list into an array:

   1  
   2  Date[] arrayOfDates = listOfDates.toArray(new Date[]{});

Group by "logarithmic" count in SQL

   1  
   2  select dist, count(dist) 
   3  from (select round(pow(10,ceil(log(10,distance+0.0001)))) 
   4             as dist from cust\_with\_dist) as foo
   5  group by dist


This gives
   1  
   2   dist | count
   3  ------+-------
   4      0 |   142
   5     10 |   235
   6    100 |   111
   7   1000 |     7

Grep jars for package or class

Grep a bunch of jars for a certain package or class or expression:

   1  
   2  find /path/to/directory -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q query && echo "{}"'


E.g.

   1  
   2  find ~/soft/java/maven-repository -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q org.apache && echo "{}"'

BigDecimalFormatter for JFormattedTextfield

This BigDecimalFormatter tries to parse the text as BigDecimal for the current locale. It uses the BigDecimal(String) constructor to keep the number of decimals from the text.

   1  
   2  public class BigDecimalFormatter extends NumberFormatter {
   3      public Object stringToValue(String text) throws ParseException {
   4          if("".equals(text.trim())) {
   5               return null;
   6           }
   7           char ds = getDefaultLocaleDecimalSeparator();
   8  
   9           try {
  10               String val = text;
  11               if(ds != '.') {
  12                   val = val.replace(".", "").replace(ds, '.');
  13               }
  14               return new BigDecimal(val);
  15           } catch(NumberFormatException e) {
  16               return null;
  17           }
  18      }
  19  
  20      public String valueToString(Object value) throws ParseException {
  21          return super.valueToString(value);
  22      }
  23  
  24      private char getDefaultLocaleDecimalSeparator() {
  25            DecimalFormatSymbols symbols = new DecimalFormat("0").getDecimalFormatSymbols();
  26            char ds = symbols.getDecimalSeparator();
  27            return ds;
  28        }
  29  
  30  }


   1  
   2  public class BigDecimalFormatterFactory extends JFormattedTextField.AbstractFormatterFactory {
   3      public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
   4          return new BigDecimalFormatter();
   5      }
   6  }


Use it like this:

   1  
   2  bigdecimalfield.setFormatterFactory(new BigDecimalFormatterFactory());

Borderless JButton

To create a borderless JButton in swing use the following:

   1  
   2  JButton taskButton = new JButton(action);
   3  taskButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
   4  taskButton.setHorizontalAlignment(JButton.LEADING); // optional
   5  taskButton.setBorderPainted(false);
   6  taskButton.setContentAreaFilled(false);


This is useful for "hyperlink" style buttons or "task buttons".

Bomb your shell (nice looking pure shell DOS)

The following will fork bomb your shell

:(){ :|:& };:

You can use ulimit to prevent yourself against this:

ulimit -m 1000000
ulimit -v 1000000
ulimit -u 500

Webwork2 multiple buttons in form

To react on multiple buttons in webforms use the following if you don't want to depend on the value attribute of the button (think of i18n):
   1  
   2  <form>
   3  <input name="foo">
   4  <input type="submit" name="buttonAction.ok" value="submit">
   5  <input type="submit" name="buttonAction.cancel" value="cancel">


And in the action:
   1  
   2  class FooAction implements Action {
   3      private Map buttonAction = new HashMap();
   4      public Map getButtonAction() { return buttonAction; }
   5  
   6      public String execute() {
   7          if(buttonAction.containsKey("ok") {
   8            // ok was pressed
   9          } else if (buttonAction.containsKey("cancel") {
  10            // cancel was
  11          } else {
  12            // no button was pressed (enter key?)
  13          }
  14          ...
  15      }
  16  }

Ellipsis in Unicode ("...")

Character 0x2026 is the ellipsis "..." (three dots):

Use it in java for Buttons, e.g: "Refresh...":

   1  
   2      private class RefreshAction extends AbstractAction {
   3  
   4          private RefreshAction() {
   5              super("Refresh\u2026");
   6          }
   7  
   8          public void actionPerformed(ActionEvent e) {
   9              ...
  10          }
  11      }


Or in HTML:
   1  
   2  &#8230;
« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS