How to Get Selected Item From LibGDX List? - libgdx

I’ve tried a few different methods to get a selected item from a list (see code below), but everything I’ve tried only returns the first item in the list no matter which item was actually selected. Visually it appears to work because the correct item gets highlighted when it is clicked on.
As a general overview of what I’m trying to do, I have a folder that contains all of the saved preset files (json files), then I read the names of all the files into a list of strings, from this list a specific preset can be selected, and then I have a separate “load” textbutton that loads the item that was selected from the list. But as mentioned above, the correct item is not loaded from the list when the load button is clicked.
Here is my code:
public class PresetLoadMenu extends Menu {
private GUI gui;
private SaveManager saveManager;
private Table scrollPaneContainerTable;
private ScrollPane scrollPane;
private Table scrollTable;
private List<String> presetList;
private TextButton loadButton;
private FileHandle rootFolderHandle = Gdx.files.external(“presets/”);
public PresetLoadMenu(GUI gui){
this.gui = gui;
refreshList();
scrollTable = new Table();
scrollTable.add(presetList);
scrollPane = new ScrollPane(scrollTable);
scrollPaneContainerTable = new Table();
scrollPaneContainerTable.add(scrollPane).size(this.getWidth(), this.getHeight()*.2f);
add(scrollPaneContainerTable);
row();
loadButton = new TextButton("LOAD", gui.menuStyles.getMenuOkCancelButtonStyle());
loadButton.addCaptureListener(new ChangeListener(){
#Override
public void changed(ChangeEvent event, Actor actor){
// METHOD 1:
// System.out.println("SELECTED PRESET: " + presetList.getSelected());
// METHOD 2:
// System.out.println("SELECTED PRESET: " + presetList.getSelection().getLastSelected());
// METHOD 3:
// for (int i=0; i<presetList.getSelection().size(); i++){
// System.out.println("INDEX: " + i + " SELECTED PRESET: " + presetList.getSelection().toArray().get(i));
// }
}
});
add(loadButton).size(loadButton.getWidth(), loadButton.getHeight());
}
public void refreshList(){
FileHandle[] files = rootFolderHandle.list();
Array<String> namesArray = new Array<String>();
for(FileHandle file: files) {
namesArray.add(file.name());
}
presetList = new List<String>(gui.menuStyles.getListStyle());
presetList.setItems(namesArray);
}
}
The last method I tried using a for loop just to see if it would print out the other items that I clicked on, but it still printed the first item just one time and didn't detect that I had clicked on any of the other items.

On request I'm copying my last comment as an answer in order to close my question.
The problem was actually something else outside of the class that I posted, so presetList.getSelected() may work just fine now that I figured out what the problem was. But before I figured it out, I had swapped out the List for a ButtonGroup, which is actually better for me than using the List anyways so it worked out. The List has a built in listener that selects the item on touch down, so items would get selected when trying to scroll up or down. With a button I can have a listener just for the checked state, which is what I want.

Based on your answer, I solved it by creating a table of TextButton (buttonTable) , every of them initialize with an string:
final TextButton button = new TextButton("my string",skin);
and a listener:
button.addListener(new ClickListener() {
public void clicked (InputEvent event, float x, float y) {
button.getText()));
}
});
This buttonTable initialize a ScrollPane to allow vertical scrolling:
scrollPane = new ScrollPane(buttonsTable);
This scrollPane was added to table used to format my form:
mainTable.add(scrollPane);
This table was the actor added to the stage:
stage.addActor(mainTable);

Related

Libgdx stage 'frozen"

