How can frozen columns be implemented in GXT 3.x (from Sencha)? Ext-JS, another product from Sencha seems to implement this, but I can't see where the Java based GXT implement the same thing:
http://dev.sencha.com/deploy/ext-4.0.0/examples/grid/locking-grid.html
The basic idea is that you need two different scrolling containers, one with the fixed column(s), one with the scrolling columns. Each of these need to be in a distinct viewport, so the standard Grid/GridView doesn't work well with this - they make assumptions about how scrolling should behave, so simply subclassing one or both is likely to be fairly involved.
Instead, you can build two grids, one for the locked columns, one for the scrolling ones. Each can deal with their own ColumnConfig classes, to draw headers and rows, and will be linked to the same ListStore to ensure their data is in sync - changes in the store will be passed along to both listening grids.
To get the full effect, some additional wiring will be needed:
Linking scrolling. Listen to the BodyScrollEvent from each Grid, and scroll the other to the same place (changing only top, not left, since you don't want one to control the other).
Sizing is the second big piece - both grids need their scrollable height to be the same, but the horizontal scrolling one needs a buffer on the bottom when it is actually showing that scrollbar. Usually the Grid is told to size based on its parent's instructions, though sometimes you directly size the Grid - in this case, this step is not needed, just size the two grids slightly differently. Otherwise, you'll need to structure the layout to properly configure this.
Finally, the locked column needs its vertical scrollbar to be hidden away - the user has no need to see two vertical scrollbars.
This covers the basic use case, but doesn't deal with things like alternate GridView implementations - GroupingView and subclasses will need to link expanding (and to hide the group headings so they don't appear twice, plus deal with the fact that the group row shouldn't get split when the second half scrolls sideways), TreeGridView and TreeGrid will need to link expanding nodes and hide the tree +/- icons from the second grid.
Here's this basic set of modifications applied to the basic grid example at http://www.sencha.com/examples/#ExamplePlace:basicgrid. To help avoid confusing the issue, I've deleted a number of other features in that grid such as tooltips and changing the selection model:
public class GridExample implements IsWidget, EntryPoint {
private static final StockProperties props = GWT.create(StockProperties.class);
private ContentPanel root;
#Override
public Widget asWidget() {
if (root == null) {
final NumberFormat number = NumberFormat.getFormat("0.00");
ColumnConfig<Stock, String> nameCol = new ColumnConfig<Stock, String>(props.name(), 50, SafeHtmlUtils.fromTrustedString("<b>Company</b>"));
ColumnConfig<Stock, String> symbolCol = new ColumnConfig<Stock, String>(props.symbol(), 100, "Symbol");
ColumnConfig<Stock, Double> lastCol = new ColumnConfig<Stock, Double>(props.last(), 75, "Last");
ColumnConfig<Stock, Double> changeCol = new ColumnConfig<Stock, Double>(props.change(), 100, "Change");
changeCol.setCell(new AbstractCell<Double>() {
#Override
public void render(Context context, Double value, SafeHtmlBuilder sb) {
String style = "style='color: " + (value < 0 ? "red" : "green") + "'";
String v = number.format(value);
sb.appendHtmlConstant("<span " + style + " qtitle='Change' qtip='" + v + "'>" + v + "</span>");
}
});
ColumnConfig<Stock, Date> lastTransCol = new ColumnConfig<Stock, Date>(props.lastTrans(), 100, "Last Updated");
lastTransCol.setCell(new DateCell(DateTimeFormat.getFormat("MM/dd/yyyy")));
List<ColumnConfig<Stock, ?>> l = new ArrayList<ColumnConfig<Stock, ?>>();
//Remove name from main set of columns
// l.add(nameCol);
l.add(symbolCol);
l.add(lastCol);
l.add(changeCol);
l.add(lastTransCol);
//create two column models, one for the locked section
ColumnModel<Stock> lockedCm = new ColumnModel<Stock>(Collections.<ColumnConfig<Stock, ?>>singletonList(nameCol));
ColumnModel<Stock> cm = new ColumnModel<Stock>(l);
ListStore<Stock> store = new ListStore<Stock>(props.key());
store.addAll(TestData.getStocks());
root = new ContentPanel();
root.setHeadingText("Locked Grid Sample");
root.setPixelSize(600, 300);
final Resizable resizable = new Resizable(root, Dir.E, Dir.SE, Dir.S);
root.addExpandHandler(new ExpandHandler() {
#Override
public void onExpand(ExpandEvent event) {
resizable.setEnabled(true);
}
});
root.addCollapseHandler(new CollapseHandler() {
#Override
public void onCollapse(CollapseEvent event) {
resizable.setEnabled(false);
}
});
//locked grid
final Grid<Stock> lockedGrid = new Grid<Stock>(store, lockedCm) {
#Override
protected Size adjustSize(Size size) {
//this is a tricky part - convince the grid to draw just slightly too wide
//and so push the scrollbar out of sight
return new Size(size.getWidth() + XDOM.getScrollBarWidth() - 1, size.getHeight());
}
};
lockedGrid.setView(new GridView<Stock>(){{
this.scrollOffset=0;
}});
//require columns to always fit, preventing scrollbar
lockedGrid.getView().setForceFit(true);
//main grid, with horiz scrollbar
final Grid<Stock> grid = new Grid<Stock>(store, cm);
//don't want this feature, want to encourage horizontal scrollbars
// grid.getView().setAutoExpandColumn(nameCol);
grid.getView().setStripeRows(true);
grid.getView().setColumnLines(true);
grid.setBorders(false);
grid.setColumnReordering(true);
grid.setStateful(true);
grid.setStateId("gridExample");
//link scrolling
lockedGrid.addBodyScrollHandler(new BodyScrollHandler() {
#Override
public void onBodyScroll(BodyScrollEvent event) {
grid.getView().getScroller().scrollTo(ScrollDirection.TOP, event.getScrollTop());
}
});
grid.addBodyScrollHandler(new BodyScrollHandler() {
#Override
public void onBodyScroll(BodyScrollEvent event) {
lockedGrid.getView().getScroller().scrollTo(ScrollDirection.TOP, event.getScrollTop());
}
});
HorizontalLayoutContainer gridWrapper = new HorizontalLayoutContainer();
root.setWidget(gridWrapper);
//add locked column, only 300px wide (in this example, use layouts to change how this works
HorizontalLayoutData lockedColumnLayoutData = new HorizontalLayoutData(300, 1.0);
//this is optional - without this, you get a little offset issue at the very bottom of the non-locked grid
lockedColumnLayoutData.setMargins(new Margins(0, 0, XDOM.getScrollBarWidth(), 0));
gridWrapper.add(lockedGrid, lockedColumnLayoutData);
//add non-locked section, taking up all remaining width
gridWrapper.add(grid, new HorizontalLayoutData(1.0, 1.0));
}
return root;
}
#Override
public void onModuleLoad() {
RootPanel.get().add(asWidget());
}
}
There are a handful of issues (no line between locked and unlocked column, locked column header menu context icon is slightly out of place), but it covers most of the details without much hassle, and leaves almost all of it open to configuration - want the lock at the end? Just move the modifications around - want more than one locked column? just add more to the lockedCm.
That functionality is not implemented in GXT, but there is an user called The_Jackal who made a workaround for this problem in the Sencha Forum - Thread. I haven't tried yet, but I hope it can help you.
GXT - Freeze grid workaround Download
Related
At the moment I am struggling with a JavaFX problem. I will try to explain it as detailed as possible. If you need any further information, just let me know!
What do I want to achieve:
In the picture below you can see what I am trying to achive
I am creating a variable number of Labels (the amount will differ from time to time)
The references to this labels are stored in an ArrayList. The Labels then are placed on the GridPane
Now I want to be able to drag and drop that Labels in the GridPane
I would like to achieve a solution like in the picture below
When you drag a Label it should be visually shown, where I am dragging it to
When passing another Label the visual effect should show, what happens when I drop it here (nr. 3 in the picture). After I drop the Label there, the Labels on the right are switching their position one field to the right. The opened space where I took the source have to be filled with the Labels on the right.
Here is the picture of what I am dreaming right now
At the moment I am able to create the Labels and place them on the GridPane. But when I try to use an inner class for the .onDragListener I am getting the message, that the Label has to be final. But I can't make it final, because it was created during the runtime and the references are stored in an ArrayList.
Okay here is some Code. I hope it helps.
public class Center {
public static GridPane createCenterContent(List<Tour> tourList){
GridPane gridPaneCenter = new GridPane();
final List<List<Label>> customerLabelList = new ArrayList<List<Label>>();
Label customerLabel = new Label();
for(int i = 0 ; i < tourList.size() ; i++){
customerLabelList.add(i, new ArrayList<Label>());
for(int j = 0 ; j < tourList.get(i).getTour().size() ; j++){
customerLabelList.get(i).add(new Label(tourList.get(i).getTour().get(j).getVorname() + " " + tourList.get(i).getTour().get(j).getNachname() + " \n" +
tourList.get(i).getTour().get(j).getStrasse() + " \n" + tourList.get(i).getTour().get(j).getAdresse()));
customerLabelList.get(i).get(j).getStyleClass().add("kdLabelGridLayout");
customerLabelList.get(i).get(j).setMinWidth(200);
customerLabelList.get(i).get(j).setMinHeight(100);
customerLabel = customerLabelList.get(i).get(j);
customerLabel.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
/* drag was detected, start a drag-and-drop gesture*/
/* allow any transfer mode */
Dragboard db = customerLabel.startDragAndDrop(TransferMode.ANY);
System.out.println("Label wird bewegt!");
}
});
customerLabelList.get(i).get(j).setOnDragOver(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
/* data is dragged over the target */
/* accept it only if it is not dragged from the same node
* and if it has a string data */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
System.out.println("Label berührt anderes Label");
}
});
gridPaneCenter.add(customerLabelList.get(i).get(j), i, j, 1, 1);
}
}
gridPaneCenter.setHgap(10);
gridPaneCenter.setVgap(10);
return gridPaneCenter;
}
}
I am getting an error on the Dragboard db = customerLabel.startDragAndDrop(TransferMode.ANY); line. Eclipse tells me that the variable has to be final.
I have a List which contains Lists of Labels. I am placing that Labels in the GridPane and trying to give them all the drag and drop listeners. The amount of labels in the List> differs from time to time.
What can I do to solve the problem? I am very thankful for any hint or link you guys can provide me with.
What I'm trying to do is add a label to follow a slider knob and display the knob's value. I currently have a vertical slider and a label stacked inside a table cell. I'm able to position the label to where the knob is and update it's text correctly but am having a problem while moving the knob.
When I move the knob I can make the label follow it as long as I don't update the text. But if I update the text then the label re-centers itself in the cell till the knob has stopped moving at which point it places itself on the knob.
How do I make it so that the label doesn't reposition while it's updating text?
Here's some sample code of what I have going on:
public class OptionsWindow implements Screen {
private Stage stage;
private Table table;
private Slider slider;
private Label label;
private Skin textSkin = new Skin(Gdx.files.internal("skins/skin.json"),
new TextureAtlas(Gdx.files.internal("skins/text.pack")));
private Skin sliderSkin = new Skin(Gdx.files.internal("skins/skin.json"),
new TextureAtlas(Gdx.files.internal("skins/text.pack")));
private min = 1, max = 2;
#Override
public void show() {
stage = new Stage();
table = new Table();
slider = new Slider(min, max, 0.001f, true, sliderSkin);
label = new Label(""), textSkin);
table.stack(label, slider).height(1000);
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
label.setY(slider.getValue() - min);
label.setText(slider.getValue());
stage.act();
stage.draw();
}
}
I've done a lot of searching for an answer to this question but have yielded no results. I have also tried placing the label in a container and moving the container while changing the label text, but the same result happens. I'm sure somebody had done this before. It seems like something people would want. Please, any help is welcome.
Never done it before, I always show the amount in a label next to or above the slider to give feedback. But the result you are after gives a nice touch. Did you try adding a ChangeListener to your slider? Everytime the slider changes this would be called. So if you insert the new position and text in there it should be updating correctly.
//Slider has to be "final" for the listener.
final Slider slider = new Slider(0, 10, 1, true, skin);
slider.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
label.setY(slider.getValue() - min);
label.setText(slider.getValue());
}
});
This does not need to be in your render method. It's a listener so whenever something happens the listener listens for it will execute.
Ok, so I came up with a work around. It would seem that labels move back to their cell alignment position while updating text. to solve this I simply placed my label in a table with a row on either side like so:
Table subTable = new Table();
subTable.add().row();
subTable.add(label).row();
subTable.add();
Then in render I calculate new height values for the exterior cells and apply them:
float topH, botH;
// Calculate heights
topH = (1 - slider.getPercent()) * parentTable.getHeight();
botH = slider.getPercent() * parentTable.getHeight();
// Apply heights to exterior table cells
subTable.getCells().get(0).height(topH);
subTable.getCells().get(2).height(botH);
// Update label text
label.setText(slider.getValue());
This is just a simple form of what I have. You'll want to take into account the label height to get it to position correctly. Also if you want it to follow with the slider at the slider's set animateDuration, you'll want to use getVisualPercent() instead of getPercent(). However this causes issues when your finger flies out of the cell. For some reason the visual percent is linked to where the slider knob is when your finger leaves the cell, not where it ends up.
My work around for this was to just set the animateDuration to 0.0f and just use getPercent(). If I or someone comes up with a solution to the getVisualPercent() option, then that would be nice to have posted as well.
I have seen this thread which asked the exact same question I have now, but find the answers a bit unsatisfactory:
Android's LinearLayout for Swing
I created a class WeightedPanel like so:
public class WeightedPanel extends javax.swing.JPanel {
private static final long serialVersionUID = 6844740568601141924L;
private boolean mVertical;
private double mLastWeight = 1;
private GridBagConstraints mConstraints;
private int mLastGrid = 0;
public WeightedPanel(boolean vertical) {
mVertical = vertical;
mConstraints = new GridBagConstraints();
setLayout(new GridBagLayout());
}
#Override
public Component add(Component comp) {
return add(comp, mLastWeight);
}
public Component add(Component comp, double weight) {
if (mVertical) {
mConstraints.weighty = weight;
mConstraints.weightx = 1;
mConstraints.gridy = mLastGrid;
mConstraints.gridx = 0;
} else {
mConstraints.weightx = weight;
mConstraints.weighty = 1;
mConstraints.gridx = mLastGrid;
mConstraints.gridy = 0;
}
mConstraints.fill = GridBagConstraints.BOTH;
add(comp, mConstraints);
mLastWeight = weight;
mLastGrid += weight;
return comp;
}
public Component add(Component comp, int weight) {
return add(comp, (double) weight);
}
}
This kind of works, but I have two problems with it:
1) In my application, I have a login screen:
#Override
protected void addComponents(WeightedPanel jPanel) {
mUpdateListener = new UpdateListener() {
#Override
public void onUpdate() {
LoginFrame.this.onUpdate();
}
};
WeightedPanel panel = getUserPanel();
jPanel.add(panel);
panel = getPasswordPanel();
jPanel.add(panel);
mLoginButton = getLoginButton();
jPanel.add(mLoginButton);
}
private WeightedPanel getPasswordPanel() {
WeightedPanel result = new WeightedPanel(false);
JLabel label = new JLabel("Password");
result.add(label);
mPasswordField = new PasswordField(mUpdateListener);
result.add(mPasswordField);
return result;
}
private WeightedPanel getUserPanel() {
WeightedPanel result = new WeightedPanel(false);
JLabel label = new JLabel("User");
result.add(label);
mUserTextField = new TextField(mUpdateListener);
result.add(mUserTextField);
return result;
}
which in practice looks like this:
Click to view
Why aren't the labels and text fields all the same size here? I figure it's got something to do with the fact that "Password" is a longer string than "User", but that's obviously not what I want!
2) My second problem is this. I have another screen like so:
#Override
protected void addComponents(WeightedPanel jPanel) {
WeightedPanel scrollPanePanel = getOrdersScrollPane();
jPanel.add(scrollPanePanel);
WeightedPanel buttonPanel = getButtonPanel();
jPanel.add(buttonPanel);
}
private WeightedPanel getOrdersScrollPane() {
WeightedPanel result = new WeightedPanel(true);
JPanel filterPanel = getFilterPanel();
result.add(filterPanel, 1);
mTableModel = new OrdersTableModel();
mTable = new JTable(mTableModel);
mTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent arg0) {
checkEnabled();
}
});
JScrollPane scrollPane = new JScrollPane(mTable);
result.add(scrollPane, 40);
return result;
}
It really doesn't look bad in practice:
Click to view
But have a look at the getOrdersScrollPane() function. The call to functions result.add(filterPanel, 1); and result.add(scrollPane, 50); say that the proportion between the filter panel and the scroll pane should be 1:50, but looking at the scroll pane, it's definitely not 50 times the size of the filter panel. Obviously, I am exaggerating to make my point, I don't really want a proportion of 1:50; it just strikes me that it makes no difference whether I do result.add(scrollPane, 10); or result.add(scrollPane, 50);
Both questions stem from an incorrect understanding of GridBagLayout. A bit more reading and experimenting should help) To answer the question at hand:
1) The problem here is that you want a single GridBagLayout, but instead are adding 2 independent panels.
The result: The columns in the top grid bag are independent of the columns in the bottom grid bag.
To rectify this, there are 2 things you can try:
Add both labels and both text fields to a single GridBag panel. That way the columns will align.
Make a minimum and preferred size for the labels so that their width matches and set their weightx to 0 (and weightx of text fields non-zero). That way you are making the GridBags allocate the same amount of space for the labels and text fields.
The first method is preferred, but not always possible. The second method is hacky and will likely break as soon as you change the label string, a user set a different default font etc, etc.
2) Here you are misunderstanding what weighty does.
It does not make your components of the specified proportion. That should be clear enough since you can mix 0 and non-0 weight components in a single layout.
What it does, is it allocates the preferred (or minimum) sizes for components, and distributes the remaining space in that proportion. Which means if you make your panel 100 pixels higher by resizing the window, 2 will go to the top panel adding spacing, and 98 will go to the table.
What you likely wanted is to make the weighty of the top filter 0 (so that there is no awkward spacings in large windows) and control its actual height with setPreferred and setMinimum size (or by setting those on the embedded components).
EDIT
As docs for Linear Layout state, to achieve a fixed proportion of sizes of components (the initial problem), one has to set their preferred sizes to 0, and then set weights (then all space is remaining space, and is distributed according to weights only). This also works for the GridBag variant.
I have some experience with Android development and now I have decided to learn something new - developing for Kindle (NOT Android based Kindle Fire).
Amazon offers KDK + Personal Basis Profile 1.1.2 (JSR 217) as a platform.
My problem is, how to design UI. I do not have any experience with awt (just did som apps in java swing), but is doesn't seem to be a big issue, because it's quite simple... Main issue is with 'replacing' android's ListView.
I've tried to use component named KPages. I'm, however, not able to put anything else but KLabel into the pages model...
PageProvider pp = PageProviders.createKBoxLayoutProvider(KBoxLayout.PAGE_AXIS);
final KPages pages = new KPages(pp);
for (int i = 0; i < 40; i++) {
final KPanel listItem = new KPanel();
// listItem.add(new KLabelMultiline("label numero " + i + " is not very good, because it will be displayed over more than one line and 'pages' won't be able to deal with it"));
listItem.add(new KLabel("label numero " + i + " is very good, because it won't be displayed over more than one line and 'pages' will be able to deal with it"));
listItem.add(new KButton("read"));
listItem.add(new KButton("edit"));
pages.addItem(listItem);
}
context.getRootContainer().add(pages);
Sample code above shows my effort to list some items. KPages works well only when adding KLabel using pages.addItem(). Just inserting KLabelMultiline causes paging to mulfunction (instead of showing labels 0-12 it 'displays' 0-26 as in single line, but screen of kindle shows only 14-26). Trying to insert whole KPanel with label and 2 buttons results in listing the labels followed by one empty line for each item, without any button... displaying same KPanel outside pages works fine - I can see the label and both buttons...
I'm almost sure it's my fault for leaving something out, but thanks to really little information available on KDK, I'm not able to find it... Can anybody give me a hand? Thanks
Try overriding the getPreferredSize, getMinimumSize, and getMaximumSize calls to your constructor for KPanel. Set width and height to whatever your screen size is.
final KPanel listItem = new KPanel() {
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public Dimension getMinimumSize() {
return new Dimension(width, height);
}
public Dimension getMaximumSize() {
return new Dimension(width, height);
}
};
Everything is in the title.
In my application, depending on a selection made by the user I fill up a combobox with a list which can sometime be small (1 element) sometimes be large (150 elements).
What I would like is not to have a fixed height set at startup to a given value but to set the maximumRowCount to the height of my JFrame or to the height of my screen and I don't know how to determine the number of rows that would match my application height or my screen height. This should be dynamical (at runtime) so when I change the combobox font size the maximumRowCount also adapts itself.
Can anyone help me?
So you need to figure out how big each row in the drop-down list is. To do this, the easy way is to create a cell renderer, populate it, and ask it for its preferred height.
final DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
dlcr.setText("one of my combo items");
int numRows = (int)( (1.0f * frame.getHeight()) / dlcr.getPreferredSize().height );
setMaximumRowCount(numRows);
Note that I used frame.getHeight() here, but at least in some look-and-feels, the drop-down list starts below the combo box, so you'd have to make some adjustment for that. And there are ways to get the size of a screen, too, such as those in http://docs.oracle.com/javase/6/docs/api/java/awt/Toolkit.html.
Below is a code snippet which sets the rowCount dynamically
The basic steps
get the pref size of the rendering component
get the location of the combo relative to the context you want (the example takes the screen) and the available space below
calculate the number of rows which fit into the available space
do all this in a PopupMenuListener to be really dynamical
The code (which is obviously not production quality, just to give you something to play with :-)
final JComboBox box = new JComboBox(new Object[] {1, 2, 3, 4, 5, 6, 6, 34,3,3});
PopupMenuListener l = new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
int pref = getRowHeight(box);
int available = getAvailableScreenHeightBelow(box);
int count = available / pref;
box.setMaximumRowCount(count);
}
private int getRowHeight(final JComboBox box) {
// note: here we assume the rendering comp's pref is the same for all rows
ComboPopup popup = (ComboPopup) box.getAccessibleContext().getAccessibleChild(0);
ListCellRenderer renderer = box.getRenderer();
Component comp = renderer.getListCellRendererComponent(popup.getList(), 1, 0, false, false);
int pref = comp.getPreferredSize().height;
return pref;
}
private int getAvailableScreenHeightBelow(final JComboBox box) {
// note: this is crude - f.i. doesn't take taskbar into account
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Point location = box.getLocationOnScreen();
location.y += box.getHeight();
int available = screen.height - location.y;
return available;
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
#Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
};
box.addPopupMenuListener(l);