in reference to my earlier question here, I did the code below.
I am trying to make a treeview which shows a radio button for leafs, and checkboxes for non-leaf items. The code below does not show anything. I am sure I am doing something extremely wrong somewhere (or everywhere). Any help is much appreciated.
Thanks
public class RadioCheckBoxTreeView extends TreeView {
public RadioCheckBoxTreeView() {
setCellFactory(new Callback<TreeView<Object>, TreeCell<Object>>() {
#Override
public TreeCell<Object> call(TreeView<Object> param) {
return new RadioCheckBoxCellImpl();
}
});
}
private static class RadioCheckBoxCellImpl extends TreeCell<Object> {
private final CheckBox check = new CheckBox();
private final RadioButton radio = new RadioButton();
private Property<Boolean> prevRadioProp;
public RadioCheckBoxCellImpl() {
}
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
#Override
public void updateItem(Object item, boolean empty) {
if (prevRadioProp != null) {
radio.selectedProperty().unbindBidirectional(prevRadioProp);
prevRadioProp = null;
}
check.selectedProperty().unbind();
if (!empty && item != null) {
Property<Boolean> selectedProp = prevRadioProp;
if (getTreeItem().isLeaf()) // display radio button
{
radio.setText("radio");
radio.selectedProperty().bindBidirectional(selectedProp);
prevRadioProp = selectedProp;
setGraphic(radio);
} else // display checkbox
{
check.setText("check");
check.selectedProperty().bind(selectedProp);
setGraphic(check);
}
} else {
setGraphic(null);
setText(null);
}
}
}
this is what my start method looks like
public void start(Stage primaryStage) {
AnchorPane pane = new AnchorPane();
Scene scene = new Scene(pane);
MyTreeView tv = new MyTreeView();
tv.setRoot(new TreeItem());
TreeItem child1 = new TreeItem();
child1.setValue("1");
TreeItem child2 = new TreeItem();
child2.setValue("2");
TreeItem child3 = new TreeItem();
child3.setValue("3");
tv.getRoot().getChildren().add(child1);
tv.getRoot().getChildren().add(child2);
child2.getChildren().add(child3);
pane.getChildren().add(tv);
primaryStage.setScene(scene);
primaryStage.show();
}
Ok I managed to get this far. I also changed the graphic to change based on its type.
#Override
public void updateItem(Object item, boolean empty) {
if (prevRadioProp != null) {
radio.selectedProperty().unbindBidirectional(prevRadioProp);
prevRadioProp.setValue(true);
}
check.selectedProperty().unbind();
if (!empty && item != null) {
SimpleBooleanProperty selectedProp = prevRadioProp;
if (item.getClass().getName().contains("Option")) // display radio button
{
radio.setText("Option");
radio.selectedProperty().bindBidirectional(selectedProp);
prevRadioProp = selectedProp;
setGraphic(radio);
} else // display checkbox
{
check.setText("Feature");
check.selectedProperty().bind(selectedProp);
setGraphic(check);
}
} else {
setGraphic(null);
setText(null);
}
super.updateItem(item, empty);
}
How do I set the selection logic? Based on the code it gives a "CheckBox.selected : A bound value cannot be set." error.
Related
I try to sort my ListView with the date from TextView.
In the Screenshots you can see my ToDo List, one TextView stores the Due Date. I try to sort the ListView with the dates, but i do not know how to do it right.
Here is the Code i could find in a Tutorial, but it does not work for me.
public class ToDoOverview extends AppCompatActivity {
ListView lv;
List<ToDo> dataSource;
ToDoOverviewListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_overview);
this.lv = (ListView) findViewById(R.id.listToDo);
this.dataSource = ToDoDatabaseHelper.getInstance(this).readAllToDos();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_NewToDo:
createToDo();
return true;
case R.id.menu_dailyToDo:
showDailyToDo();
return true;
case R.id.menu_Sort_Fav:
sortFav();
return true;
case R.id.menu_Sort_Date:
sortDate();
return true;
case R.id.menu_Sort_Progress:
sortProg();
return true;
}
return super.onOptionsItemSelected(item);
}
public void sortDate(){
adapter.sort(new Comparator<ToDo>() {
#Override
public int compare(ToDo todo1, ToDo todo2) {
if(todo1.getDueDate() != null && todo2.getDueDate() != null){
long date = todo1.getDueDate().compareTo(todo2.getDueDate());
if(date != 0) {
refreshListView();
return (int) date;
} else if(todo1.getDueDate() != null){
refreshListView();
return -1;
} else if(todo2.getDueDate() != null){
refreshListView();
return 1;
}
return todo1.getName().compareToIgnoreCase(todo2.getName());
}
});
}
And here is My ToDoOverviewListAdapter Class:
public class ToDoOverviewListAdapter extends ArrayAdapter<ToDo> {
public ToDoOverviewListAdapter(Context context, List<ToDo> objects) {
super(context, 0, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ToDo currentToDo = (ToDo) getItem(position);
View view = convertView;
if(view == null){
view = LayoutInflater.from(getContext()).inflate(R.layout.todo_overview_listitem, parent, false);
}
// First Step
((TextView) view.findViewById(R.id.tvName)).setText(currentToDo.getName());
TextView dueDate = (TextView) view.findViewById(R.id.tvDueDate);
ImageView fav = (ImageView) view.findViewById(R.id.favIcon);
TextView progStatus = (TextView) view.findViewById(R.id.progStatus);
if(currentToDo.getDueDate() == null){
dueDate.setVisibility(View.INVISIBLE);
} else {
dueDate.setVisibility(View.VISIBLE);
dueDate.setText(currentToDo.getDueDate());
}
if(currentToDo.isFavorite()){
fav.setVisibility(View.VISIBLE);
}else{
fav.setVisibility(View.INVISIBLE);
}
if(currentToDo.getProgress() == null){
progStatus.setVisibility(View.INVISIBLE);
} else {
progStatus.setVisibility(View.VISIBLE);
progStatus.setText(currentToDo.getProgress());
}
return view;
}
}
I want to set the Typeface of the TextView to a font in the Assets folder. The problem-code is "var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");," not the first use, but the second one towards the end of my code (the red squiggly line appears under "Assets").
namespace UndergroundSports.Android
{
[Activity]
public class CityPage : Activity
{
Sport[] sports = Sport.Sports;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.SetContentView(Resource.Layout.CityPage);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
Button bttJoin = FindViewById<Button>(Resource.Id.bttJoin);
bttJoin.Click += (sender, e) =>
{
gotoJoinPage();
};
bttJoin.Typeface = font;
ListView lstSports = FindViewById<ListView>(Resource.Id.lstSport);
lstSports.Adapter = new SportsAdapter(this, sports);
lstSports.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
{
Sport selectedFromList = sports[e.Position];
Global.Instance.CurrentSport = selectedFromList;
gotoMembersPage();
};
}
private void gotoJoinPage()
{
var intent = new Intent(this, typeof(JoinPage));
StartActivity(intent);
}
private void gotoMembersPage()
{
var intent = new Intent(this, typeof(MembersPage));
StartActivity(intent);
}
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override Sport this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Length; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
}
}
But when I tried to create a variable containing the font I got an error telling me:
Cannot access a nonstatic member of outer type Android.Content.Context' via nested typeUndergroundSports.Android.CityPage.SportsAdapter' (CS0038) (UndergroundSportsAndroid)"
From looking at related questions I think I need to either create an instance of the Assets object or make it static.
I'm pretty new to C# and don't really understand what's going on. I would appreciate it if someone could explain why I'm unable to access Assets in this part of my code. The part that confuses me the most is that I use the exact same line of code to access the font earlier within the same file without getting that error.
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
Pass your activity's instance to your adapter via constructor, and use it to access Assests
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
....
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
Also, make sure your .ttf file's build action is set to AndroidAssests. Right the .tff file > Build Action > AndroidAsset
I have implemented a custom TableCell & TableColumn to show a hyperlink while the cell is not editing. I want to add setOnAction event for the hyperlink. As i want to reuse the TableCell i cannot add the code in TableCell updateItem method. Is there any way to implement this?
public class TableColumnHyperlink<S> extends TableColumn<S, String> {
public TableColumnHyperlink (String header) {
super(header);
Callback<TableColumn<S, String>, TableCell<S, String>> hypCellFactory =
(TableColumn<S, String> p) -> new TableCellHyperlink();
setCellFactory(hypCellFactory);
}
}
And the TableCell implementation is
public class TableCellHyperlink<S> extends TableCell<S, String> {
private final TextField textField;
private final Hyperlink hyperlink;
public TableCellHyperlink() {
textField = new TextField();
hyperlink = new Hyperlink();
setAlignment(Pos.CENTER_LEFT);
}
#Override
public void startEdit() {
super.startEdit();
createTextField();
setText(null);
setGraphic(textField);
textField.requestFocus();
}
#Override
public void cancelEdit() {
super.cancelEdit();
setText(getItem());
setGraphic(null);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty && (getTableRow() == null ? true : getTableRow().isEmpty()));
if (empty) {
setText(null);
setGraphic(null);
} else {
if(isEditing()) {
setText(getString());
setGraphic(textField);
} else {
setText(null);
hyperlink.setText(getString());
setGraphic(hyperlink);
}
}
}
private void createTextField() {
textField.setText(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
});
}
private String getString() {
return (getItem() != null)?getItem():"";
}
}
If the event handler implementation will vary by instance, you need to pass the event handler (or a function) into the constructor. Since you probably need to access the cell, you'll do something like
public class TableCellHyperlink<S> extends TableCell<S, String> {
private final TextField textField;
private final Hyperlink hyperlink;
public TableCellHyperlink(Consumer<TableCellHyperlink<S> handlerFunction) {
textField = new TextField();
hyperlink = new Hyperlink();
hyperlink.setOnAction(event -> handlerFunction.accept(this));
setAlignment(Pos.CENTER_LEFT);
}
// ...
}
Now you can do something like
TableCellHyperlink<MyType> hyperlinkCell = new TableCellHyperlink<>(cell -> {
MyType rowValue = (MyType) cell.getTableRow().getValue(); // getTableRow() returns TableRow, not TableRow<MyType>
String cellValue = cell.getItem();
// implement action
});
Obviously you can move the parameter up and pass it into the custom TableColumn constructor if you like.
After hours of trying i finally somewhat managed to figure out how to hook a listener to TreeItems in a TreeView, it probably isn't at all the right way to do so but hey it works so far.
Although one thing isn't, that is the "label" or better said text in the TreeItems isn't showing up anymore.
Can anyone look at my code and tell me, if i'm either doing it completely wrong or why the text isn't showing up anymore?
Thanks in advance.
Code:
TreeView<String> tree = new TreeView<>();
TreeItem<String> treeRoot = new TreeItem<>(Login.name + " - " + Login.accountType);
treeRoot.getChildren().addAll(new TreeItem<>("Branches"),
new TreeItem<>("Planning"), new TreeItem<>("Courses"),
new TreeItem<>("Add new item"));
treeRoot.getChildren().get(1).getChildren().addAll(
new TreeItem<>("2014 - Q1"), new TreeItem<>("2014 - Q2"),
new TreeItem<>("2014 - Q3"), new TreeItem<>("2014 - Q4"));
treeRoot.getChildren().get(3).getChildren().addAll(
new TreeItem<>("Branch"), new TreeItem<>("Course"));
for(String str : loadBranchData()) {
treeRoot.getChildren().get(0).getChildren().add(
new TreeItem<>(str));
}
for(String str : loadCourseData()) {
treeRoot.getChildren().get(2).getChildren().add(
new TreeItem<>(str));
}
for(int c = 0; c <= 2; c++) {
treeRoot.getChildren().get(c).setExpanded(true);
}
treeRoot.setExpanded(true);
tree.setPrefWidth(PREFWIDTH);
tree.setRoot(treeRoot);
tree.setShowRoot(true);
tree.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
#Override
public TreeCell<String> call(TreeView<String> p) {
return new EpicTreeCell();
}
});
...
private final class EpicTreeCell extends TreeCell<String> {
#SuppressWarnings("unchecked")
super.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent evt) {
System.out.println("TEST?");
}
});
}
When the custom cell is defined, the setText() method should be called in its overridden updateItem() method.
private final class EpicTreeCell extends TreeCell<String> {
public EpicTreeCell() {
setOnMouseClicked (
new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent evt) {
System.out.println("TEST?");
}
}
);
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(getItem() == null ? "" : getItem().toString());
}
setGraphic(null);
}
}
See the "Custom Java-fx cellfactory messes up the setCellValueFactory" for more information.
I want right click menu option in the tree root node(JavaFX). Could any one help me on this.
TreeItem<String> root = new TreeItem<>(""+selectedDirectory);
root.setExpanded(true);
locationTreeView.setRoot(root);
root.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
You can perform the desired behaviour in two steps:
Defining a custom TreeCell factory on your TreeView;
Attaching a context menu on the TreeCell of the root tree item.
The following code defines the custom TreeCell factory:
// defines a custom tree cell factory for the tree view
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(TreeView<String> arg0) {
// custom tree cell that defines a context menu for the root tree item
return new MyTreeCell();
}
});
And, here is the implementation of a custom tree cell that attaches a context menu for the root tree item:
class MyTreeCell extends TextFieldTreeCell<String> {
private ContextMenu rootContextMenu;
public MyTreeCell() {
// instantiate the root context menu
rootContextMenu =
ContextMenuBuilder.create()
.items(
MenuItemBuilder.create()
.text("Menu Item")
.onAction(
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
System.out.println("Menu Item Clicked!");
}
}
)
.build()
)
.build();
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// if the item is not empty and is a root...
if (!empty && getTreeItem().getParent() == null) {
setContextMenu(rootContextMenu);
}
}
}
The following example ilustrates the use of both, cell factory and custom cell, together:
public class TreeViewWithContextMenuOnRoot extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tree with context menu on root");
TreeItem<String> rootItem = new TreeItem<String> ("Tree root");
rootItem.setExpanded(true);
for (int i = 1; i < 3; i++) {
TreeItem<String> item = new TreeItem<String> ("item" + i);
rootItem.getChildren().add(item);
}
final TreeView<String> tree = new TreeView<String> ();
tree.setRoot(rootItem);
// defines a custom tree cell factory for the tree view
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(TreeView<String> arg0) {
// custom tree cell that defines a context menu for the root tree item
return new MyTreeCell();
}
});
StackPane root = new StackPane();
root.getChildren().add(tree);
primaryStage.setScene(new Scene(root, 200, 100));
primaryStage.show();
}
private static class MyTreeCell extends TextFieldTreeCell<String> {
private ContextMenu rootContextMenu;
public MyTreeCell() {
// instantiate the root context menu
rootContextMenu =
ContextMenuBuilder.create()
.items(
MenuItemBuilder.create()
.text("Menu Item")
.onAction(
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
System.out.println("Menu Item Clicked!");
}
}
)
.build()
)
.build();
}
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// if the item is not empty and is a root...
if (!empty && getTreeItem().getParent() == null) {
setContextMenu(rootContextMenu);
}
}
}
}
You can take a look at the TreeView tutorial to see other uses and examples related to this JavaFX control.