SWT Label Drag'n'Drop - java

I'm surprised to see that this hasn't been done, or at least my research says so.
I have a Canvas with RowLayout and a bunch of Labels.
The title is pretty straight forward: I want to reorder my labels using DND.
Please don't tell me I have to engineer my own algorithm for calculating bounds and sizes and stuff like that.
Later edit:
I'm considering using Zest, but again, I can't find any example where graph nodes are snapped to eachother.

I did something like this about a year ago.
My method of solving this problem was to use a data model to hold the label information. Use the canvas.getChildren() and search for a separator composite between each object or the label that you dropped on top of. When a drag and drop operation was completed you would search for the item that you dropped on and move the reference to the appropriate position in the model. Then reset the information on each label. Only requirement to do this is to keep a data structure with the label info and a reference to the canvas.

Related

Why can't GridLayout allow components to be changed by row and column?

This is perhaps more of a whine than a question, and I'm aware there's a workaround to the problem described in this post. But I have a situation where I have to add components to a GridLayout, possibly enlarging the grid as I go; but the components don't get created and inserted in a nice neat order; and I don't know when I'm done being handed components to insert into the grid. In other words, in a 3x3 grid I might get handed a component to put at (0,2) and then another at (1,0) and then one at (0,0). And then I might get one at (5,2) and need to enlarge the grid. And then I might get told to replace the line at (0,0) with something else.
I understand that there's no way to say "put a component at x, y". I get that I'm going to have to build a 2D array to hold my components and then empty and refill the grid from the array, each time it changes, which is going to be quite often in several large grids, so I'd rather not. I get that life is like that and the language is the language and who am I to question why.
But I'm really curious. This seems like the most basic of operations for a grid to support. Not having it feels like I'm working with a spreadsheet that only lets you enter values in order from left to right - it's simple madness.
I haven't taken apart the source code for GridLayout, but any naive understanding of how it "must work" makes me think add(component, row, col) should be trivial to implement. It must not be, because GridLayout isn't exactly new and I can't believe I'm the first to think random access to a grid is a good idea. So it must be really hard. But why?
I realize understanding why the internals of GridLayout can't support this, doesn't solve any real problems, except the problem of me unable to stop thinking about how fundamentally weird the restriction is. In the end I'm going to end up with a parallel array, or messing with the more complex GridBagLayout, unless someone knows a better way. But I just want to know why I have to.
Why not approach the problem from a different angle:
Create your grid using GridLayout
Fill the grid with empty JPanels
JPanels that each uses a BorderLayout
Keep these JPanels where they are
But swap the JPanels that they display within them in each of their BorderLayout.CENTER positions
Alternatively, you could have them use a CardLayout, but the effect would be the same

Best way to move and draw elements on screen

i have a so so general question.
I need to make a screen that deals with dragging and drawing of objects. I will need to have like a predefined rectangle on top of the screen that represents some kind of a sitting table. below that i need to have predefined empty space (like a floor plan) on which will user put those tables into. Those tables need to contain and carry some data with it so i can send it to my server when the drag and drop is finished. Now i am new with drawing things on android and any kind of help or suggestion would be much appreciated! Thanks :D
If it's simple, stick with a custom View that represents your entire world. Keep track of the Table and other spaces as data structures, then draw them in the onDraw. This is similar to how you'd code up a simple game. Here's an article that goes into more detail: http://cjds.github.io/2014/04/28/Creating-a-simple-android-game/

Java Swing. Add custom items to scroll pane (Tile selector)

