[SOLVED] SelectBox does not appear

Anything libgdx related goes here!

[SOLVED] SelectBox does not appear

Postby SuchuuDesu » Thu May 16, 2019 4:20 pm

FinalEdit:

I figured it out.
In the end, I was able to get it all to work with the Stage, so it looks a little neater and is what I was hoping for in the first place!

Code:

Code: Select all
public class MyGdxGame implements ApplicationListener
{
   private Stage stage;

   @Override
   public void create()
   {
      String[] monthsS = new String[12];
      monthsS = setMonths();
      BitmapFont font = new BitmapFont();
      font.setColor(Color.BLACK);
      
      List.ListStyle lstStyle = new List.ListStyle();
      lstStyle.background = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("button_simple01.png")), 10, 10, 10, 10));
      lstStyle.selection = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("selection.png"))));
      lstStyle.font = font;
      lstStyle.fontColorSelected = Color.BLUE;
      lstStyle.fontColorUnselected = Color.BLACK;
      
      SelectBox.SelectBoxStyle sbStyle = new SelectBox.SelectBoxStyle();
      sbStyle.listStyle = lstStyle;
      sbStyle.scrollStyle = new ScrollPane.ScrollPaneStyle();
      sbStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button_simple01.png"))));
      sbStyle.backgroundOpen = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button_simple01.png"))));
      sbStyle.font = font;
      sbStyle.fontColor = Color.BLACK;
      
      SelectBox<String> monthsSB = new SelectBox<String>(sbStyle);
      monthsSB.setItems(monthsS);
      
      
      Table table = new Table();
      table.setFillParent(true);
      table.center();
      table.add(monthsSB).width(64);
      
      stage = new Stage(new StretchViewport(400, 800)); // Mobile devices
      Gdx.input.setInputProcessor(stage);
      stage.addActor(table);
   }

   @Override
   public void render()
   {       
       Gdx.gl.glClearColor(1, 1, 1, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
      stage.act();
      stage.draw();
   }
   
   private String[] setMonths() {
      ...
   }
}


Edit:

Updated my code with ShatterBlast's changes.
And I got something onscreen. But it's not right.
https://i1161.photobucket.com/albums/q5 ... ectBox.jpg

As for the way it starts from the middle and doesn't fill the screen, I know that's because the camera's middle point is 0,0 (something like that) and I'm sure I can sort that out on my own eventually - but why does the actor itself look so weird?
Is it the bitmap I made?

I created a 32x16 bitmap for buttons and reused it here to save time. Photobucket shows up weird on my phone but hopefully it looks better on PC
https://i1161.photobucket.com/albums/q5 ... mple01.png

My code currently looks like this:

Code: Select all
public class MyGdxGame implements ApplicationListener
{
   private SpriteBatch batch;
   private Table table;
   private OrthographicCamera camera;
   
   private Stage stage;

   @Override
   public void create()
   {
      String[] monthsS = new String[12];
      monthsS = setMonths();
      BitmapFont font = new BitmapFont();
      font.setColor(Color.BLACK);
      
      List.ListStyle lstStyle = new List.ListStyle();
      lstStyle.background = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("button_simple01.png")), 10, 10, 10, 10));
      lstStyle.selection = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("selection.png"))));
      lstStyle.font = font;
      lstStyle.fontColorSelected = Color.BLUE;
      lstStyle.fontColorUnselected = Color.BLACK;
      
      SelectBox.SelectBoxStyle sbStyle = new SelectBox.SelectBoxStyle();
      sbStyle.listStyle = lstStyle;
      sbStyle.scrollStyle = new ScrollPane.ScrollPaneStyle();
      sbStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button_simple01.png"))));
      sbStyle.backgroundOpen = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("button_simple01.png"))));
      sbStyle.font = font;
      sbStyle.fontColor = Color.BLACK;
      
      SelectBox<String> monthsSB = new SelectBox<String>(sbStyle);
      monthsSB.setItems(monthsS);
      
      table = new Table();
      table.add(monthsSB);
      
      batch = new SpriteBatch();
      camera = new OrthographicCamera();
      
      stage = new Stage();
      Gdx.input.setInputProcessor(stage);
      stage.addActor(table);
   }

   @Override
   public void render()
   {       
       Gdx.gl.glClearColor(1, 1, 1, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      batch.setBlendFunction(770, 771);
      
      table.draw(batch, 1f);
      
      batch.end();
      
      stage.act();
      stage.draw();
   }
   
   private String[] setMonths() {
      return new String[] {"Jan", "Feb", "Mar", "Apr",
                     "May", "June", "Jul", "Aug",
                     "Sep", "Oct", "Nov", "Dec"};
   }
}


