Reputation: 3
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:
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);
}
}
Upvotes: 0
Views: 61
Reputation: 324118
You add the table to the panel:
panelRicerca.add(scrollPane);
Later you add the label to the panel:
panelRicerca.add(lblNoGiochi);
Nowhere do you remove the table pane from the panel, so it will always be displayed.
You need to either:
CardLayout
. The CardLayout
allows you to swap components in the same panel so only one is ever visible. Read the Swing tutorial on How to Use CardLayout for more information.Edit:
Well in my original answer I suggested you need to remove the table from the panel. Actually I meant "scroll pane" since you never add the table to the panel. Notice the code I posted is showed add the "scroll pane" to the panel. You were meant to do the opposite.
In your original code you added and removed the label from the same panel. My suggestion was to add/remove the scroll pane from the same panel.
Again the same concept, you could remove the table from the scroll pane. But again you add the table to the "viewport" of the scrollpane. So if you want to remove it you do the opposite. You remove it from the viewport of the scrollpane, by using setViewportView(null)
. This won't remove the scroll pane from the panel (which is still the easiest solution) but the table won't display in the scroll pane.
However, neither of the above suggestions will work in you demo code because you keep creating a new JScrollPane in your MouseListener so you don't have a reference to the scrollpane that you added to the frame. So the scroll pane variable needs to be an instance variable and you create the scroll pane in your constructor.
Also, don't use null layouts. Swing was designed to be used with layout managers. So the basic code when do dynamic layout changes is:
panel.remove(...);
panel.add(...);
panel.revalidate(); // to invoke layout manager
panel.repaint(); // repaint changes.
So the basic answer I was trying to give is that if your want to "remove" something, you need to do the opposite of what you did to add the component to the frame.
Upvotes: 1