Painting checkbox icons in a custom JComponent :Scala
I'm creating a custom scala component which needs an unchecked icon at (100,100) and checked icon at (200,100), the same icons used by swing. My code below works, but looks quite weird because I need to create dummy checkboxes. Is there a standard way to accomplish this ? ( No I'm not trying to add components to container etc etc...this is not a layout management problem...am trying to create a custom component )
val comp = new JComponent() {
override def paintComponent(g:Graphics) {
val cb1 = new JCh开发者_JAVA百科eckBox()
val cb2 = new JCheckBox()
cb2.setSelected( true )
val icon = UIManager.getIcon("CheckBox.icon")
icon.paintIcon( cb1, g, 100,100)
icon.paintIcon( cb2, g, 200,100)
}
}
val f = new JFrame
f.getContentPane().setLayout( new BorderLayout )
f.getContentPane().add( comp , BorderLayout.CENTER )
f.pack
f.show
You shouldn't define components within paintComponent
. Define them in the component's constructor so that they're not re-defined each time the component is redrawn.
The standard thing to do if you don't want the user to change the values of checkboxes would be to use setEnabled(false)
.
Also, have you tried using the scala.swing
package?
精彩评论