In my game there is a menu, in this menu, there are 4 tabs and each tab have its own stage with tables scrollpanes and buttons (is it a good idea ?)
My problem is that every stage seems "frozen", buttons are not responding and scrollpanes don't scroll
My menu structure :
Menu class
-> render a tab (render selectedTab, selectedTab is a Tab object (custom class) that is asigned with a specific tab (Ex: sele shopTab (extend tab class))
// menu class
private Tab selectedTab;
private Tab RecipeTab, SellTab, UpgradeTab, ShopTab;
// menu constructor
RecipeTab = new RecipeTab(viewport, sb, itemsdata);
SellTab = new SellTab(viewport, sb, hud);
UpgradeTab = new UpgradeTab(viewport, sb, itemsdata);
ShopTab = new ShopTab(viewport, sb);
selectedTab = RecipeTab;
// render
selectedTab.render(sr, delta);
// on tab change
public void setSelectedTab(Tab newTab) {selectedTab = newTab;}
setSelectedTab(ShopTab);
-> menu contain a navbar to switch tabs (the selected tab is asigned with another tab object)
I don't know if the issue comes from the stages or the actors. even a simple textbutton doesn't work
// how my stages are made
// constructor
this.stage = new Stage(viewport, sb); // ExtendViewport (same everywhere), Spritebatch
Gdx.input.setInputProcessor(this.stage);
// render
this.stage.draw();
this.stage.act(delta);
I figured it out.
The answer was simple, there was a conflict with the inputProcessor
i was seting the inputProcessoron each tabs
so i added a setInputProcessor() method to the tabs a called it on tab change

jpopupmenu where to add listener

I have a jTextField with a button along side to call a popupmenu. The popupmenu contains a list of standard text for the jTextField. This list is held in a list array of variable length, since it can be added too elsewhere in the program.
I'm using the following to generate the popupmenu.
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
}
My question is how do I include the listener that captures which item on the popup menu is selected so I can then put the value into the jTextField.
You need to add a ItemListener interface. If your JMenuItem implements ItemSelectable interface. You can change your code to be like below:
ItemListener il =
e -> {JMenuItem source = (JMenuItem)(e.getSource());
String s = "Item event detected on '" + source.getText() +",New state: "
+ ((e.getStateChange() == ItemEvent.SELECTED) ?
"selected":"unselected");
jTextField.setText(s);
};
for(i=0;i<=menuArray1.size()-1;i++){
JMenuItem item = new JMenuItem((String) menujlArray1.get(i));
jPopupMenu1.add(item);
item.addItemListener(il);
}
More examples here and here

SWT JFACE: How to move one row to another row in JFace TableViewer by using TAB Key [duplicate]

What I'd like to do is be able to tab between elements in table.
I currently am creating my table like this.
this.tableViewer =
new TableViewer(parent , SWT.FULL_SELECTION);
tableViewer.setUseHashlookup(true);
table = tableViewer.getTable();
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.grabExcessVerticalSpace = true;
table.setLayoutData(gridData);
table.setLinesVisible(true);
table.setHeaderVisible(true);
...
/** Create the Cell Editor Array - will hold all columns **/
editors = new CellEditor[table.getColumnCount()];
/** Cell Editor Row 1 **/
/** Set the column properties **/
tableViewer.setColumnProperties(columnNames);
/** Assign the cell editors to the viewer **/
tableViewer.setCellEditors(editors);
/** Set the cell modifier for the viewer **/
tableViewer.setCellModifier(new MyCellModifier(this));
//Create the Table Viewer
/** Table Viewer Content and Label Provider **/
tableViewer.setContentProvider(new MyContentProvider(this));
tableViewer.setLabelProvider(new MyLabelProvider());
But I'm not sure how to set up the tabulation. Everything else works as far as editing columns, showing data, etc. Just stuck on this last part.
If I've missed obvious documentation or javadocs - my apologies and even pointing to those would be great.
Although the solution thehiatus posted is very low level and will probably work (I haven't tested it), JFace gives you a framework for this specific problem. See the org.eclipse.jface.viewers.TableViewerFocusCellManager along with org.eclipse.jface.viewers.CellNavigationStrategy classes to solve this problem.
I think by default tab does not jump from cell to cell in an swt table. Instead it traverses to the next control. So you'll also need to tell it not to traverse when tab is pressed
KeyListener keyListener = new KeyLisener()
{
public void keyPressed(KeyEvent evt)
{
if (evt.keyCode == SWT.TAB)
{
// There are numerous setSelection methods. I'll leave this to you.
tableViewer.getTable().setSelection(...)
}
}
public void keyReleased(KeyEvent evt){}
}
TraverseListener traverseListener = new TraverseListener()
{
public void keyTraversed(TraverseEvent evt)
{
if (evt.keyCode == SWT.TAB)
evt.doit = false;
}
}
tableViewer.getTable().addKeyListener(keyListener);
tableViewer.getTable().addTraverseListener(traverseListener);
Also, as derBiggi suggested, the listeners need to be added to the Table object, not the TableViewer.
I couldn't get the desired behavior with a TraverseListener (it would not traverse within the table), and I had trouble getting it to work with a FocusCellManager and CellNavigationStrategy. I finally found this solution that enables me to tab from column to column within a row and automatically activate the editor.
Viewer viewer = ...
TableViewerFocusCellManager focusCellManager =
new TableViewerFocusCellManager(
viewer,
new FocusCellHighlighter(viewer) {});
ColumnViewerEditorActivationStrategy editorActivationStrategy =
new ColumnViewerEditorActivationStrategy(viewer) {
#Override
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
ViewerCell cell = (ViewerCell) event.getSource();
return cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2;
}
};
TableViewerEditor.create(viewer, focusCellManager, editorActivationStrategy,
TableViewerEditor.TABBING_HORIZONTAL);
You need to add a KeyListener and set the selection or focus to the next cell:
tableViewer.getTable().addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed");
if (e.keycode == SWT.TAB)
{
System.out.println("Detected TAB key");
// set table viewer selection
}
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Released");
}}
);
I also had to implement tabbing between elements in a table. We use Grid from Nebula as the table.
Firstly, I had to suppress tabbing the focus preventing it from moving out of the table.
and then I added a Key Listener which moves the focus/selection to the next cell:
I also made my own algorithm to move the selection one cell to the right and when at the end of the row, move it to the beginning of the next row. When end of table is reached, the selection moves back to the first cell in the table.
This solved the problem for me.

