2

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
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Solvek
  • 5,158
  • 5
  • 41
  • 64
  • Try setting your default close operation to do_nothing_on_close but set it in your MainScreen class and leave your window listener as is. – eSuarez Nov 30 '12 at 18:06
  • This frame should probably be a dialog. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Dec 01 '12 at 03:10

1 Answers1

3

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);
  }
});
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138