Solvek
Solvek

Reputation: 5360

Hide frame instead of close when user click on close button of the window

I have an application which shows a window. Instead of closing window when user presses on "Close" button I want just to hide this window, how can I do this?

Here is my code (it does not work, the app is closed on click):

  object UxMonitor {

  def main(args: Array[String]) {
    val frame = new MainScreen()
    frame.peer.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE)
    initTrayIcon(frame.peer)
  }

....

class MainScreen extends MainFrame {
      private val HEADERS = Array[Object]("Security", "Last Price", "Amount", "Cost")
      private val quotes = new Quotes()
      private val tableModel = new DefaultTableModel()

      title = "UX Monitor"

      tableModel.setColumnIdentifiers(HEADERS)

      private val table = new Table {
        model = tableModel
      }

      contents = new ScrollPane {
        contents = table
      }

      quotes.setListener(onQuotesUpdated)
      quotes.startUpdating

      peer.addWindowListener(new WindowAdapter{
        def windowClosing(e : WindowEvent){
            self.setVisible(false)
        }
      })

      pack

Upvotes: 2

Views: 7752

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

Simplest method is:

final JFrame frame=...;

....

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

Alternatively you can achieve the same result with adding a WindowAdapter overriding windowClosed(...) and in there calling setVisible(false) on JFrame) like so:

...

 frame.addWindowListener(new WindowAdapter() {
    @Override
  public void windowClosing(WindowEvent evt) {
    frame.setVisible(false);
  }
});

Upvotes: 3

Related Questions