Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:54:55+00:00 2026-05-27T20:54:55+00:00

I’m trying to use Scala/Swing to create a Table, one of whose columns is

  • 0

I’m trying to use Scala/Swing to create a Table, one of whose columns is populated by Buttons.

My starting point is the SCells spreadsheet example from Odersky et al’s book, and in particular the use of rendererComponent to control the Component appearing in each cell.

Unfortunately, while this creates a button successfully, the button is not clickable. Here’s a reasonably minimal and self-contained example:

import swing._
import swing.event._

class TableButtons extends ScrollPane {
  viewportView = new Table(2,2) {
    rowHeight = 25
    override def rendererComponent(isSelected: Boolean, hasFocus: Boolean,
                                   row: Int, column: Int): Component =
      if (column == 0) {
        new Label("Hello")
      } else {
        val b = new Button { text = "Click" }
        listenTo(b)
        reactions += {
          case ButtonClicked(`b`) => println("Clicked")
        }
        b
      }
  }
}

object Main extends SimpleSwingApplication {
  def top = new MainFrame {
    title = "Table button test"
    contents = new TableButtons
  }
}

When I run this, I get a table with two columns; the first contains labels, the second contains buttons, but the buttons aren’t clickable.

Possibly related issue: the cells (including the ones containing buttons) are editable. What’s the best way to disable editing?

I’ve seen this question (and this one) and have tried following the approach there (using Table.AbstractRenderer) but that’s also not working – and it’s not at all obvious to me where to put reactions to button clicks in that version. (Is that approach outdated? Or is the approach from the Scala book too simplisitic?)

Thanks for any advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:54:55+00:00Added an answer on May 27, 2026 at 8:54 pm

    You can make a column ineditable by providing a custom table model. However, your cell must be editable, because that is the only way the editing component becomes ‘live’ (repaints state changes, receives mouse events).

    In the normal rendering (using renderComponent), the component is only used to ‘stamp’ it, i.e. the table just calls paint on the component. Thus, performance-wise, you should re-use one instance of each rendering component, instead of creating a new Label / Button in every call.

    So, you need to override the editor method. Unfortunately it returns a plain javax.swing.table.TableCellEditor, and thus you must step down to the plain javax.swing stuff and loose all the Scala goodness…

    The following almost works. Strangely, the button disappears when clicking on it — have no idea why 🙁

    import scala.swing._
    import scala.swing.event._
    import javax.swing.{AbstractCellEditor, JTable}
    import javax.swing.table.TableCellEditor
    import java.awt.{Component => AWTComponent}
    

     

    class TableButtons extends ScrollPane {
      private val lb = new Label("")
      private val b  = new Button
    
      private val buttonEditor = new AbstractCellEditor with TableCellEditor {
        listenTo(b)
        reactions += {
          case ButtonClicked(`b`) => 
            println("Clicked")
            fireEditingStopped()
        }
        def getCellEditorValue: AnyRef = "what value?"
                                   // ouch, we get JTable not scala.swing.Table ...
        def getTableCellEditorComponent(tab: JTable, value: AnyRef, isSelected: Boolean,
                                           row: Int, col: Int): AWTComponent = {
          b.text = "Click!"
          b.peer  // ouch... gotta go back to AWT
        }
      }
    
      viewportView = new Table(2, 2) {
        rowHeight = 25
        override def rendererComponent(isSelected: Boolean, hasFocus: Boolean,
                                       row: Int, column: Int): Component =
          if (column == 0) {
            lb.text = "Hello"
            lb
          } else {
            b.text = "Click?"
            b
          }
    
        override def editor(row: Int, col: Int): TableCellEditor =
          if (col == 1) buttonEditor else super.editor(row, col)
      }
    }
    

     

    val top = new Frame {
      title = "Table button test"
      contents = new TableButtons
      pack()
      visible = true
    }
    

    In any case, check the Oracle JTable tutorial for the intricate details of renderers and editors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.