Original Post:

Hello, everyone.
I am learning to use LibGDX. I have a couple years Java exp under my belt - but it's been a while.
I'm trying now to get my head around SelectBoxes.
I've looked online for some basic explainations of how to set them up - but I'm struggling to understand.
I started a simple project to just start by getting one to display onscreen. Although there are no compile errors and the app does run without crashing, the SelectBox isn't showing up onscreen.
My level of 'noobness' is so high, I'm not even slightly sure what the problem is.
I bet it's not even that hard to figure out! x_x

MyGDXGame.java

Code: Select all
 
public class MyGdxGame implements ApplicationListener
{
   private Stage stage;

   @Override
   public void create()
   {
      String[] monthsS = new String[12];
      monthsS = setMonths();
      BitmapFont font = new BitmapFont();
      font.setColor(Color.BLACK);
      
      List.ListStyle lstStyle = new List.ListStyle();
      lstStyle.background = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("textfieldBGSmall.png")), 10, 10, 10, 10));
      lstStyle.selection = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("selection.png"))));
      lstStyle.font = font;
      lstStyle.fontColorSelected = Color.BLUE;
      lstStyle.fontColorUnselected = Color.BLACK;
      
      SelectBox.SelectBoxStyle sbStyle = new SelectBox.SelectBoxStyle();
      sbStyle.listStyle = lstStyle;
      sbStyle.scrollStyle = new ScrollPane.ScrollPaneStyle();
      sbStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("textfieldBGSmall.png"))));
      sbStyle.backgroundOpen = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("textfieldBGSmall.png"))));
      sbStyle.font = font;
      sbStyle.fontColor = Color.BLACK;
      
      SelectBox<String> monthsSB = new SelectBox<String>(sbStyle);
      monthsSB.setItems(monthsS);
      
      Table table = new Table();
      table.add(monthsSB);
      
      stage = new Stage();
      Gdx.input.setInputProcessor(stage);
      stage.addActor(table);
   }

   @Override
   public void render()
   {       
       Gdx.gl.glClearColor(1, 1, 1, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
      stage.act();
      stage.draw();
   }

   private String[] setMonths() {
      return new String[] {"Jan", "Feb", "Mar", "Apr",
                     "May", "June", "Jul", "Aug",
                     "Sep", "Oct", "Nov", "Dec"};
   }
}
Last edited by SuchuuDesu on Thu May 23, 2019 7:51 am, edited 12 times in total.
SuchuuDesu
 
Posts: 6
Joined: Thu May 16, 2019 3:20 pm

Re: SelectBox does not appear

Postby shatterblast » Thu May 16, 2019 5:26 pm

Basically, you need to add a Camera, a SpriteBatch, and your Table to that same SpriteBatch in your create() and render() methods. I do not have your images to test whether the below modified code correctly compiles, but I think it works. I commonly prefer making my variables as instance variables. I declare them outside any method, but I only instantiate them within a method. In that way, I can share them between whatever methods. Sure, it creates a mess of code somewhat, but with that idea, you can instantiate your Table object in your create() method and then attach it to the SpriteBatch in your render() method. Of course, not EVERY variable requires that particular setup, but I hope the concept creatively benefits you. I suggest looking below at my version of your given code:

