- Storing data in tool class
class tool{
private String sno, code, type;
public tool(String sno, String code, String type){
this.sno = no;
this.code = code;
this.type = type;
}
public String getsno(){
return sno;
}
public String getCode(){
return code;
}
}
- modelData.java to fetch database data
public class modelData{
private String code;
public modelData(String code){
this.code = code;
}
public ArrayList<tool>moldList(){
ArrayList<tool>moldList = new ArrayList<>(); //declare the arraylist
Connection conn = null;
String dbURL = "jdbc:postgresql://serveraddress:port/database";
Properties parameters = new Properties();
parameters.put("user","schema_username");
parameters.put("password","schema_password");
try{
conn = DriverMeanager.getConnection(dbURL, parameters);
if(conn != null){
String sql = "SELECT * FROM myTable WHERE id>'0'";
if(code.equals("abc")==false)sql += " AND code LIKE '%"+code+"%'";
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet result = statement.executeQuery();
tool data;
While(result.next()){
data = new tool(
result.getString("id"),
result.getString("code"),
result.getString("type")
);
moldList.add(data);
}
conn.close();
}
}catch(SQLException ex){JOptionPane.showMessageDialog(null,ex);}
return moldList;
}
}
public void show_model(){
//clear data
model.setRowCount(0);
modelData data = new modelData(myItem.getText());
ArrayList<tool> list = data.moldList(); // fetch data and keep inside arraylist list
Object[] row = new Object[11]; // table column's size
for(int i=0; i<list.size();i++){
row[0] = list.get(i).getsno();
row[1] = list.get(i).getCode();
model.addRow(row); // clear data(old) and add back data(new)
}
model.getDataVector();
// set the data to table
table = new JTable(model){
//set the table cell's properties
public Component prepareRenderer(TableCellRenderer renderer, int row, int col){
//go thru each row and each column of the table
Object value = getModel().getValueAt(row,col);
((JLabel)comp).setHorizontalAlignment(JLabel.CENTER);
comp.setForeground(Color.BLACK);
comp.setFont(new Font("Arial", Font.BOLD,18));
Color lightBlue = new Color(173, 216,230);
Color lightCoral = new Color(240,128,128);
if(value.equals("ABC")){
comp.setBackground(Color.magenta);
}else{
comp.setBackground(Color.White);
}
return comp;
}
};
table.setRowHeight(25);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
scrollableTextArea = new JScrollPane(table);
scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
}
//constructor
public toodie(){
intUI();
// the following line must be inside the constructor
Object[] columnNames = {"id","name","code","spec"};
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(columnNames);
show_model();
setGrid();
}
private void intUI(){
setTitle("myTitle");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon whebIcon = new ImageIcon("src/resources/x.png");//the close icon on the top right
setIconImage(webIcon.getImage());
productCode = new JTextField("Product", 15);
Font codeFont = productCode.getFont().driveFont(Font.PLAIN, 25f);
productCode.setFont(codeFont);
productCode.setHorizontalALignment(productCode.CENTER);
productCode.setColumns(10);
btnRow = new JButton("ROW");
Font btnFont = btnRow.getFont().deriveFont(Font.PLAIN, 18f);
btnRow.setFont(btnFont);
btnRow.setCursor(new Cursor(Cursor.HAND_CURSOR);
btnRow.setPreferredSize(new Dimension(90,30));
btnBack = new JButton(new ImageIcon("src/resources/img-left.png"));
btnBack..addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String t = productCode.getText();
String s = t.substring(0,t.length()-1);
productCode.setText(s);
show_model();
}
});
table.setAutoCreateRowSorter(true); //enable row sorting
table.setAutoResizeMode(JTable_AUTO_RESIZE_OFF);
//the following setting is to hide the column for viewing
table.getColumnModel().getColumn(0).setPreferredWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getColumnModel().getColumn(0).setMaxWidth(0);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
}