I have a Swing JTable. It is large and users can scroll up and down to see the row of data they want to work on. When the user stops scrolling, I want to know the row number of the JTable that is the first visible row that the user can see. I want to use this to scroll back to this position in the table after the user is through additional operations (I know how to do the scrolling piece - its getting the first visible row that has me stumped).
You first need to find out which part of the table is visible and then map the visual coordinates to the underlying row:
JViewport viewport = scrollPane.getViewport();
Point p = viewport.getViewPosition();
int rowIndex = table.rowAtPoint(p);
In addition to that you might want to experiment with offsets to p (such as, e.g., offsetting it by half a row height etc.) depending on the behavior you want to achieve when the first visible row is only partly visible.
Related
GOAL: Print a table with each row having varying row heights to GUI and to a physical printer using custom renderers.
I am able to print the JTable data to a table within my Java Swing GUI by extending the TableCellRenderer using a customized renderer to change the row height for each row depending on the height needed for each row.
int height = stringValue.length * 22;
if(height > table.getRowHeight(row)) {
table.setRowHeight(row, height);
}
This works for the GUI display and outputs perfectly to my screen.
However, when I want to physically print a paper copy of the entire table, I am unable to correctly print the new row heights using the jtable.print() method. Instead, the data is being cut off because only a single row height is used for all rows. i.e. it ignores my new inputs (from the looks of it).
I have a simple grid. Two columns with a variable amount of rows. I want to make it so i have a header row with an arrow that can collapse and show the whole grid. So when I bring up the app, only the header row is visible with an arrow, and I can click to expand/collapse to show the rest of the grid.
A TreeGrid seems like overkill since I don’t need any hierarchical structure, just the ability to collapse/expand one row. I exclusively use IE and I’ve read that Drawyer doesnt work with IE 8 and above. I return a list of the objects and the object just has two string variables.
Any help with this? I am new to Vaadin 8.
Set grid height (workaround)
As a workaround, you could set the height to be approximately the number of pixels you expect to be the height of the header.
See the Sampler demo. Click the gear icon at top to expose properties of the example Grid object. The last property shown is "Size (W x H)". Change 100% to 100px to see the effect.
Grid height set to 100%
Grid height set to 100px
You can also hide the footer (see checkbox in that property list).
I don't think this can be done with plain Vaadin. But I recommend the following simpler approach:
Initially call grid.setHeightByRows(1.5) (javadoc). This will show exactly one row and a half to indicate more data is available. A scrollbar will appear, too.
Make a new column within the grid that has a button or add a button below the grid that - when clicked - calls setHeightByRows with the number of elements in the grid and hides the button. This will show all rows.
Normally When we add roll over canvas in smartgwt list grid, that canvas automatically added to end of list grid row.
Sample code
https://code.google.com/p/smartgwt/source/browse/trunk/samples/showcase/src/com/smartgwt/sample/showcase/client/grid/appearance/RollOverControlsSample.java?r=1072
Screen Shot
But I want to to add this roll over canvas to Country Column.
Is there any way to do this.
thanks.
In the example code, use ListGrid.setUseCellRollOvers(true) to activate cell based rollover canvas. You can then check if the colNum is for the column of interest (i.e. the Country Column, in your case) and show the rollover only for it.
I have a problem with configurate position of buttons on my scenes layer. I'm trying to build menu, and fill it with buttons, but they are seems to ignore commands like setPosition() etc.
Code looks like that:
Table layerMenuControls = buildMenuControlsLayer();
stage.clear();
Stack stack = new Stack();
stage.addActor(stack);
stack.setSize(GameEngine.VIEWPORT_GUI_WIDTH, GameEngine.VIEWPORT_GUI_HEIGHT);
stack.add(layerBackground);
stack.add(layerMenuControls);
private Table buildMenuControlsLayer() {
Table layer = new Table();
singleplayerButton = new Button(swsSkin, "singlePlayerButton");
singleplayerButton.setPosition((GameEngine.VIEWPORT_GUI_WIDTH / 2) - 64, 64);
layer.add(singleplayerButton);
And there is nothing happening there. Buttons are ignoring these commands and position itselfs one by one horizontally.
Is there something i could forget about?
Tables in LibGDX set the position of their children relatively. So using setPosition won't work. Instead you can make use of Align
Firstly you should set the dimensions and position of the table so it knows where to align things. I assume you want the table to take up the full screen, if not you can change width and height accordingly. The following will do that
layer.setSize(GameEngine.VIEWPORT_GUI_WIDTH, GameEngine.VIEWPORT_GUI_HEIGHT);
layer.setPosition(0,0);
Then you can position buttons etc. relative to this table.
In order to set the button in the centre of the table you can use
layer.align(Align.center);
To position in the top center you can use
layer.align(Align.center|Align.top);
To stop all of your buttons adding in one line, you need to add multiple rows to the table. In between layer.add(singlePlayerButton); and whatever other buttons you are adding, you simply call layer.row();. This positions whatever you add to the table after this on a new row directly below it. In order to make things look a bit nicer you can use padding.
Hope this helps
From the wiki on libgdx's Table:
Table is a WidgetGroup that sets the position and size of its children using a logical table...
So basically, once you added singleplayerButton to layer, its position is set by its position in the Table. setPosition will be ignored in this case. If you want to set the position of the button manually, Table is not your best option. The whole point of Table is to relieve the user from needing to define the children's position and size.
Linked post: How to use MouseListener to find a specific cell in a grid
I am trying to create a Battleships game, with a 10x10 grid made up of 100 cells. The Grid extends JPanel and the Cell also extends JPanel. Earlier on I made the link above to ask how to implement a MouseListener to do this. As the code is all on that page, I am just going to refer to it.
Someone posted an excellent answer on that link which I accepted. I can now use MouseListener to detect movement, clicks etc in any cell on the grid.
However, I've hit another road block. I am trying to place ships on my grid. I am forgetting about orientation for now (as I'm assuming all ships will be placed horizontally). The first ship will take up five cells. I would like to move my cursor onto a cell on the grid and have four other 'tail' cells, to the right of the cell the cursor is over, highlighted as well. If there aren't five free cells in total (perhaps because the cursor is too near the edge of the grid), the cells will turn red. Otherwise, they will turn green.
After the first ship is placed, the second will need placing. It is four cells long. Therefore, when hovering over a cell it will have a 'tail' of three cells and so on.
I am happy with how to change colours, how to handle orientation and how to change from five ships to four ships and so on. However, I have no idea how to select multiple cells at once.
Does anybody know how to implement this? I would love to post what I have tried but the truth is, I've got nothing.
use JButtons / JToggleButtons instead of JPanels
you can to use Icon (one Image splitted to desired numbers)
by usage of implemented methods in API
use ActionListener for (undecorated) JButton
override ButtonModel for implemented mouse events (without / not required to add MouseListner) in JButton
Generally, selecting a row or column of cells is the same as selecting a rectangle of cells.
The mouseClicked method sets the initial cell.
The mouseMoved method sets the current cell that the mouse is in. This is so you can visually indicate to the user what cells have been "selected".
The mouseReleased method sets the final cell.
You check to see that the initial cell to the final cell makes up a row or a column. The length of the row or column determines which ship you place. This allows you to place the ships in any order, not just largest to smallest.
You'll have to add a MouseMoveListener to use the mouseMoved method.