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 JCheckBox()
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?
Related
I am attempting to use the geotools.org library (Version 24) to write a user-defined function to style my GIS map according to an external data source. The documentation for the GeoTools library includes this tantalizing paragraph in the section on Functions:
When a function is used as part of a Style users often want to calculate a value based on the attributes of the Feature being drawn. The Expression PropertyName is used in this fashion to extract values out of a Feature and pass them into the function for evaluation.
This is exactly what I want to do. However, the documentation includes no actual example of how to do this.
I have spent several days trying various permutations of the Function definition, and I get the same result every time: My user-defined function only receives the geometry attribute, not the extra attributes I have specified.
I have verified that everything else works:
The features are read correctly from the shapefile
The function is actually called
The Feature geometry is passed into the function
Upon completion, the map is drawn
But I cannot get the Geotools library to pass in the additional Feature properties from the shapefile. Has anyone gotten this working, or can you even point me to an example of where this is used?
My current function definition:
package org.geotools.tutorial.function;
import mycode.data.dao.MyTableDao;
import mycode.data.model.MyTable;
import org.geotools.filter.FunctionExpressionImpl;
import org.geotools.filter.capability.FunctionNameImpl;
import org.opengis.feature.Feature;
import org.opengis.filter.capability.FunctionName;
import org.opengis.filter.expression.PropertyName;
import org.opengis.filter.expression.VolatileFunction;
import org.springframework.beans.factory.annotation.Autowired;
import java.awt.*;
import java.beans.Expression;
import java.util.Random;
public class DataFunction extends FunctionExpressionImpl {
#Autowired
MyTableDao myTableDao;
public static FunctionName NAME =
new FunctionNameImpl(
"DataFunction",
Color.class,
FunctionNameImpl.parameter("featureData1", PropertyName.class),
FunctionNameImpl.parameter("featureData2", PropertyName.class));
public DataFunction() {
super("DataFunction");
}
public int getArgCount() {
return 2;
}
#Override
public Object evaluate(Object feature) {
Feature f = (Feature) feature;
// fallback definition
float pct = 0.5F;
if (f.getProperty("featureData1") != null) {
MyTable temp = myTableDao.read(
f.getProperty("featureData1").getValue().toString(),
f.getProperty("featureData2").getValue().toString());
pct = temp.getColumnValue();
}
Color color = new Color(pct, pct, pct);
return color;
}
}
EDIT: I am invoking the function programmatically through a Style that is created in code. This Style is then added to the Layer which is added to the Map during the drawing process.
private Style createMagicStyle(File file, FeatureSource featureSource) {
FeatureType schema = (FeatureType) featureSource.getSchema();
// create a partially opaque outline stroke
Stroke stroke =
styleFactory.createStroke(
filterFactory.literal(Color.BLUE),
filterFactory.literal(1),
filterFactory.literal(0.5));
// create a partial opaque fill
Fill fill =
styleFactory.createFill(
filterFactory.function("DataFunction",
new AttributeExpressionImpl("featureData1"),
new AttributeExpressionImpl("featureData2")));
/*
* Setting the geometryPropertyName arg to null signals that we want to
* draw the default geomettry of features
*/
PolygonSymbolizer sym = styleFactory.createPolygonSymbolizer(stroke, fill, null);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[] {rule});
Style style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
I think you want to set up your function to take a pair of Strings or Doubles (or whatever those attributes are) so something like:
public static FunctionName NAME =
new FunctionNameImpl(
"DataFunction",
Color.class,
FunctionNameImpl.parameter("featureData1", Double.class),
FunctionNameImpl.parameter("featureData2", Double.class));
public DataFunction(List<Expression> params, Literal fallback) {
this.params = params;
this.fallback = fallback;
}
then your evaluate method becomes:
#Override
public Object evaluate(Object feature) {
Feature f = (Feature) feature;
// fallback definition
float pct = 0.5F;
Expression exp1 = params.get(0);
Expression exp2 = params.get(1);
if (f.getProperty("featureData1") != null) {
MyTable temp = myTableDao.read(
exp1.evaluate(f, Double.class),
exp2.evaluate(f, Double.class));
pct = temp.getColumnValue();
}
Color color = new Color(pct, pct, pct);
return color;
}
and you would use it in the style using something like:
Fill fill =
styleFactory.createFill(
filterFactory.function("DataFunction",
filterFactory.propertyName("featureData1"),
filterFactory.propertyName("featureData2")));
using urwid, I'm trying to separate the highlight/walk and cursor functionality of a Pile widget. How can I use up/down to change which widget is highlighted, while keeping the cursor in a different widget?
The default focus behavior couples the cursor with attribute (highlighting) behavior. The example below shows one way to decouple these, where a list of SelectableIcons retains the highlight feature, while the cursor is moved to a separate Edit widget. It does this via:
overriding the keypress method to update the focus where the cursor is not
wrapping each SelectableIcon in AttrMap that change their attribute based on their Pile's focus_position
after changing the SelectableIcon attributes, the focus (cursor) is set back to the Edit widget via focus_part='body'
self._w = ... is called to update all widgets on screen
There may be more concise ways of doing this, but this should be rather flexible.
import urwid
def main():
my_widget = MyWidget()
palette = [('unselected', 'default', 'default'),
('selected', 'standout', 'default', 'bold')]
urwid.MainLoop(my_widget, palette=palette).run()
class MyWidget(urwid.WidgetWrap):
def __init__(self):
n = 10
labels = ['selection {}'.format(j) for j in range(n)]
self.header = urwid.Pile([urwid.AttrMap(urwid.SelectableIcon(label), 'unselected', focus_map='selected') for label in labels])
self.edit_widgets = [urwid.Edit('', label + ' edit_text') for label in labels]
self.body = urwid.Filler(self.edit_widgets[0])
super().__init__(urwid.Frame(header=self.header, body=self.body, focus_part='body'))
self.update_focus(new_focus_position=0)
def update_focus(self, new_focus_position=None):
self.header.focus_item.set_attr_map({None: 'unselected'})
try:
self.header.focus_position = new_focus_position
self.body = urwid.Filler(self.edit_widgets[new_focus_position])
except IndexError:
pass
self.header.focus_item.set_attr_map({None: 'selected'})
self._w = urwid.Frame(header=self.header, body=self.body, focus_part='body')
def keypress(self, size, key):
if key == 'up':
self.update_focus(new_focus_position=self.header.focus_position - 1)
if key == 'down':
self.update_focus(new_focus_position=self.header.focus_position + 1)
if key in {'Q', 'q'}:
raise urwid.ExitMainLoop()
super().keypress(size, key)
main()
If you really need this, it probably makes sense to write your own widgets -- maybe based on some classes extending urwid.Text and urwid.Button
There is no real "highlight" feature in the widgets that come with urwid, there is only a "focus" feature, and it doesn't seem to be easy to decouple the focus highlight from the focus behavior.
You probably want to implement your own widgets with some sort of secondary highlighting.
I am using Swing in Scala "org.scala-lang" % "scala-swing" % "2.11.0-M7".
I want to set position for my components explicitly. It is possible to do in Swing API for Java.
Question: is it possible to set absolute position for components in Swing Scala API?
Swing API for Scala example:
import scala.swing._
object PositionAbsolute extends SimpleSwingApplication {
lazy val top = new MainFrame() {
title = "PositionAbsolute"
val label = new Label("I want to be at (0, 0)")
val panel = new FlowPanel()
panel.preferredSize = new swing.Dimension(300, 400)
panel.contents += label
contents = panel
}
}
I know it's a little late for a response - and I am not at all an expert in these things, so please bear with me.
If you absolutely want or need to do absolute positioning of controls with swing in scala, here is a way to do it:
import scala.swing.{Button, Dimension, MainFrame}
object Main extends App {
val b1 = new Button {
text = "one"
preferredSize = new Dimension(60, 30)
}
val b2 = new Button {
text = "two"
preferredSize = new Dimension(80, 40)
}
val b3 = new Button("three")
b1.peer.setBounds(25, 5, b1.peer.getPreferredSize.width, b1.peer.getPreferredSize.height)
b2.peer.setBounds(55, 50, b2.peer.getPreferredSize.width, b2.peer.getPreferredSize.height)
b3.peer.setBounds(150, 15, b3.peer.getPreferredSize.width, b3.peer.getPreferredSize.height)
javax.swing.SwingUtilities.invokeLater(() => {
val frame: MainFrame = new MainFrame {
title = "AbsoluteLayoutDemo"
resizable = true
size = new Dimension(300, 150)
}
frame.peer.setLayout(null)
frame.peer.add(b1.peer)
frame.peer.add(b2.peer)
frame.peer.add(b3.peer)
frame.visible = true
})
}
I don't like it very much myself, but this works.
I compiled this code with scala version 2.13.8 and
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0"
in the build.sbt file
This was my translation of the java-example in docs.oracle.com/javase/tutorial/uiswing/layout/none.html
but I made a few changes that I thought would make sense for a scala example.
I am not exactly sure what the consequences of this approach are, so please use at your own risk - because I am really not sure how this works.
How can I left align, rather than right align, the label in an iPython 3 widget, such as IntSlider? My ultimate goal is to left align a set of labeled widgets. This entails left aligning their labels, since the label is the leftmost element of each widget.
I've read the discussion in
Aligning TextBox Widgets in IPython Notebooks, but (a) it focuses on making more space for a right-aligned label, and (b) the proposed solution does not seem to affect the label width. (As an aside, I would be interested in finding cell-executable code that can reset the minimum label width.)
I've also read the discussion in Change the size of the label in an IPython notebook widget, but it doesn't seem to offer a simple solution.
Thanks for your help.
Addendum (2015-06-02):
Looks like widget.interactive() does not play nicely with the solution suggested by Jakob. Example:
from IPython.html import widgets
from IPython.display import display
def mySlider(text='', twidth=100, min=0, max=10, value=5):
c1 = widgets.HBox()
ints = widgets.IntSlider(min=min, max=max, value=value)
text = widgets.HTML(text, width=twidth)
c1.children = (text, ints)
return c1
s1 = mySlider('Test')
s2 = mySlider('TestTest')
s3 = mySlider('TestTestTest')
def process(a, b, c):
print([a, b, c])
widgets.interactive(
process,
a=s1.children[1].value,
b=s2.children[1].value,
c=s3.children[1].value
)
yields slider labels a, b, c with the usual alignment.
You can simply combine an IntSlider with an Html widget to create your custom widget like
from IPython.html import widgets
from IPython.display import display
def mySlider(text='', twidth=100):
c1 = widgets.HBox()
ints = widgets.IntSlider()
text = widgets.HTML(text, width=twidth)
c1.children = (text, ints)
return c1
Using this method, some widgets could look like
s1 = mySlider('Test')
s2 = mySlider('TestTest')
s3 = mySlider('TestTestTest')
display(s1,s2,s3)
Update to work with interact
To use these custom widgets with interact it is necessary to add some properties and callbacks. The interact method requires the widget.description and the widget.value arguments to setup the interactive widgets. As our container widget does not have these arguments, they are added manually. Moreover it is necessary to link the container.value with the IntSlider.value. This is once realized by a simple assignment, and more important via the on_trait_change method.
Finally, the interact methods calls the process function on widget.on_trait_change callbacks, thus the container.on_trait_change method is replaced by the IntSlider.on_trait_change call.
The updated code looks like:
def mySlider2(text='', twidth=100, min=0, max=10, value=5):
c1 = widgets.HBox()
ints = widgets.IntSlider(min=min, max=max, value=value)
text = widgets.HTML(text, width=twidth)
c1.children = (text, ints)
c1.description = text
c1.value = ints.value
def update(name, value):
c1.value = value
ints.on_trait_change(update,'value')
c1.on_trait_change = ints.on_trait_change
return c1
s1a = mySlider2('Test')
s2a = mySlider2('TestTest')
s3a = mySlider2('TestTestTest')
widgets.interactive(
process,
a=s1a,
b=s2a,
c=s3a
)
In scala.swing, I can add a component to a container like so:
new Frame {
contents += label
}
but sometimes I'd like to clear the contents of a container and replace them with new components. Based on the docs, I should be able to do:
frame.contents.remove(0)
or
frame.contents.clear
but neither of those compile ("clear/remove is not a member of Seq[scala.swing.Component]").
How can I resolve this? Also, it seems that I can't call frame.contents += blah after intialization. If this is so, how do I add a new component to a Container?
If you're talking about Frame specifically, you can only add one item, so use the method
def contents_= (c: Component) : Unit
and you should be good. Try this out in the REPL (one line at a time so you can see what's going on):
import scala.swing._
val f = new Frame { contents = new Label("Hi") }
f.visible = true
f.contents = new Label("Hey there")
If you're using something that is intended to have multiple items like a BoxPanel, contents is a Buffer so you can add to it and remove from it:
val bp = new BoxPanel(Orientation.Vertical)
val (label1,label2) = (new Label("Hi"), new Label("there"))
bp.contents += label1
bp.contents += label2
f.contents = bp // Now you see "Hi" "there" stacked
bp.contents -= label1
f.pack // "Hi" disappears--need pack not repaint to fix layout
bp.contents += label1
f.pack // "Hi" is back, but at the end
If you have something else like a Component that you're extending, it is your job to override contents with a buffer or have some other way of modifying it (or inherit from SequentialContainer as J-16 said).
contents in Container was a Seq[];
you need a SequentialContainer for that remove method.