Skip to content

Instantly share code, notes, and snippets.

@Bios-Marcel
Created April 9, 2018 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bios-Marcel/5c877ce7fd450d8390d70e4db20cdc46 to your computer and use it in GitHub Desktop.
Save Bios-Marcel/5c877ce7fd450d8390d70e4db20cdc46 to your computer and use it in GitHub Desktop.
Swing - Example of Creating a JTable that disables HTML rendering in labels.
import org.apache.commons.lang3.reflect.FieldUtils
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JTable
import javax.swing.table.TableCellRenderer
import java.awt.Component
class NoHtmlRenderer() : JLabel(), TableCellRenderer {
init {
putClientProperty("html.disable", true)
}
override fun getTableCellRendererComponent(table: JTable, value: Any, isSelected: Boolean, hasFocus: Boolean,
row: Int, column: Int): Component {
setText(value.toString())
println(getClientProperty("html.disable"))
return this
}
}
fun main(args: Array<String>) {
val data = arrayOf(
arrayOf("Test"), //Normal Text
arrayOf("Test <html> Test"), //Won't be rendered like html anways, since its not valid html
arrayOf("<html> Test <b> Test </b> Test </html>")) //Should be rendered as html if html.disable is false
val columns = arrayOf("Test")
val table = JTable(data, columns)
table.setDefaultRenderer(Object::class.java, NoHtmlRenderer())
val frame = JFrame("JTable - Attempt to disable JLabel HTML rendering")
frame.add(table)
frame.setSize(800,600)
frame.setVisible(true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment