GWT java CELLTABLE MouseHover Buttoncell - java

So i have here a Cellbutton :
Column<dateiles, String> column_2 = new Column<dateiles, String>(new ButtonCell()) {
#Override
public String getValue(dateiles object) {
int s = object.comments.size();
String ss = String.valueOf(s);
return ss;
}
};
cellTable.addColumn(column_2, "Comments");
cellTable.setColumnWidth(column_2, "100px");
i want to add to my buttons for each cell column a Tooltip but how is it possible

this.addCellPreviewHandler(new Handler<Tabletype>() {
#Override
public void onCellPreview(final CellPreviewEvent<Tabletype> event) {
int columnID = event.getColumn();
}
});
}
Just replace tabletype with the object you definded when instantiating the table ( The T of CellTable<T> ) Then with the columnID you can detect the right column.

Related

A JComboBox in a specific cell in JTable

I would like to know how to set up a JComboBox in a particular cell in a JTable.
I have seen people using TableColumn setCellEditor(new DefaultCellEditor(comboBox)).
But this is for an entire column, I would like a specific cell.
So maybe I should do a custom TableCellEditor that would fit my needs, but I am a little lost on how to do it...
The goal of this is to manage filters on parameters. There are two kinds of filters:
The one that compares two values, for instance: number of balloons > 5
The one that will say is a value is inside a range of value, for instance: parameter name is inside {"one", "two", "three", "seven"}.
screenshot of my JTable:
As we can see in the picture, when there is the "comparator" "is among", we would need a JComboBox in cell[0][2] to choose the values of the range within a complete set of fields.
While cell[1][2] does not need a JComboBox, but just an editable cell.
I hope I have been clear and thank you for your help.
EDIT:
I was able to display a JComboBox only to realize, I couldn't select multiple values on it. So now I am trying to display a JList instead of a ComboBox.
But when I click on the cell, the JList is not displayed, I don't know why.
Here is my code:
JTable tableParametersFilter = new JTable(modelParametersFilter){
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
int modelRow = convertRowIndexToModel( row );
Parameter_Filter pf = view.listParameter_Filter.get(modelRow);
if(modelColumn == 2 && pf instanceof Parameter_Filter_To_List_Of_Fields) {
Parameter_Filter_To_List_Of_Fields pftlof = (Parameter_Filter_To_List_Of_Fields)pf;
JList<String> list = new JList<String>(pftlof.list_of_fields_total_names);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(-1);
return new TableCellEditor() {
#Override
public boolean stopCellEditing() {
return false;
}
#Override
public boolean shouldSelectCell(EventObject anEvent) {
return false;
}
#Override
public void removeCellEditorListener(CellEditorListener l) {
}
#Override
public boolean isCellEditable(EventObject anEvent) {
return true;
}
#Override
public Object getCellEditorValue() {
return list.getSelectedValuesList().toString();
}
#Override
public void cancelCellEditing() {
}
#Override
public void addCellEditorListener(CellEditorListener l) {
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return list;
}
};
}
return super.getCellEditor(row, column);
}
};
Any suggestions?
I have solved my problem.
I have not been able to add multiple choice JComboBox, or a displayable JList on the Cell of the Jtable.
Instead, I have used a JOptionPane that displayed a JList.
Here's the code:
tableParametersFilter.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
if(column == 2){
Parameter_Filter pf = view.listParameter_Filter.get(row);
if(pf instanceof Parameter_Filter_To_List_Of_Fields) {
Parameter_Filter_To_List_Of_Fields pftlof = (Parameter_Filter_To_List_Of_Fields) pf;
JList<String> jlist = new JList<String>(pftlof.list_of_fields_total_names);
String StringOfIntArray = (String) tableParametersFilter.getValueAt( row, 2);
int[] list_parameter_id = Statique.StringOfIntArrayToIntegerArray(StringOfIntArray);
if(list_parameter_id.length < jlist.getModel().getSize()) {
int[] list_places = pftlof.getPlaceOfParameters(list_parameter_id);
for(int i = 0; i < list_places.length; i++) {
jlist.setSelectedIndices(list_places);
}
}
JScrollPane scrollPane = new JScrollPane(jlist);
scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
JOptionPane.showMessageDialog(
null, scrollPane, "Multi-Select Example", JOptionPane.PLAIN_MESSAGE);
int[] SelectedIndices = jlist.getSelectedIndices();
Integer[] listParametersId = new Integer[SelectedIndices.length];
for(int i = 0; i < SelectedIndices.length; i++) {
int id = pftlof.list_of_fields_Total[SelectedIndices[i]].id;
try {
Parameter p = Parameter.getParameter(
id,
Parameter_Filter_To_List_Of_Fields.getTotal_Parameter_In_Parameter_Filter_To_List_Of_Fields());
listParametersId[i] = p.id;
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
}
}
System.out.println(Arrays.toString(listParametersId));
tableParametersFilter.setValueAt(Arrays.toString(listParametersId), row, 2);
}
}
}
}

