I was having much trouble with JTable. Now, I still have it. How many
parameters we need to change, when we change some columns. Because of
its complicated, I try to
build a simple library to decrease that trouble. And perhaps, we can
use
it...
Requirements:
- we have read
007. Register library, and download daniani.jar
- download
derby.jar (database library), and register it to the
project.
- download
sample database (Java DB - Apache Derby 10.8.1.2).
On GoogleDrive page, choose menu: File -> Download. This file contain database=DANIANI_DB; user=daniani_user; password=daniani_pwd;
schema=SCH. Then extract it in a certain location.
Let's do it...
- Create a new JFrame (001. THE BEGINNING)
- Choose JTable component from Pallete, then drop it on our new
frame.
- Design it
- Click jTable1 in Inspector tab then right click to rename = dtble
- Set property autoResizeMode = OFF, and rowHeight=25
- Switch to view Source code, then insert this script below
initComponents():
initComponents();
try{
Connection
dCon=null;
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
dCon
= DriverManager.getConnection("jdbc:derby:"
+ "yourDBfolderLocation/DANIANI_DB"
+ ";user=daniani_user;password=daniani_pwd");
Table dtble = new Table();
dtble.initColumn();
dtble.addColumn("PRT.IDPARENT","idparent", false,
"text", 70, "");
dtble.addColumn("PRT.DINT","dint", false, "number", 50, "");
dtble.addColumn("PRT.DVARCHAR","dvarchar", false, "text", 120, "");
dtble.addColumn("PRT.DBOOLEAN","dboolean", true, "yesno", 90, false);
dtble.addColumn("PRT.DDECIMAL","decimal", true, "numberBD2", 50, "");
dtble.addColumn("PRT.DDATE","date", false, "date",
100, "");
dtble.addColumn("PRT.DTIMESTAMP","timestamp", false, "timestamp", 150, "");
dtble.addColumn("PRT.DCOLOR","color", false, "color",
80, "");
String sqlFrom="FROM SCH.PARENT PRT";
dtble.buildTable(tbl, false, sqlFrom, dCon, false);
//-
render
tbl.setDefaultRenderer(Color.class, new daniani.ColorRenderer(true));
//to view color
dtble.cellFormatNumber(2, new String[]{"decimal"}); // to view fraction
by 2 scale
dtble.cellAlignment(SwingConstants.CENTER,
new String[]{"idparent", "date", "timestamp"}); // center alignment
} catch(Exception cnfe){
System.out.println(cnfe);
}
This should be work.
NOTE:
If the database connection is
failure, try to delete file db.lck in .../DANIANI_DB folder.
REFFERENCE (case-sensitive)
Syntax:
daniani.Table.addColumn(String FieldName, String ColumnName, boolean isEditableCell, String ColumnClass, String ColumnFormat, int ColumnWidth, Object DefaultValue);
|
Column
Format |
|
|
text |
store String
datatype |
|
date |
view Date datatype
in dd-MM-yyyy |
|
dateYMD |
view Date datatype
in yyyy-MM-dd |
|
dateMDY |
view Date datatype
in MM-dd-yyyy |
|
date;ff |
view Date datatype
in custom date-formatted,
ex.: date;dd-yyyy-MM |
|
time |
view Time datatype
in HH:mm:ss |
|
timestamp |
view Timestamp
datatype in dd-MM-yyyy HH:mm:ss |
|
timestampYMD |
view Timestamp
datatype in yyyy-MM-dd HH:mm:ss |
|
timestampMDY |
view Timestamp
datatype in MM-dd-yyyy HH:mm:ss |
|
timestamp;ff |
view Timestamp
datatype in custom timestamp-formatted,
ex.: timestamp;dd-yyyy-MM HH:mm:ss |
|
yesno |
cell with
checklist view |
|
number |
store Integer
datatype |
|
numberF |
store Float
datatype |
|
numberD |
store Double
datatype |
|
numberBD |
store BigDecimal
datatype |
|
numberBD0 |
store BigDecimal
datatype with 0 scale when edited. |
|
numberBD2 |
store BigDecimal
datatype with 2 scale when edited. |
|
numberBD;n |
store BigDecimal
datatype with n scale when
edited,
ex.: numberBD;4 -> store BigDecimal datatype with 4
scale when edited. |
|
color |
view cell in a
certain color |
Syntax:
daniani.Table.buildTable(JTable jtableComponent, boolean isAllowAddNewRow, String FromSQLClause, java.sql.Connection ConnectionName, boolean isHitEnterKeyToRightDirection);