MvvmCross, Mvx.MvxListView and custom binding in MvxItemTemplate

In a MvvmCross app, I have a page with the classic chat behavior (WhatsApp like): this page shows the history of messages exchanged between two users with the last message at the bottom of the list.
I've successfully implemented the view in Windows Phone 8.1, but I'm struggling with a problem in Android.
I'll give you a short introduction and description of my problem and next I'll go through technical details.
INTRODUCTION
Actually, my need is to apply different style to messages sent by different users: tipically align left messages sent from other user and align right messages sent by me (I do this through the weight property); I need to apply a different drawable background and set different gravity property also.
I use custom binding because, AFAIK, those properties cannot be binded with classic binding: local:MvxBind="Gravity MyPropery" doesn't work because there is no Gravity property.
So, I have of course two axml files:
the first one contains the Mvx.MvxListView
the second one contains the item template for MvxListView
And I've created three different custombinding (for Background, Gravity and Weight) following these guides:
http://slodge.blogspot.it/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
In MvvmCross how do I do custom bind properties
THE PROBLEM
I want that, when a user opens the chat View, the list widget shows automatically the last message. To accomplish this, I scroll programmatically the list to the last message and this seems to be the problem.
If I don't scroll programmatically, when I open the page and scroll manually to the end of the page, all custom bindings are applied successfully: I can see messages aligned right and left, with correct background and weight applied.
If I force the scroll programmatically, when I open the page I see a strange behavior: all the messages are present (classic binding, such as Text property, have been successfully applied), but custom bindings are missing. All the messages have the same background and are all left aligned.
BUT, if I scroll manually up and down, the custom binding are processed and the messages are displayed with right style.
DEBUG ANALYSIS
To debug the behaviour I've put a simple static counter in a custom binding procedure to track every time the function is processed.
public class LinearLayoutWeightTargetBinding : MvxAndroidTargetBinding
{
public static int debugCounter = 0;
public LinearLayoutWeightTargetBinding(object target) : base(target)
{
}
protected LinearLayout MyTarget
{
get { return (LinearLayout)Target; }
}
public override Type TargetType { get { return typeof(bool); } }
protected override void SetValueImpl(object target, object value)
{
var ll = (LinearLayout)target;
var itsMe = (bool)value;
var weight = itsMe ? (float)20.0 : (float)5.0;
var layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WrapContent, weight);
ll.LayoutParameters = layoutParams;
Log.Debug("MeeCHAT", string.Format("LinearLayoutWeightTargetBinding::SetValueImpl::ItsMe:{0} - counter:{1}", itsMe, ++debugCounter));
}
public override MvxBindingMode DefaultMode { get {return MvxBindingMode.TwoWay;} }
}
By this way I saw that actually by scrolling up and down the custom bindings are applied (debugCounter increases correctly).
BUT when I apply the programmatically scroll, only the first 10 items are processed by the custom bindings and this seems the reason why I see the messages without the right style. Because I have a long list, only the first 10 items are processed but they are not visible (they are out of the visible area) and the visibile items have not been processed.
TECHNICAL DETAILS
Here are some details related to technical aspects of my app. I try to give you all important aspects.
ORGANIZATION OF THE VIEWS
By following the approach described by Greg Shackles in this article http://gregshackles.com/presenters-in-mvvmcross-navigating-android-with-fragments/ I have just one general Activity for the app and one Fragment for each View; then through a Presenter is possible to activate the right ViewModel and manage the stack of the navigation.
The Fragment for the View where I have the Mvx.MvxListView widget is
public class MyMatchersChatView : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
var result = this.BindingInflate(Resource.Layout.MyMatchersChatView, null);
var headerFrame = result.FindViewById<FrameLayout>(Resource.Id.headerFrameMyMatchersChatView);
var headerWidget = new HeaderWidget() { ViewModel = this.ViewModel };
var tran = ChildFragmentManager.BeginTransaction();
tran.Add(headerFrame.Id, headerWidget, "headerMyMatchersChat");
tran.Commit();
var listView = result.FindViewById<MvxListView>(Resource.Id.messagesList);
listView.SetSelection(listView.Adapter.Count - 1); // Scroll to the end of the list
return result;
}
}
The statement listView.SetSelection(listView.Adapter.Count - 1); force the list to scroll to the end.
Last two things: how the custom bindings are registered and how are applied in axml file.
REGISTRATION OF CUSTOM BINDING
In Setup.cs I have:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterFactory(new MvxCustomBindingFactory<LinearLayout>("CustomWeight",
(b) => new LinearLayoutWeightTargetBinding(b)));
}
APPLYING OF CUSTOM BINDING
In my axml I have:
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="CustomWeight IsCurrentUser">
LISTVIEW AND VIEWMODEL
Here is the code of ListView
<Mvx.MvxListView
android:id="#+id/messagesList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="ItemsSource MyMessages"
local:MvxItemTemplate="#layout/mymatcherschatview_itemtemplate" />
and the property in ViewModel
private IEnumerable<MyMatchMessageModel> _myMessages;
public IEnumerable<MyMatchMessageModel> MyMessages
{
get { return _myMessages; }
set
{
_myMessages = value;
RaisePropertyChanged(() => MyMessages);
}
}
ENVIRONMENT
Finally, here is my environment:
Visual Studio 2015
MvvmCross 3.5.1
Core targets: .NET Framework 4.5, Windows 8, ASP.NET Core 5.0, Windows Phone 8.1, Xamarin.Android, Xamarin.iOS, Xamarin.iOS (Classic)
The Android app target is API Level 19 (Xamarin.Android v4.4 Support)
Xamarin 3.11.1450.0
Xamarin.Android 5.1.6.7
Someone can help me to understand if I'm doing something wrong?
Thanks for reading and for any help!
>>EDIT 1<<
I've changed my layout by adding stackFromBottom and transcriptMode properties and by removing the scrolling to below programmatically in Fragment obtaining an auto-scroll behavior, but the problem still remains: to see messages with correct style I have to manually scroll up and down (to activate the custom bindings)
Here is the new axml...
<Mvx.MvxListView
android:id="#+id/messagesList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
local:MvxBind="ItemsSource MyMessages"
local:MvxItemTemplate="#layout/mymatcherschatview_itemtemplate" />
...and the new code in Fragment
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
var result = this.BindingInflate(Resource.Layout.MyMatchersChatView, null);
var headerFrame = result.FindViewById<FrameLayout>(Resource.Id.headerFrameMyMatchersChatView);
var headerWidget = new HeaderWidget() { ViewModel = this.ViewModel };
var tran = ChildFragmentManager.BeginTransaction();
tran.Add(headerFrame.Id, headerWidget, "headerMyMatchersChat");
tran.Commit();
return result;
}
First thing I would do is to make sure that your custom binding is always getting called.
Set a breakpoint on the SetValueImpl() method and check it´s getting called on those problematic items. If that happens, then the issue relies on the view no getting updated for any reason and you should work on that. If it doesn´t break, you will know for sure it´s a custom binding problem (possibly a bug) in MvxAdapter.
If you find out it´s the second one. I would suggest getting rid of your custom binding and creating your own ChatListAdapter : MvxAdapter as follows:
public class CoolChatListAdapter : MvxAdapter
{
public CoolChatListAdapter(Context context, IMvxAndroidBindingContext bindingContext) : base(context, bindingContext)
{
}
protected override View GetBindableView(View convertView, object source, int templateId)
{
var item = source as MyMatchMessageModel;
var weight = item.IsCurrentUser ? (float) 20.0 : (float) 5.0;
var ll = (LinearLayout) convertView;
var layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WrapContent, weight);
ll.LayoutParameters = layoutParams;
return base.GetBindableView(convertView, source, templateId);
}
}
Then, in your android view:
var adapter = new ChatListAdapter(this, (IMvxAndroidBindingContext)BindingContext);
_chatList = FindViewById<MvxListView>(Resource.Id.chat_list_view);
_chatList.Adapter = adapter;

