Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing how to close JPanel programmatically

Tags:

java

swing

My main class extends JPanel and I create a table and a button on this panel.Now I want to close this panel when the user press it.On the internet closing examples are about JFrame.Is there solution for JPanel?

  • There is a panel
  • On Panel there is a table and a button
  • I add an action listener to the button
  • I want to close panel if user press button
  • This is my code now I want when the user press the btnDelete then it close the panel

    public class ListUsers extends JPanel {

    ResultSet rs; ClientDAO dao; JScrollPane scrollPane; JTable table; Object columnId;

    public ListUsers() throws SQLException {

    dao = new ClientDAO();
    rs=dao.getUsers();
    ResultSetMetaData md = rs.getMetaData();  
    int columnCount = md.getColumnCount();  
    
    Vector<String> columns = new Vector(columnCount);  
    
    //store column names  
    for(int i=1; i<=columnCount; i++)  
        columns.add(md.getColumnName(i));  
    
    Vector data = new Vector();  
    Vector row;  
    
    //store row data  
    while(rs.next())  
    {  
        row = new Vector(columnCount);  
        for(int i=1; i<=columnCount; i++)  
        {  
            row.add(rs.getString(i));  
        }  
        data.add(row);  
    }  
    
    
    
    
    
    table = new JTable(data, columns);  
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));  
    table.setFillsViewportHeight(true);  
    table.setVisible(true);  
    table.validate();
    table.setEnabled(true);
    add(table);
    
    
    
    
    table.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e) {
    
    
    
    
    
               final JDialog dialog = new JDialog();
               dialog.setSize(300, 200);
               dialog.setLayout(null);
    
    
               columnId = table.getValueAt(table.getSelectedRow(),0);
    
               Integer no = new Integer(columnId.toString());
               final int i =no.intValue();
    
               String columnNo =columnId.toString();
               String name = table.getValueAt(table.getSelectedRow(),1).toString();
               String surname = table.getValueAt(table.getSelectedRow(),2).toString();
               String gender = table.getValueAt(table.getSelectedRow(),3).toString();
    
    
               String labelText ="<html>Name :<b>"+name+"</b><br>Surname :<b>"+surname+"</b><br>Gender :<b>"+gender+"</b></html>";
               JLabel label=new JLabel(labelText);
               label.setVisible(true);
               label.setBounds(10,  10,300, 100);
               dialog.add(label);
    
    
    
               JButton btnUpdate= new JButton("Update");
               btnUpdate.setVisible(true);
               btnUpdate.setBounds(10,100,100,35);
    
    
               JButton    btnDelete= new JButton("Delete");
               btnDelete.setVisible(true);
               btnDelete.setBounds(150,100,100,35);
    
    
    
    
               dialog.add(btnDelete);
               dialog.add(btnUpdate);
    
    
               btnUpdate.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
    
                }
            });
    
    
               btnDelete.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
    
    
    
                    try {
                        dao.deleteUser(i);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    dialog.setVisible(false);
    
    
                    setVisible(false);
                    System.exit(0);
    
    
    
                }
            });
    
               dialog.setVisible(true);
    
like image 318
Harun ERGUL Avatar asked Nov 05 '14 16:11

Harun ERGUL


People also ask

How do I close a JPanel JFrame?

If you want to close a JFrame, you could use the dispose() method.

How do you close a JFrame programmatically?

You can close the frame programmatically by sending it the WINDOW_CLOSING event, like this: WindowEvent closingEvent = new WindowEvent(targetFrame, WindowEvent. WINDOW_CLOSING); Toolkit.

How do I disable all components of a JPanel?

You can call like disableComponents(this); from within your JPanel.

How do I close a JFrame without exiting?

DISPOSE_ON_CLOSE -- The frame will be closed and disposed but the application will not exit. JFrame. DO_NOTHING_ON_CLOSE -- The frame will be closed but not disposed and the application will not exit.


2 Answers

public void actionPerformed(ActionEvent e) {
  JComponent comp = (JComponent) e.getSource();
  Window win = SwingUtilities.getWindowAncestor(comp);
  win.dispose();
}

e.getSource(): Get Java Component

getWindowAncestor: Returns the first Window ancestor of Component

win.dispose(): Releases all of the native screen resources used by this its subcomponents, and all of its owned children.

like image 128
Lefteris Bab Avatar answered Oct 16 '22 04:10

Lefteris Bab


If you want to 'close' a JPanel, you can hide it.

myPanel.setVisible(false);

And if/when you want to 'open' it again:

myPanel.setVisible(true);
like image 32
forgivenson Avatar answered Oct 16 '22 03:10

forgivenson