From e16536713302d799c82bcad67b8ffc297b652eac Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 7 May 2024 14:31:14 +0200 Subject: [PATCH] made search field react on keypress --- src/ch/hevs/isc/slopify_v2/GUI.scala | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ch/hevs/isc/slopify_v2/GUI.scala b/src/ch/hevs/isc/slopify_v2/GUI.scala index faefe30..37d8cf5 100644 --- a/src/ch/hevs/isc/slopify_v2/GUI.scala +++ b/src/ch/hevs/isc/slopify_v2/GUI.scala @@ -1,5 +1,6 @@ package ch.hevs.isc.slopify_v2 +import java.awt.event.{KeyEvent, KeyListener} import java.awt.{GridBagConstraints, GridBagLayout, LayoutManager} import java.util import javax.swing._ @@ -29,7 +30,11 @@ class GUI extends JFrame { }) val artistsSearchBar: JTextField = new JTextField() - artistsSearchBar.addActionListener(_ => updateArtistsSearch()) + artistsSearchBar.addKeyListener(new KeyListener { + override def keyTyped(keyEvent: KeyEvent): Unit = {} + override def keyPressed(keyEvent: KeyEvent): Unit = {} + override def keyReleased(keyEvent: KeyEvent): Unit = updateArtistsSearch() + }) val col1Constraints: GridBagConstraints = new GridBagConstraints() col1Constraints.fill = GridBagConstraints.BOTH @@ -94,10 +99,14 @@ class GUI extends JFrame { val songsSelectionModel: ListSelectionModel = songsTable.getSelectionModel songsSelectionModel.addListSelectionListener(e => { val row: Int = e.getFirstIndex - val songName: String = songsTableModel.getValueAt(row, 0).asInstanceOf[String] - val song: Option[Song] = curAlbum.get.getSongByTitle(songName) - if (song.isDefined) { - selectSong(song.get) + try { + val songName: String = songsTableModel.getValueAt(row, 0).asInstanceOf[String] + val song: Option[Song] = curAlbum.get.getSongByTitle(songName) + if (song.isDefined) { + selectSong(song.get) + } + } catch { + case e: ArrayIndexOutOfBoundsException => {} } })