GXT 3.1: how to deselect a selected row in tree grid?

Is there a default way to allow the users deselect a selected row?
If not, I want to make the row to be deselected on another click. How can I achieve it?
In the bellow I attached the code piece for my tree grid tg
// ---------------------- set up columns ------------------------ \\
// set up name column
ColumnConfig<ProductMapping, String> cc1 = new ColumnConfig<ProductMapping, String>(new ValueProvider<ProductMapping, String>(){
#Override
public String getValue(ProductMapping object) {
return object.getName();
}
#Override
public void setValue(ProductMapping object, String value) {
object.setName(value);
}
#Override
public String getPath() {
return "name";
}
});
cc1.setWidth(200);
cc1.setHeader("Name");
// setup solution column
ColumnConfig<ProductMapping, String> cc2 = new ColumnConfig<ProductMapping, String>(new ValueProvider<ProductMapping, String>() {
#Override
public String getValue(ProductMapping object) {
return object.getSolution();
}
#Override
public void setValue(ProductMapping object, String value) {
object.setSolution(value);
}
#Override
public String getPath() {
return "solution";
}
});
cc2.setHeader("Solution");
cc2.setWidth(200);
// setup condition column
ColumnConfig<ProductMapping, String> cc3 = new ColumnConfig<ProductMapping, String>(new ValueProvider<ProductMapping, String>() {
#Override
public String getValue(ProductMapping object) {
return object.getCondition();
}
#Override
public void setValue(ProductMapping object, String value) {
object.setCondition(value);
}
#Override
public String getPath() {
return "condition";
}
});
cc3.setHeader("Condition");
cc3.setWidth(200);
// create column model
List<ColumnConfig<ProductMapping,?>> ccl = new LinkedList<ColumnConfig<ProductMapping,?>>();
ccl.add( cc1);
ccl.add( cc2);
ccl.add( cc3);
ColumnModel<ProductMapping> cm = new ColumnModel<ProductMapping>(ccl);
// Create the tree grid using the store, column model and column config for the tree column
tg = new TreeGrid<ProductMapping>(treeStore, cm, ccl.get(0));
// tg.getSelectionModel().get
tg.addRowClickHandler(new RowClickEvent.RowClickHandler(){
#Override
public void onRowClick(RowClickEvent event) {
tgRowClicked(event);
}
});

Can't sort celltable column

I want to sort celltable column, have written so far this, but it doesn't seem to be working, i did it all like in gwt showcase with celltable column sorting:
ListHandler<M> sort;
public View() {
getM(0,m);
createM();
}
void createM() {
Column<M, String> firstColumn = new Column<M, String>(
new TextCell()) {
#Override
public String getValue(M object) {
return object.getName();
}
};
table.addColumn(firstColumn,"Name");
firstColumn.setSortable(true);
sort.setComparator(firstColumn, new Comparator<M>() {
#Override
public int compare(M m1, M m2) {
return m1.getName().compareTo(m2.getName());
}
});
void getM(int dataID, M m) {
final ListDataProvider<M> listProvider = new ListDataProvider<M>();
listProvider.addDataDisplay(table);
listProvider.refresh();
final List<M> mList = listProvider.getList();
sort = new ListHandler<M>(mList);
AsyncCallback<List<M>> callback = new AsyncCallback<List<M>>() {
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(final List<M> result) {
for (final M m : result) {
mList.add(m);
}
table.addColumnSortHandler(sort);
}
};
rpcService.getDataSource(dataID, m, callback);
any suggestion, what is wrong here? How can i solve this?
i moved my code from getM() into createM() like:
void createM() {
final ListDataProvider<M> listProvider = new ListDataProvider<M>();
listProvider.addDataDisplay(table);
listProvider.refresh();
final List<M> mList = listProvider.getList();
sort = new ListHandler<M>(mList);
Column<M, String> firstColumn = new Column<M, String>(
new TextCell()) {
#Override
public String getValue(M object) {
return object.getName();
}
};
table.addColumn(firstColumn,"Name");
firstColumn.setSortable(true);
sort.setComparator(firstColumn, new Comparator<M>() {
#Override
public int compare(M m1, M m2) {
return m1.getName().compareTo(m2.getName());
}
});
and it sort the column, but if i choose the next data to show in column it will be added to the existing items in column, can anyone point me why it is so?
The first example is not working because you re-initialize your sort object:
sort = new ListHandler<M>(mList);
This removes all settings from sort.
In the second example, you need to call
mList.clear();
before you add new items to this list. Also, move
table.addColumnSortHandler(sort);
from the callback to your create method. There is no need to call it every time you add data.

JComboBox - How to render names for time values

I want to display a JComboBox that shows the text values but stores the actual int values for the user to select for session timeout values.
private static final String[] SESSION_TIMEOUT_OPTION_NAMES = new String[]{
"5 Minutes",
"10 Minutes",
"15 Minutes",
...
};
private static final Integer[] SESSION_TIMEOUT_OPTION_VALUES = new Integer[]{
TimeConstants.FIVE_MINUTES,
TimeConstants.TEN_MINUTES,
TimeConstants.FIFTEEN_MINUTES,
...
};
I know this can be done with a ListCellRenderer but the only way I could see it working is with a big mapping Basically alongs the lines of:
sessionTimeoutJComboBox.setRenderer(new ListCellRenderer<Integer>()
{
private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
#Override
public Component getListCellRendererComponent(...)
{
JLabel jlabel = (JLabel)defaultRenderer.getListCellRendererComponent(...);
for(int x=0; x<SESSION_TIMEOUT_OPTION_VALUES.length; x++)
{
if(SESSION_TIMEOUT_OPTION_VALUES[x] == value)
{
jlabel.setText(SESSION_TIMEOUT_OPTION_NAMES[x]);
return jlabel;
}
}
throw new RuntimeException("Invalid mapping");
}
});
Is there a better way to do this?
Btw the reason I want to do this is so that I can just do
(int)sessionTimeoutJComboBox.getSelectedItem();
and not have to care more than that.
You could use enums:
private enum TimeConstants {
FIVE_MINUTES("5 Minutes", 5),
TEN_MINUTES("10 Minutes", 10);
private final String text;
private final int value;
private TimeConstants( String text, int value ) {
this.text = text;
this.value = value;
}
public int getValue() {
return this.value;
}
#Override
public String toString() {
return this.text;
}
}
And heres how to use them with JComboBox:
TimeConstants[] constants = {TimeConstants.FIVE_MINUTES, TimeConstants.TEN_MINUTES};
JComboBox<TimeConstants> combo = new JComboBox<TimeConstants>(constants);
System.out.println( "Selected value: " + ((TimeConstants)combo.getSelectedItem()).getValue());
You could use a Map instead of doing the mappings manually. i.e.:
Map<Integer, String> timeoutOptions = new HashMap<>();
timeoutOptions.put( TimeConstants.FIVE_MINUTES, "5 Minutes");
...
and then in your renderer you could just do this:
sessionTimeoutJComboBox.setRenderer(new DefaultListCellRenderer<Integer>()
{
#Override
public Component getListCellRendererComponent(...)
{
JLabel label = (JLabel)super.getListCellRendererComponent( ... );
label.setText( timeoutOptions.get( value ) );
return label;
}
});

How to remove a row from the Cell Table

At first I used the Grid.
After creating a new version of the GWT, I want to replace the Grid on the CellTable.
Check out the javadoc for details. My example is like the one you'll find there (just a little extended):
public void onModuleLoad() {
final CellTable<Row> table = new CellTable<Row>();
TextColumn<Row> firstColumn = new TextColumn<Starter.Row>() {
#Override
public String getValue(Row object) {
return object.firstColumn;
}
};
table.addColumn(firstColumn, "header one");
TextColumn<Row> secondColumn = new TextColumn<Starter.Row>() {
#Override
public String getValue(Row object) {
return object.secondColumn;
}
};
table.addColumn(secondColumn, "header two");
TextColumn<Row> thirdColumn = new TextColumn<Starter.Row>() {
#Override
public String getValue(Row object) {
return object.thirdColumn;
}
};
table.addColumn(thirdColumn, "header three");
table.setRowCount(getList().size());
final ListDataProvider<Row> dataProvider = new ListDataProvider<Starter.Row>(getList());
dataProvider.addDataDisplay(table);
final SingleSelectionModel<Row> selectionModel = new SingleSelectionModel<Starter.Row>();
table.setSelectionModel(selectionModel);
Button btn = new Button("delete entry");
btn.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Row selected = selectionModel.getSelectedObject();
if (selected != null) {
dataProvider.getList().remove(selected);
}
}
});
RootPanel.get().add(table);
RootPanel.get().add(btn);
}
private class Row {
private String firstColumn;
private String secondColumn;
private String thirdColumn;
public Row(String firstColumn, String secondColumn, String thirdColumn) {
this.firstColumn = firstColumn;
this.secondColumn = secondColumn;
this.thirdColumn = thirdColumn;
}
}
private LinkedList<Row> getList() {
LinkedList<Row> list = new LinkedList<Row>();
list.add(new Row("first", "entry", "foo"));
list.add(new Row("second", "entry", "foo"));
list.add(new Row("third", "entry", "foo"));
list.add(new Row("fourth", "entry", "foo"));
return list;
}
Or you can just run the cycle like that
#UiHandler("combo")
public void onChange(ChangeEvent e) {
textBoxes.clear();
searchFields.clear();
while(resultsTable.getColumnCount()!=0) {
resultsTable.removeColumn(0);
}
resultsTable.redraw();
Where resultsTable is a CellTable
CellTable as part of new DataPresentationWidgets used just to display data. So you should delete according member from list of data which CellTable using to display.

Categories