I am writing a code which takes as input a word (the name of a board game) or a letter and searches it in my DB.
If there are any entries I'll show them in a table, otherwise I don't want to show the empty table and I want to show a label "No results found" which disappears after 1500ms.
The if statement works properly. For example if I write 't' I'll get all the games starting with 't' in a table.
The else statement sometimes has problems:
- if I run my program and I first write as input 'b' (no entries starting with 'b' in my DB) --> it shows the label "no results found" and then it disappears (it works!)
- if I run my program and I write as input 't' (it works as said) and then I write 'a' (I have entries with 'a', it works) [.....] and then finally I write as input 'b' (no entries starting with 'b' in my DB) here is the problem: it shows the label "No results found" but it ALSO shows the table with the results of my very first input (i.e. the games starting with 't').
What am I doing wrong? Thanks!
Here the code after clicking the button "Search":
btnCerca.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*Connect to DB:*/
Database db = new Database();
Connection connection = null;
try
{
connection = db.connectToDB();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
/*Create a query which search in the DB the word/letter entered by the user*/
Query query = new Query (connection);
query.search(textField.getText()); //execute the query
/*Get results:*/
ResultSet rs = query.getResults();
/* check if rs is empty */
boolean res = false;
try
{
res = rs.isBeforeFirst();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
ListTableModel model = null;
JScrollPane scrollPane = new JScrollPane();
if (res) //there is something in rs
{
try
{
model = ListTableModel.createModelFromResultSet(rs);
}
catch (SQLException e1)
{
e1.getMessage();
}
/*The table which will show the results*/
JTable table_1 = new JTable(model);
scrollPane.setBounds(10, 75, 1200, 300);
panelRicerca.add(scrollPane);
scrollPane.setViewportView(table_1);
}
else //rs is empty
{
JLabel lblNoGiochi = new JLabel("No results found.");
lblNoGiochi.setBounds(448, 30, 400, 14);
panelRicerca.add(lblNoGiochi);
panelRicerca.repaint();
/*...I want the label disappear after 1500ms:*/
int delay = 1500;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
panelRicerca.remove(lblNoGiochi);
panelRicerca.repaint();
}
};
new Timer(delay, taskPerformer).start();
}
}
});
------------------------EDIT------------------------
I made a simpler example which is affected by the same problem so you can compile and check quicker.
If I write "hello" I want to display a table. If I write anything else, I want the table to show a label instead.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JTextField textField;
private JTable table;
private JLabel lbl;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
lbl = new JLabel("Hey");
lbl.setBounds(378, 236, 46, 14);
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(102, 48, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnOk = new JButton("ok");
btnOk.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 20, 50, 50);
table = new JTable();
if (textField.getText().equals("hello"))
{
System.out.println("I'm in the if statement");
//show the table:
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(table);
}
else
{
System.out.println("I'm in the else statement");
//don't show the table:
scrollPane.remove(table);
/* Already tried to use:
- revalidate and repaint the scrollPane
- revalidate and repaint the contentPane
- remove the entire scrollPane from the ContentPane
- remove the table from the scrollPane, then remove the scrollPane from the ContentPane
*/
//and show a label instead:
frame.getContentPane().add(lbl);
frame.getContentPane().repaint();
}
}
});
btnOk.setBounds(211, 47, 89, 23);
frame.getContentPane().add(btnOk);
}
}