I am creating a graphical interface that presents a JTextField and other elements. I have created a class with inside : a field String a method that appends the results from sql queries to the field string
I have created the bindings between the string field of this class and the text of the JTextField , but I can not dynamically update the object JTextField.
What is the best practice in this case? any suggestions? thanks
This is the code of class:
public class firstStatistics { private String result;
public void getResult(String mese, String anno){
result = "targets_ais_ship_class_a_all_aaaammgg \n";
result += "Num di giorni:\t" + nDays + "\n";
try {
/*
Connection with DB
*/
Statement stmt = con.createStatement();
//for(int i=1;i<=nDays;i++){
for(int i=1;i<=1;i++){
// create and execute a SELECT
ResultSet rs = stmt.executeQuery("****" + anno + nMonth + getStringDay(i));
rs.next();
result += getStringDay(i)+"/"+ mese + "/"+ anno + ":\t" + rs.getInt(1) + "\t Time(ms): " + elasped + "\n";
}
// close statement and connection
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
}
this is code of GUI: public class maresStatisticsGui {
private JFrame frmMaresStatistics;
private JTextField txtAnno;
private JTextField txtLat1;
private JTextField txtLong1;
private JTextField txtLat2;
private JTextField txtLong2;
private static firstStatistics fs;
private JTextArea txtResult;
/**
* Launch the application.
*/
public static void main(String[] args) {
fs = new firstStatistics();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
maresStatisticsGui window = new maresStatisticsGui();
window.frmMaresStatistics.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public maresStatisticsGui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMaresStatistics = new JFrame();
frmMaresStatistics.setTitle("Mares Statistics");
frmMaresStatistics.setBounds(100, 100, 609, 487);
frmMaresStatistics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMaresStatistics.getContentPane().setLayout(null);
JPanel panel = new JPanel();
frmMaresStatistics.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblMese = new JLabel("Mese");
panel.add(lblMese);
Choice choice = new Choice();
panel.add(choice);
choice.add("");
choice.add("Gennaio");
//...
JLabel lblAnno = new JLabel("Anno(aaaa)");
panel.add(lblAnno);
txtAnno = new JTextField();
txtAnno.setFont(new Font("Tahoma", Font.PLAIN, 15));
panel.add(txtAnno);
panel.revalidate();
panel.repaint();
JPanel panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Risultato", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
frmMaresStatistics.getContentPane().add(panel_2);
panel_2.setLayout(null);
txtResult = new JTextArea();
panel_2.add(txtResult);
JButton btnRicerca = new JButton("Ricerca");
btnRicerca.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fs.getResult("Febbraio", "2015");
}
});
frmMaresStatistics.getContentPane().add(btnRicerca);
}
protected void initDataBindings() {
BeanProperty<firstStatistics, String> firstStatisticsBeanProperty = BeanProperty.create("result");
BeanProperty<JTextArea, String> jTextAreaBeanProperty = BeanProperty.create("text");
AutoBinding<firstStatistics, String, JTextArea, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, fs, firstStatisticsBeanProperty, txtResult, jTextAreaBeanProperty);
autoBinding.bind();
}
}
When i call fs.getResult("Febbraio", "2015"), this method writes on string "result" of class "firstStatistics", but the jtextarea does not refresh.
thanks