I would like to have a text field whose value always reflects that of a certain field in a given object. I thought Bindable might be the way to do this. However, using the following example:
#!/usr/bin/env groovy
import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import static javax.swing.JFrame.EXIT_ON_CLOSE
class TextModel {
#Bindable String text
}
def textModel = new TextModel()
def builder=new SwingBuilder()
builder.build {
frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
gridLayout cols: 1, rows: 2
textField id: 'textField'
bean textModel, text: bind{ textField.text }
label text: bind{ textModel.text }
}
}
textModel.text="AAAA"
modified from:
http://groovy.codehaus.org/Bindable+and+Vetoable+transformation
only the label text is set to that of textModel, but not that of the textField.
Any ideas???
Thank you
Misha
p.s. I seem to be able to get the opposite behavior, where the TextField reflects that state of the variable, but its value is not updated, if I do:
#!/usr/bin/env groovy
import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import static javax.swing.JFrame.EXIT_ON_CLOSE
class TextModel {
#Bindable String text
}
def textModel = new TextModel()
def builder=new SwingBuilder()
builder.build {
frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
gridLayout cols: 1, rows: 2
textField id: 'textField',text:bind{ textModel.text }
label text: bind{ textModel.text }
}
}
textModel.text="AAAA"
p.p.s. If I add both:
#!/usr/bin/env groovy
import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import static javax.swing.JFrame.EXIT_ON_CLOSE
class TextModel {
#Bindable String text
}
def textModel = new TextModel()
def builder=new SwingBuilder()
builder.build {
frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
gridLayout cols: 1, rows: 2
textField id: 'textField',text:bind{ textModel.text }
bean textModel, text: bind{ textField.text }
label text: bind{ textModel.text }
}
}
textModel.text="AAAA"
I get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
p.p.p.s. This is my best solution:
#!/usr/bin/env groovy
import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import static javax.swing.JFrame.EXIT_ON_CLOSE
class TextModel {
#Bindable String text
}
def textModel = new TextModel()
textModel.text="AAAA"
def builder=new SwingBuilder()
builder.build {
frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
gridLayout cols: 1, rows: 2
textField id: 'textField',text:textModel.text
bean textModel, text: bind{ textField.text }
label text: bind{ textModel.text }
}
}
The Griffon guide on binding, describes the mutual property as being what you want. Even though you're not using Griffon in this case, bind seems to be a standard Groovy feature. If you create textField like this:
textField id: 'textField', text: bind('text', source: textModel, mutual: true)
textField will get its initial value from textModel.text, write updates to it when the user types in the field, and display the updated value when changes to textModel.text occur (from some background thread, say). When I tried to bind two text inputs like this, I started getting the IllegalStateExceptions you described, but it seems one input and multiple labels are fine.
Related
I am trying to add a Lumino widget to the 'right' panel of the Jupyter Lab UI. However, I am not able to set the icon in the title bar of the widget using font awesome icons. Here's my code:
class ExampleWidget extends Widget {
constructor() {
super()
this.id = 'myextension-widget'
this.title.label = 'Sample'
this.title.closable = true
this.title.iconClass = 'face-smile'
this.addClass('mypanel')
}
}
What am I missing here? Can someone please help?
You need to:
had the 'fa' class
fix the font class name ('fa-smile-o' instead of 'face-smile')
This example works for me:
class ExampleWidget extends Widget {
constructor() {
super()
this.id = 'myextension-widget'
this.title.label = 'Sample'
this.title.closable = true
this.title.iconClass = 'fa fa-smile'
this.addClass('mypanel')
}
}
I am trying to attach onclick listener on images in this code, using Glide framework but I'm not able to find a solution for that. Can someone help with this?
Images are showing in emulator already.
Thanks in advance
package com.example.andoridlifecycle.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.example.andoridlifecycle.R
import kotlinx.android.synthetic.main.item_custom_row.view.*
class ItemAdapter(private val context: Context, private val urls: ArrayList<String>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
/**
* Inflates the custom view which is designed in xml layout file
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_custom_row,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
// Gets the number of items in the list
override fun getItemCount(): Int {
return urls.size
}
// A ViewHolder describes an item view and metadata about its place within the RecyclerView.
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imageView: ImageView = view.iv_image
}
}
I'm Java Developer so I cannot code with Kotlin but I'll show you how to set OnClickListener on your imageView. For using OnclickListener on imageView no need for Glide Library.
Just add the below lines in your onBindViewHolder
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Your new code looks like the below:-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
//code for onClick on image
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
Make sure to change JAVA to Kotlin in my code. If you any problem is causing, Let me know
I put a string in a variabele and used it as text in a label. This works for shorter text, but my text is a few sentences long, so it ends with ... instead of finishing my string. I'm working with JavaFX 1.0. Does anyone know how i can fix this?
Edit:
This is the code I wrote. I put the variable HandleidingDobbelsteen in a stage in an other file.
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
package var DobbelsteenHandleiding = Label {
text: "Hier komt te staan wat de uitkomst van de dobbelsteen betekent."
translateX: 830
translateY: 245
width: 140
textWrap: true;
}
package var HandleidingDobbelsteen = Group {
content:[
DobbelsteenHandleiding,
Rectangle {
x: 825, y: 220
width: 150, height: 120
fill: null
stroke: Color.BLACK
strokeWidth: 1.3
},
Rectangle {
x: 825, y: 220
width: 150, height: 20
fill: Color.BLACK
},
Text {
font : Font {size: 18}
x: 845, y: 235
content: "De regels"
fill: Color.WHITE
},
]
}
I am going to assume that your instructor is mistaken on the version
of JavaFX they have you using. If they are correct, these instructions may not work for you (as I am not familiar with steam-powered Java)...
The likely cause of your issue is that the String being used to set the Label's textProperty is longer than the Label is able to display. Without seeing your actual code, I have a couple of suggestions:
First of all, make sure that your Label is wrapping its text:
label.setWrapText(true);
Secondly, you should make sure that the Label is wrapped in a layout container that can handle a node that grows (not always necessary, but recommended). My recommendation is a VBox:
VBox vBox = new VBox(label);
Again, there is some assumption here on the version of JavaFX you're actually using, but you could try this simple example to see if it works for you:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// Simple interface
VBox vBox = new VBox(5);
vBox.setPadding(new Insets(10));
vBox.setAlignment(Pos.CENTER);
Label label = new Label();
// Let's add some very long text to our Label
label.setText("This is long text. It doesn't mean much because I am just writing things to make it long. Yes, I am that clever. Don't you just love this? Neither do I!");
// Make sure the Label is setup to wrap text when it reaches the end of its boundaries
label.setWrapText(true);
// Add the Label to our VBox
vBox.getChildren().add(label);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
}
Result:
To confirm that label.setWrapText(true); actually makes a difference, here's the result of the above code without that line, complete with the elipses ("...") you're experiencing:
I've been following example straight from demo app. Nevertheless, I get an error when using addAll() method to add table data to the model:
Error in ./MdlTableComponent class MdlTableComponent - inline template:7:18 caused by: Cannot read property 'columns' of undefined
Code is below, any insights would be greatly appreciated! (I simplified code and hard-coded table data).
import { Component, OnInit } from '#angular/core';
import {
MdlDialogService, MdlDefaultTableModel, IMdlTableModelItem
} from 'angular2-mdl';
export interface ITableItem extends IMdlTableModelItem {
fname: string;
fsize: number;
ftype: string;
}
#Component({
...
})
export class MyComponent implements OnInit {
public fileListModel: MdlDefaultTableModel = new MdlDefaultTableModel(
[
{ key: 'fname', name: 'File Name' },
{ key: 'fsize', name: 'File Size', numeric: true },
{ key: 'ftype', name: 'File Type' }
]);
public fileListData: [ITableItem] = [
{
fname: 'aaa.png',
fsize: 500,
ftype: 'image/png',
selected: true
}];
ngOnInit(): void {
this.fileListModel.addAll(this.fileListData); // Error is thrown here
Check your corresponding template. It should be using fileListModel
<mdl-table-selectable mdl-shadow="2"
[table-model]="fileListModel"
[table-model-selected]="selected"
(table-model-selectionChanged)="selectionChange($event)">
</mdl-table-selectable>
I want Wijmo FlexGrid column width should cover full width.
As I'm dynamically assigning the columns so can't use [width]="'*'"
Assign a template reference variable to the grid. E.g. flex1
<wj-flex-grid #flex1
[itemsSource]="data">
<wj-flex-grid>
Then in the ngAfterViewInit callback set up your columns and assign the column in question the width '*', e.g.
import { Component, ViewChild, AfterViewInit } from '#angular/core';
import { Collection } from 'wijmo/wijmo';
import { FlexGrid } from 'wijmo/wijmo.grid';
#Component({ ... })
export class MyComponent implements AfterViewInit {
#ViewChild('flex1') protected grid: FlexGrid;
protected data: any;
constructor() {
this.data = new Collection([
{ id: 1, country: 'United States' },
{ id: 2, country: 'Germany' },
]);
}
public ngAfterViewInit() {
// Missing part: Set up columns programmatically
// OP has done this already.
// Set the column width of the column with binding to the `country` property
this.grid.columns.getColumn('country').width = '*';
}
}