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