Saturday, June 29, 2013

011. JTABLE POPUP (LIKE AS JCOMBOBOX)


Sometimes, we need Combobox more complicated. So, I build it with some helps.


Requirements (we have read):
- 010. Add Component (Control) to Palette.

  • Create new JFrame, named comboTable.
  • Drag dnComboBox control (component),  named cb.


  •  Drag the other controls, for example: JTextField = tx, JFormattedTextField = ft, JSlider = sld, JCheckbox = cb, JLabel = lb. Then design it, like as below:
  • Edit the source code:
    public comboTable() {
            initComponents();

            try{
                Connection dCon=null;
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                dCon = DriverManager.getConnection("jdbc:derby:"
                        + "E:/DKARTZK/WEB/14netbeaner/DANIANI_DB"
                        + ";user=daniani_user;password=daniani_pwd");

                cb.initColumn();

                cb.addColumn("PRT.IDPARENT","idparent", false, "text", 40, "");
                cb.addColumn("PRT.DINT","dint", false, "number", 40, "");
                cb.addColumn("PRT.DVARCHAR","dvarchar", false, "text", 120, "");
                cb.addColumn("PRT.DBOOLEAN","dboolean", false, "yesno", 90, false);
                cb.addColumn("PRT.DDECIMAL","decimal", false, "numberBD2", 50, "");
                cb.addColumn("PRT.DDATE","date", false, "date", 100, "");
                cb.addColumn("PRT.DTIMESTAMP","timestamp", false, "timestamp", 150, "");
                cb.addColumn("PRT.DCOLOR","color", false, "color", 80, "");

                String sqlFrom="FROM SCH.PARENT PRT";

                cb.buildCombo(0, 2, sqlFrom, dCon);
                cb.setTabTitle("parent table");
                cb.setSizePopup(600, 5);

                //--render
                cb.cellFormatNumber(2, new String[]{"decimal"});
                cb.cellAlignment(SwingConstants.CENTER, new String[]{"idparent", "date", "timestamp"});
                cb.cellColoring(Color.GREEN, new String[]{"dvarchar"});

                JTable tbl=cb.getTable();
                tbl.setDefaultRenderer(Color.class, new daniani.ColorRenderer(true));
               
                //cb.setIndex(0);

                JTextField txCb=(JTextField) cb.getFormattedTextField();
                txCb.addFocusListener(new java.awt.event.FocusAdapter() {
                    public void focusLost(java.awt.event.FocusEvent evt) {
                        lb.setOpaque(true);
                        lb.setBackground((Color) cb.getSelectedCellValue("color"));
                        tx.setText(cb.getSelectedCellValue("dvarchar").toString());
                        ft.setValue(cb.getSelectedCellValue("decimal"));
                        sld.setValue((Integer) cb.getSelectedCellValue("dint"));
                        ck.setSelected((Boolean) cb.getSelectedCellValue("dboolean"));

                    }
                });

            } catch(Exception cnfe){
                System.out.println(cnfe);
            }
        }
  • Syntax:
    cb.buildCombo(int boundColumn, int viewColumn, String sqlFrom, Connection dnCon);
    boundColumn = column index that set as ID. So we can call: cb.getID();
    viewColumn = column index that will be shown on dnComboBox when row being selected.


    NOTE:
    For reference, we can read 008. Add Database to JTable (JTable Part 1). 

010. ADD COMPONENT (CONTROL) TO PALETTE

Requirements (we have read):
- 007. Register library, and download (or re-download for new version) daniani.jar.

  • On menu Tools, select Palette -> Swing/AWT Components
  • On Palette Manager, click Add from Library...
  • Choose daniani library (for example), then click Next >
  • On Install Components to Palette, choose dnComboBox, then click Next >
  • Choose palette category, select Swing Controls category. Click Finish.
  • dnComboBox will be shown on Palette Manager dialog. Click Close.
  • dnComboBox will be added to Palette tab.

Thursday, June 27, 2013

009. ADD ROW / REMOVE ROW (JTABLE PART 2)

 Requirements (we have read):
- 008. Add Database to JTable (JTable Part 1).

In this case, we are talking about my way.
We need to change script to be:
dtble.buildTable(tbl, true, sqlFrom, dCon, false);

And, the first column should be editable. For example:
dtble.addColumn("'' STATUS","status", true, "java.lang.String", "text", 70, "");

Then:
- to add row: type -dn- in first column
- to remove row: type -xx- in first column.

Perhaps:
- tbl.setValueAt("-dn-", rowIndex, 0);
- tbl.setValueAt("-xx-", rowIndex, 0);

Note:
This action doesn't affect the database. We need another action to do that. May be, someday I will write it.

Monday, June 10, 2013

008. ADD DATABASE TO JTABLE (JTABLE PART. 1)




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);