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.
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")));
In the following scala code I change foreground, horizontalAlignment and background to some values. However in the GUI these properties are not shown.
The horizontalAlignment remains centered.
The backgrould remains gray.
However the foreground (font color) changes according to the values.
How can I obtain the desired effects?
Thanks for any help!
import scala.swing._
object GuiTest extends SimpleSwingApplication {
def top = new MainFrame {
title = "Label Test"
val tempList = List("Celsius", "Fahrenheit", "Kelvin")
contents = bagPnl(tempList)
val fields = contents(0).peer.getComponents
val valuefields
= for (f <- 0 until fields.length / 2)
yield tempList(f) -> fields.apply(2 * f + 1).asInstanceOf[javax.swing.JLabel]
val tfm = valuefields.toMap[String, javax.swing.JLabel]
tfm.apply("Celsius").setText("35°C")
tfm.apply("Kelvin").setText("0 K")
}
def bagPnl(list: List[String]) = new GridBagPanel {
val gbc = new Constraints
gbc.insets = new Insets(3, 3, 3, 3)
var l = 0
for (title <- list) {
gbc.gridy = l
gbc.gridx = 0
/* title label */
add(new Label {
horizontalAlignment = Alignment.Left
foreground = java.awt.Color.RED
background = java.awt.Color.CYAN
text = title
}, gbc)
gbc.gridx = 1
/* value label */
val t = new Label {
horizontalAlignment = Alignment.Right
foreground = java.awt.Color.BLUE
background = java.awt.Color.YELLOW
name = title
}
t.background = java.awt.Color.GREEN
add(t, gbc)
l = l + 1
}
}
}
sorry I am not allowed to post images yet :-(
GridBagLayout is one hell of a layout manager. You'll be probably better of with GroupLayout, but there is no related panel type in Scala-Swing yet. (Here for an example).
The problem with the label positioning is that its alignment only makes sense when it is given more space than its preferred size. By default, the grid bag layout doesn't give it more space, and the centering is a result of its own alignment (not that of the label). The easiest here is to specify that the components can use up the horizontal space if available:
gbc.fill = GridBagPanel.Fill.Horizontal
The second questions concerns the background color of the label. Here is a related question. In short, by default the label is transparent and its background color ignored. You can switch to opaque painting:
new Label {
...
opaque = true
}
I'm trying to make a custom GTK widget in Vala, but I'm already failing at the very first basic attempt, so I'd like some help in knowing where I'm going wrong. I feel like I must be missing something painstakingly obvious, but I just can't see it.
I have three files with the following contents:
start.vala:
using Gtk;
namespace WTF
{
MainWindow main_window;
int main(string[] args)
{
Gtk.init(ref args);
main_window = new MainWindow();
Gtk.main();
return 0;
}
}
main_window.vala:
using Gtk;
namespace WTF
{
public class MainWindow : Window
{
public MainWindow()
{
/* */
Entry entry = new Entry();
entry.set_text("Yo!");
this.add(entry);
/* */
/*
CustomWidget cw = new CustomWidget();
this.add(cw);
/* */
this.window_position = WindowPosition.CENTER;
this.set_default_size(400, 200);
this.destroy.connect(Gtk.main_quit);
this.show_all();
}
}
}
custom_widget.vala:
using Gtk;
namespace WTF
{
public class CustomWidget : Bin
{
public CustomWidget()
{
Entry entry = new Entry();
entry.set_text("Yo");
this.add(entry);
this.show_all();
}
}
}
As you can see, in main_window.vala, I have two sets of code. One that adds the Entry widget directly, and one that adds my custom widget. If you run the one that adds the Entry widget directly, you get this result:
If you run the one with the custom widget, however, you get this result:
Just for the record, this is the complication command I use:
valac --pkg gtk+-2.0 start.vala main_window.vala custom_widget.vala -o wtf
EDIT:
Following user4815162342's suggestion, I implemented the size_allocate method on my custom Bin widget, like so:
public override void size_allocate(Gdk.Rectangle r)
{
stdout.printf("Size_allocate: %d,%d ; %d,%d\n", r.x, r.y, r.width, r.height);
Allocation a = Allocation() { x = r.x, y = r.y, width = r.width, height = r.height };
this.set_allocation(a);
stdout.printf("\tHas child: %s\n", this.child != null ? "true" : "false");
if (this.child != null)
{
int border_width = (int)this.border_width;
Gdk.Rectangle cr = Gdk.Rectangle()
{
x = r.x + border_width,
y = r.y + border_width,
width = r.width - 2 * border_width,
height = r.height - 2 * border_width
};
stdout.printf("\tChild size allocate: %d,%d ; %d, %d\n", cr.x, cr.y, cr.width, cr.height);
this.child.size_allocate(cr);
}
}
It writes the following in the console:
Size_allocate: 0,0 ; 400,200
Has child: true
Child size allocate: 0,0 ; 400, 200
And the window renders thusly:
GtkBin is an abstract single-child container, typically intended to decorate the child widget in some way, or change its visibility or size. Without some added value, a single-child container would be indistinguishable from the widget it contains and therefore not very useful.
Since GtkBin doesn't know what kind of decorations you will draw around the child, it expects you to implement your own size_allocate. A simple implementation is available in gtk_event_area_size_allocate, a more complex one in gtk_button_size_allocate.
This answer shows a minimal size_allocate implementation in PyGTK which should be straightforward to port to Vala. If you do anything more complex than that, you will need to also implement expose, and possibly other methods, but this will get you started.
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?
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.