Java concept
C++ Code –> Executable –> OS –> CPU
Java code –> Class –> { “JVM win”, “JVM linux”, CPU that supports JAVA} –> OS –> CPU
Java is like a application (Your Java app) running in another application (JavaVM).
so you need cmd java to run your app (.class)
javac yourJavaApp.java java youJavaApp
Java tool, library and system environment path
- Java IDE list:
- J2EE - for Enterprise system and application
- J2SE - for desktop application
- then click download Java JDK,
- for windows x86 direct link: http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-windows-i586.exe
- idiot spoon feeding video guide if you're too lazy to google or youtube: http://www.youtube.com/watch?v=Hl-zzrqQoSE
- J2ME - for micro and mobile application
add java bin directory path to system environment or user profile environment variable:
- for win dos
set path=c:\java\bin\location;%PATH%
- or go system property > environment variable > user variable : path; and add java location at end
- for mac,linux
export path=/java/bin/location:$PATH
- or edit ~/.bash_profile, add this cmd line in it.
Portable Java JRE
- copy existing JRE to portable device
- when use, in mac or linux terminal,
export JAVA_HOME=/jre_dir/bin/java export PATH=/jre_dir/bin:$PATH
and in dos
set path = %PATH%;d:\jre_dir\bin set JAVA_HOME=d:\jre_dir\bin\java
- force java use local JRE instead of asking system for it
java -verbose -jar MyApp.jar
Quick Syntax
- Data type and access
- String
// compare string new String("test").equals("test") //==> true //ref: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
- Array (static type)
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; System.out.println("size of num[]: " + num.length);
- Dynamic array
import java.util.ArrayList; private ArrayList<Item> items = new ArrayList<Item>(); System.out.println(items.size());
- GUI
- Layout
FlowLayout() //FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT, FlowLayout.LEADING, FlowLayout.TRAILING GridLayout(int rows, int columns) BorderLayout() BoxLayout() GroupLayout() SpringLayout() //ref: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
- JTable operation:
- header update ref: http://www.roseindia.net/java/example/java/swing/ChangeColumnName.shtml
- refresh header display after update
listTable.getTableHeader().getColumnModel().getColumn(0).setHeaderValue("IC No"); listTable.getTableHeader().repaint(); //ref: http://www.java.net/node/692818
- update date ref: http://stackoverflow.com/questions/5918727/updating-data-in-a-jtable
- fill data
for(int i=0;i<list.length;i++){ listTable.getModel().setValueAt(list[i].getICNO(), i, 0); listTable.getModel().setValueAt(list[i].getFirstName(), i, 1); listTable.getModel().setValueAt(list[i].getLastName(), i, 2); listTable.getModel().setValueAt(list[i].getTitle(), i, 3); listTable.getModel().setValueAt(list[i].getOrg(), i, 4); }
- clear data
for (int i = 0; i < table.getRowCount(); i++) for(int j = 0; j < table.getColumnCount(); j++) { table.setValueAt("", i, j); } }
- user input
- custom form dialog
String[] n_arr=new String[3]; JTextField icno = new JTextField(); JTextField firstName = new JTextField(); JTextField lastName = new JTextField(); final JComponent[] inputs = new JComponent[] { new JLabel("IC NO"), icno, new JLabel("First Name"), firstName, new JLabel("Last Name"), lastName }; JOptionPane.showMessageDialog(null, inputs, "Member Creation", JOptionPane.PLAIN_MESSAGE); n_arr[0]=icno.getText(); n_arr[1]=firstName.getText(); n_arr[2]=lastName.getText();
- Data validation for form guis
Quick GUI dev and App dev with NetBean
- Quick GUI design guide from JFrame in NetBean (25 min)
- Quick App Build guide with user interaction (15 min)
Some UI design Tips:
- hide and show part of UI
panelA.setVisible(false); //ref: http://www.codecommit.com/blog/java/techniques-of-java-ui-development
Program Design Pattern
- MVC tips:
- Calc example: http://leepoint.net/notes-java/GUI/structure/40mvc.html