"Events type" in java

My program contain two classes, one represent the main program and the other one is a gui implemented using swing,
I'm trying to create an "event type", meaning I want my main program to wait until the UserInterface (GUI) will indicate some event, like pressing a button, and I would like to sends some information when my button is pressed.
General Code for the main program (this is the relevant section)
// Open window GUI with the requested BID and wait for confirmation or denial
HumanIFWindow nextWindowGUI = new HumanIFWindow();
nextWindowGUI.setVisible(true);
// ----------------- //
// - Wait on event - //
// ----------------- //
// Here is where I want to wait for the gui Indication
return returnedBid;
Code for the GUI (Again only relevant part)
JButton btnAprove = new JButton("Aprove");
btnAprove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// ----------------- //
// - Trigger event - //
// ----------------- //
// Here is where I want to trigger the event
}});
Preferably I would like to use some library, is there's one that match my needs?
(Maybe BusEvent?)
Edit to specify the question (Thanks Kishan Sarsecha Gajjar)
I want the first class (the general one) to enter a wait statement, I know how to wait using:
while( someBoolean...)
Thread.sleep(...)
and I can change someBoolean with a handle in the GUI class, Like:
FisrtClass.someBoolean == False
But I want something nicer and neater, like a library that Implements the sleep statement. and there's no additional code needed. Is there something like that?
I've looked at Google-BusEvent library but I'm not sure if that's compatible
EDIT, adding JDialog
updated code: Main program:
Bid returnedBid = requestBid;
// Open window GUI with the requested BID and wait for confirmation
DialogHumanConfirmManual nextWindowGUI = new DialogHumanConfirmManual(requestBid);
// Wait on event
if ( (returnedBid = nextWindowGUI.getAnswer()) != null ){
System.out.println("Got Bid " + returnedBid.print());
}
GUI - Dialog:
public DialogHumanConfirmManual(Bid requestedBid){
currentBid = requestedBid;
currentBid.approvedHuman = false;
Dialog mainFrame = new Dialog(new Frame());
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel("Confirmation Dialog"));
yesButton = new JButton("Confirm");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (yesButton == e.getSource()) {
currentBid.approvedHuman = true;
answeredBid = currentBid;
}
}
After opening the Dialog the if ( returnBid ) is called, which result in Null Pointer Exception later on in the code, So How can I delay the main program until the user can Confirm the request??
the other one is a gui implemented using swing,
Use a modal JDialog not a JFrame.
Once the dialog is made visible, the code after the setVisible(true) statement will NOT execute until the dialog is closed.
Read the section from the Swing tutorial on How to Make Dialogs for more information. The tutorial covers the JOptionPane class, but you can just use a JDialog, which is created exactly the same way a JFrame is. You can choose whether to use a JOptionPane or JDialog depending on your exact requirement.