I am currently working on a TiledMap editor for a game I am working on. I am now at the part where I need to implement a tile selector. I am pretty sure I would be able to do this in canvas and draw each tile individually and draw a box around the selected tile but I want to know if there is an easier, more professional looking way already implemented in java swing before I start making my own.
Here is a basic drawing of what I want:
Green boxes in tile selector are individual tiles and the one with bold blue it the selected one (Just a basic example, colors don't matter to me).
Additional Info: Each tile has it's own object, so if the solution involves an ArrayList or something, it would work very well. Also I would like to be able to manipulate which objects are shown in the selector or not. I have a search bar that I want to use to narrow down the tiles shown.
Thanks in advance, if you need more details, please ask.
A JList can display a collection of tile images just fine and would be the likely candidate for the tile selector component on the left.
Ok looks pretty good. How do you tell the JList what you want to be displayed about each item in the list. For my case I want to JList to display a simple image, not text, is that possible?
If you add Icons to the JList, they will be automatically displayed correctly. If you need to fine tune the display, then you will want to write a ListCellRenderer as per the tutorials and API.
Also, can it do lists with multiple columns?
If you mean multiple columns of the same thing, such as a 4x4 grid of images, then yes a JList can handle this great, and you would call setLayoutOrientation(JList.HORIZONTAL_WRAP) (or vertical wrap if desired). If you mean columns which each hold a different data type such as an image, text, a checkbox, then go with a JTable.

Grid building for Connect Four gui

I have been able to create a grid using an image file (serves as the empty circles), a loop, and GridLayout, but I am well aware that there's more functionality needed (like for dropping the token, though no animation is necessary yet) so I scrapped it and now I'm back to an empty grid. I am stuck and I'm not really sure how I can accomplish this. My code is a mess at the moment so I'm not sure if it'd even make sense for me to post it.
My main problem is how to build a grid, which will then just be filled with a solid color (I'm cancelling using an image file, it seems a little more complicated as far as I'm concerned) with empty circles, that I will be able to fill up with an image file of a token once the player clicks on a button that corresponds to the column he chose (and then reset everything after the game is over). In other words, a rectangle of solid color and with empty circles to be filled up by tokens, but not with solid color, but an image file.
I have been trying to familiarize myself with paint() but I only started learning GUI last week so there are still likely some more things I'll have to learn to probably understand it in a considerable degree.
I am running out of options tantamount to my knowledge of GUI (Swing in particular) and I have been trying to work on this for a week now.
Any hints?
There are multiple possible ways to solve this, but one easy one is to give a JPanel a GridLayout, and then fill it with JLabels with ImageIcons that show empty circles. When the column is selected, the appropriate JLabel is given a new ImageIcon via setIcon that shows a color filled circle.
Also,
Always strive to separate your program logic code from your GUI code, since the better your separation, the easier will be your ability to debug and enhance.
Work on small problems one at a time. Don't move on to the next problem until the current small step is solved.
Work out your logic and ideas on paper first before committing it to code.
Don't "work with paint". If you need to do Swing graphics, you'll want to override a JPanel or JComponent's paintComponent method. The paint method also concerns itself with drawing borders and children, and so overriding it can have nasty and unexpected side effects on these. Also paint is not double buffered by default, and this can lead to bad animation once you start working with animation.
Edit
You state in comment:
Will it be okay to use JButton though? Because that was what i used during my first attempt. I can use setIcon with it too right?
Do you mean use a JButton instead of a JLabel? That would work, and yes you can call setIcon on JButtons, but would make all your rectangles look like buttons. So if that's OK, then do it. Otherwise, you could still use JLabels, and then create a row grid of JButtons to put below or above your game grid, and then have the user press those buttons, and in their ActionListeners have them change the icons of a JLabel in the selected column.
But having said this, I mainly recommend that you use what works best for you. The learning will be in the creating, no matter what you create.
Edit 2
You ask:
do you think it'll be possible/a nice approach to store jlabels in an array and then lay them out in a panel?
Absolutely, either an array of JLabel[] or a List<JLabel> I think is not only possible but in fact essential for this to work well. I think that you're definitely on the right track here.

Java Swing Gridlayout: Accessing Specific Coordinate

I'm creating a Java swing GUI and I have formatted a JPanel to use a GridLayout. I need to access a specific "box" (i.e. specific coordinate) of the grid, but I cannot see a way to do so.
How can I do this?
You shouldn't depend on GUI code (the View) to give you information about program data (the model). The best solution would be to "know" which component is where from the start--maybe you should have a data structure (2D array?) that holds the components and is updated whenever something's added to the grid.
If you want a quick and very-dirty fix, though, you could start playing games with JPanel.getComponentAt(). This requires pixel coordinates, though, so you'd need to do some reverse-engineering to figure out how much space a given grid square takes up. The space between grid squares is given by your GridLayout object. This is not recommended whatsoever though. I'm just including it in the interest of completeness (and since it's a more literal response to your question).
In GridLayout, "The container is divided into equal-sized rectangles." You can add an empty, transparent component in places you want to appear empty, e.g. new JLabel(""). See also GridBagLayout and Using Layout Managers.

Categories

Resources