Podstawą tego modułu jest wyświetlanie co jakiś czas np. 20s lotów odbywających się o danej godzinie w tabeli. Dane mają te być cały czas aktualizowane czyli pojawiać się i znikać.
Połączenie z bazą jest, tabela też wyświetla, ale problem jest w tym, że po każdym przejściu pętli tabela odsuwa się w prawo i po lewej stronie wyświetla się jakby kolejny pusty panel. Próbowałem juz nawet czyszczenia tabeli.
Jak więc radzicie to zrobić podejśc do tego ?
CODE:
public class PopulateTablewitchFlyData {
static JFrame f = new JFrame("All actual fly");
static JScrollPane scroll = new JScrollPane();//tab
static JTable tab = new JTable();
static DefaultTableModel dtm = new DefaultTableModel();
//Part where is creata a table
public void createTable()
{
f.setBounds(200,200,400,300);
f.setVisible(true);
}
public void getTable() throws Exception
{
int x = 1;
String actualDate = "2019-05-21 10:00:00s";
JFrame frame = new JFrame("Actual Fly Connections");
JTable tab = new JTable();
DefaultTableModel dtm = new DefaultTableModel();
while(x > 0)
{
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/baza_lotow","root","");
Statement st = connect.createStatement();
ResultSet rs2 = st.executeQuery("select * from przyloty where data_i_godzina_przylotu >= '"+actualDate+"' ORDER BY data_i_godzina_przylotu limit 2 ");
ResultSetMetaData rsmd = rs2.getMetaData();
int columns = rsmd.getColumnCount();
String col[] = new String[columns];
//Get data from columns
for(int i=0;i < columns; i++)
{
col[i] = rsmd.getColumnName(i+1);
dtm.addColumn(col[i]);
}
Object rows[] = new Object[columns];
while(rs2.next())
{
for(int a=0; a < columns; a++)
{
rows[a] = rs2.getString(a+1);
}
dtm.addRow(rows);
}
tab.setModel(dtm);
JScrollPane scroll = new JScrollPane(tab);
frame.add(scroll);
frame.setBounds(200,200,400,300);
frame.setVisible(true);
//dtm.fireTableDataChanged();
connect.close();
//Sleep appplication on 20 min
TimeUnit.SECONDS.sleep(10);
}
}
public static void close() {
Connection connect = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
public static void main(String[] args) throws Exception{
PopulateTablewitchFlyData populate = new PopulateTablewitchFlyData();
populate.getTable();
}
}