Archive for the ‘programming’ Category
Auto Resize JTable Column Width
This code should resize your JTable column width based on the contents of the header and data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public JTable autoResizeColWidth(JTable table, DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; // Set the width col.setPreferredWidth(width); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment( SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); for (int i = 0; i < table.getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); column.setCellRenderer(new DefaultTableColour()); } return table; } |
Example:
1 2 3 | // Must pass the model DefaultTableModel model = new DefaultTableModel(); jTable = autoResizeColWidth(jTable, model); |
Tested on JRE v5 and JRE v6
Reading MyKad using Visual Basic
It seems that a lot of people asking how to read MyKad using Visual Basic, but since I don’t have the slightest idea on how to code in VB, I started to search around. To my surprise, Xenon (the one who reverse engineered MyKad to get the APDU) actually wrote a small application in VB to read MyKad. So, for those who can’t afford to buy SDK from commercial company, you can download the code here for FREE !
Printing to Zebra S4M Using Java and ZPL II
Apparently there’re some codes scattered on the net telling people that you can print to a Zebra printer by sending ZPL II codes using PrintService. But the problem is, it’s not working, I don’t know why, maybe because of a different version of printer or model but I’m pretty sure the thing that came out from the printer is just ordinary text not barcode which is what the code was supposed to output.
UPDATE: Problem solved and we know have 3 ways in printing using Zebra S4M
Read the rest of this entry »
Automatically Sign JARs using Ant and Bash
This guide is more towards Netbeans project, but it can be used as a reference for you to customize the script to suit your needs.
The signer bash file:
1 2 3 4 | #!/bin/bash find . -name "*.jar" -exec jarsigner -keystore /path/to/your/key -storepass yourpassword '{}' yourkeystorename \; echo 'JARs signed'; exit 0 |
This script will actually search all files ending with .jar from the current directory recursively and then sign it. This means, it can be used separately without ant script. Just make it executeable and run it.
Put this in the last line of your build.xml but it must before the closing tag of the “project” (build.xml can be found in your main project directory)
1 2 3 4 5 6 7 8 | <project> . . . <target name="-post-jar"> <exec dir="${dist.dir}" executable="/path/to/your/signer" os="Linux" /> </target> </project> |
This script will basically sign the jars after all the jars has been build. Please note that it’s better if you set all the path using absolute type of path.
After this, you just have to use Clean and Build to automatically generate the JARs and also automatically sign it. This script will also sign all of your libraries. Good luck :)
Reading MyKad in Linux
If this is your first time reading my guide in reading MyKad, I suggest you read my first post regarding MyKad. It will give you the basic idea what this is all about.
This article will only guide you what you should do so that you can read MyKad in Linux. However, your success rate still depends on the type of hardware you use, especially the reader since there are not so many reader that support Linux. These are my working environment in Linux:
- Slackware 12
- JDK 6 Update 3
- Netbeans 6.0
- Athena Smart Card Reader
Reading MyKad in Windows
For my first post in this blog, I decided to give out an example application to read MyKad which is written in Java. This application has successfully read:
- Old and new IC number
- Name
- Sex
- Date of birth
- Birth place
- Nationality
- Race
- Religion
- Address
- Photo