Scala - Creating a web browser with Scala Swing Code Example

The code for Creating a web browser with Scala Swing

object ScalaBrowser extends SwingApplication { 

  NativeInterface.open
  val browser = new DJBrowser

  def top = new MainFrame {
    title = "Scala Browser" 
    peer.add(browser)
    size = desiredInitialSize
    reactions += {
      case WindowClosing(_) => quit
    }
  }
  
  override def main(args: Array[String]) {
    SwingUtilities.invokeLater(new Runnable {
      def run {
        val f = top
        f.peer.setLocationRelativeTo(null)
        f.peer.setVisible(true)
      }
    })
    NativeInterface.runEventPump
  }

  def desiredInitialSize = {
    val screenSize = Toolkit.getDefaultToolkit.getScreenSize
    val w = (screenSize.getWidth * 0.625f).toInt
    val h = (screenSize.getHeight * 0.625f).toInt
    new Dimension(w, h)
  }

  override def startup(args: Array[String]) {}

}

class DJBrowser extends JPanel {

  import java.awt.BorderLayout
  import javax.swing.JPanel

  setLayout(new BorderLayout)
  val webBrowserPanel = new JPanel(new BorderLayout)
  val webBrowser = new JWebBrowser
  webBrowserPanel.add(webBrowser, BorderLayout.CENTER)
  add(webBrowserPanel, BorderLayout.CENTER)

}
Code by IncludeHelp, on August 10, 2022 22:15
Reference: alvinalexander.com

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.