Best Way For JComboBox To Detect Which Item Is Selected
I recently posted articles on how to get the selected item chosen in a JComboBox. Using an ItemListener is painfully confusing if you do not know how it works since it fires two events. The best listener to use for a JComboBox to listen which item is selected is an ActionListener.
The same ActionListener...
Source: Tech Tips Tricks
Detect Which Screen Monitor Your Coordinates Belong To Using Java
Getting a screen’s size is simple using Toolkit class’ getScreenSize(). But what if you are working using more than 1 screen monitor, say a dual monitor? The ToolKit class won’t apply in this case. There are a few set of classes in Java that can do the trick namely GraphicsEnvironment...
Source: Tech Tips Tricks
Get Value Of Ratio Using Java
This method gets the value based on the ratio that you specify. Getting the value for a 1:1 ratio or 2:1 is pretty simple. What if you want to get the value for a 3:2 ratio? Looks complicated? I did have a hard time when I first encountered this when I was making a Craps Java applet game but I managed…
Source: Tech Tips Tricks
Round Off In N Decimal Places Using Java
This simple method will round off a value in N decimal places.
public static double round(double value, int decimalPlaces) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
...
Source: Tech Tips Tricks
Catch Events After JSplitPane Divider Location Is Moved
Catching events after you move a JSplitPane’s divider can be made possible through the PropertyChangeListenerJSplitPane class. Make sure you supply the ’s DIVIDER_LOCATION_PROPERTY as the parameter so that this listener will listent to modified divider location events.
If you do not supply...
Source: Tech Tips Tricks
Auto Resize JTable Columns
This class auto resizes columns of a JTable, maximizing any non-used width columns and transferring those extra widths to other columns. This way, a column with a JCheckBox will have a width almost equal to that of a JCheckBox’s width. The class TableColumnResizer contains one static method and…
Source: Tech Tips Tricks
Sort JTree
Sorting a JTree’s nodes is easy. Just make sure your nodes are of type DefaultMutableTreeNode. Have your class extend DefaultMutableTreeNode and add this code within the class. As you can see in the code, once a tree node is inserted into the tree, the nodes are sorted alphabetically using the...
Source: Tech Tips Tricks
JWindow Or JDialog?
Recently I wanted to create a form on top of a JWindow since I thought that it would look good for a popup window to have no title bar and title bar buttons. JWindow is usually good for splash screens, those sort of thing. After much researching, a JDialog actually can be set to look and act like a JWindow.
Just...
Source: Tech Tips Tricks
Rotate JLabel Vertically
To rotate a JLabel’s text vertically, you only need to use this class and pass it as a parameter to the JLabel’s setUI() method. Kudos to the developer who created this class to make it easy for other developers to plug this class in and create a vertical JLabel in an instant.
To use this...
Source: Tech Tips Tricks