Code: Select all
public class MyGdxGame extends ApplicationAdapter {
   
   SpriteBatch batch;
   Texture img;

   private Stage stage;


   //Modifications.
   //=============================
   Table table;
   SpriteBatch spriteBatch;
   OrthographicCamera orthoCamera;
   //=============================

   
   @Override
   public void create () {

      String[] monthsS = new String[12];
      monthsS = setMonths();
      BitmapFont font = new BitmapFont();
      font.setColor(Color.BLACK);

      List.ListStyle lstStyle = new List.ListStyle();
      lstStyle.background = new NinePatchDrawable(new NinePatch(new Texture(Gdx.files.internal("textfieldBGSmall.png")), 10, 10, 10, 10));
      lstStyle.selection = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("selection.png"))));
      lstStyle.font = font;
      lstStyle.fontColorSelected = Color.BLUE;
      lstStyle.fontColorUnselected = Color.BLACK;

      SelectBox.SelectBoxStyle sbStyle = new SelectBox.SelectBoxStyle();
      sbStyle.listStyle = lstStyle;
      sbStyle.scrollStyle = new ScrollPane.ScrollPaneStyle();
      sbStyle.background = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("textfieldBGSmall.png"))));
      sbStyle.backgroundOpen = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("textfieldBGSmall.png"))));
      sbStyle.font = font;
      sbStyle.fontColor = Color.BLACK;

      SelectBox<String> monthsSB = new SelectBox<String>(sbStyle);
      monthsSB.setItems(monthsS);


      //Modifications.
      //=====================================
      table = new Table();
      table.add(monthsSB);

      spriteBatch = new SpriteBatch();
      orthoCamera = new OrthographicCamera();
      //=====================================


      stage = new Stage();
      Gdx.input.setInputProcessor(stage);
      stage.addActor(table);

   }

   @Override
   public void render () {


      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


      //Modifications.
      //======================================================
      spriteBatch.setProjectionMatrix( orthoCamera.combined );
      spriteBatch.begin();
      spriteBatch.setBlendFunction(770, 771 );

      table.draw( spriteBatch, 1f );

      spriteBatch.end();
      //======================================================


      stage.act();
      stage.draw();

   }


   @Override
   public void dispose () {

      stage.dispose();

   }


   private String[] setMonths() {
      return new String[] {"Jan", "Feb", "Mar", "Apr",
            "May", "June", "Jul", "Aug",
            "Sep", "Oct", "Nov", "Dec"};
   }

}


You might consider a Viewport for later when you get far enough.
shatterblast
 
Posts: 289
Joined: Sun Jul 06, 2014 1:14 pm

Re: SelectBox does not appear

Postby evilentity » Fri May 17, 2019 9:43 am

You should probably add the table to the stage instead.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4767
Joined: Wed Aug 24, 2011 11:37 am

Re: SelectBox does not appear

Postby SuchuuDesu » Sat May 18, 2019 2:25 pm

Okay ShatterBlast, thanks for your advice.
The camera is something I've had a little look into but have yet to fully comprehend it. It's on my list of things to learn.
Thank you for the code help. I tried it and I'll update main post with results.
SuchuuDesu
 
Posts: 6
Joined: Thu May 16, 2019 3:20 pm

Re: SelectBox does not appear

Postby SuchuuDesu » Sat May 18, 2019 2:58 pm

Oh, speaking of Viewports, doesn't the Stage maintain its own one anyway?
SuchuuDesu
 
Posts: 6
Joined: Thu May 16, 2019 3:20 pm

Re: SelectBox does not appear

Postby shatterblast » Sat May 18, 2019 10:34 pm

There is a default if you do not list one, but it helps to consider a fancier one with different behavior when facing the decision to either re-size your graphics or stretch them, like on tablets versus phones for example.
shatterblast
 
Posts: 289
Joined: Sun Jul 06, 2014 1:14 pm


Return to Libgdx

Who is online

Users browsing this forum: WowHow and 1 guest