Selecting elements in a JEditorPane - java

I'm creating a Java application where the user can search through a list of objects, which are then displayed in a JEditorPane window using a dynamically generated table whose size varies by the amount of results returned.
I then want to allow the user to select and edit the objects. (The Java objects, not the HTML code) Is this feasible, or should I be doing something completely different? Should I even be using a JEditorPane, or should I be using a different method, and how would I go about that?
The display in question has anywhere from 1 to 50 (depending on the results) cells that read like this:
Name
Text
a picture of the object
year
check boxes for two boolean variables that are part of the object.
The check boxes should allow the user to click on them and change the boolean variables, which would then be saved to the main object collection.
Thoughts?

JTable is suited for this sort of task. you can do a lot of what you are looking for with renderers and editors. Here is a tutorial. An exerpt with my emphasis reads:
To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. If you did not, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered. This list is initialized by the table, but you can add to it or change it. Currently, tables put the following types of data in the list:
Boolean — rendered with a check box.
Number — rendered by a right-aligned label.
Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat instance (using the default number format for the current locale).
Date — rendered by a label, with the object-to-text translation performed by a DateFormat instance (using a short style for the date and time).
ImageIcon, Icon — rendered by a centered label.
Object — rendered by a label that displays the object's string value.
Cell editors are chosen using a similar algorithm.

Related

What GWT widget should I use to display String attributes from an Object?

In my GWT application my onSuccess(Object result) method ultimately retreives an Object with an ArrayList attribute.
This attribute contains Objects which has String attributes.
I'd like to display those String attributes to my GUI, and after researching CellTable and other widgets, I can't find a simple way to do so. I can't seem modify this CellTable source code example to fit my needs.
What would be the simplest way for me to achieve this? (Simple in the context of a novice).
Edit* Each String attribute would need to be displayed in a titled column and there will be more than one row of information.
Another option is CellList<T> - this lets you take a List<T> of some kind and provide a cell that can draw each item. Since it isnt building a widget for every single item, it is much lighter weight, especially if you end up drawing hundreds or thousands of items.
In contrast to CellTable<T> or DataTable<T>, the CellList<T> maps each cell to each item, instead of assuming that each item in the list is an object with many properties, each of which should go in its own column.
As a sample of how you might draw each string, com.google.gwt.cell.client.TextCell knows just enough to take each string and automatically draw it as-is, no custom styles or anything. As you need more styling later, you can build your own cell.
Small example with comments, taken from GWT's CellListExample with comments (and edited slightly):
// Create a cell to render each value.
TextCell textCell = new TextCell();
// Create a CellList that uses the cell.
CellList<String> cellList = new CellList<String>(textCell);
// Set the total row count. This isn't strictly necessary, but it affects
// paging calculations, so its good habit to keep the row count up to date.
cellList.setRowCount(data.getListOfStrings().size(), true);
// Push the data into the widget.
cellList.setRowData(0, data.getListOfStrings());
// Add the cell list to its parent widget
parent.add(cellList);
FlexTable http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/FlexTable.html ?
or Grid http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/Grid.html ?
These are essentially the same. But Grid is faster, but needs to know its number of columns and rows before hand.

Looking for approach to display multiple records on single row using celltable

Generally, for GWT list views of POJOs/DTOs I typically use a ListDataProider and a CellTable and place the CellTable into a Vertical Panel with a SimplePager. All is well.
I have a scenario where the Pojo's DTO is only 3 fields; and as such it would be nice to create a list view where I can put two columns of my DTO's next to each other and page accordingly. For example, instead of just three columns per row, there would be six where the first three columns represent one DTO instance and the next three (repeated) columns represent a second DTO instance.
Has anybody successfully done this before using celltables or other GWT component? Looking for a simple approach.
My fallback position (which is good enough) was to use a FlexTable with a search form and caping the results to 40 records. Two columns of 20 DTO's.
I have done something similar. I was using a Grid. The key to align the components was this line of code:
myGrid.setWidget(position/COLUMNS_NUMBER+1, position%COLUMNS_NUMBER, lName);
Where lName was a GWT Label with the element (property) to be displayed (the 'name' property of my POJO / DTO). COLUMNS_NUMBER is my constant for how many columns I want to display.
I was displaying just this property, but you could easily adapt this idea to show more than one property of more than one DTO in different columns. My algorithm was just iterating through the DTO collection, getting each DTO's name prpoerty, which would be inserted as a Label in the Grid via setWidget, and updating the index variable position, which starts in 0.
I also needed this condition:
if(position%COLUMNS_NUMBER==0){
list.insertRow(position/COLUMNS_NUMBER +1);
}
I'm displaying only onew property of each DTO, but the idea is also not having each one in a different row, and customizing the number of columns displayed. Again, you can adapt that to meet your needs, by adapting the way you increment the index variable position and the column where you display the DTO property or (even simpler) by following the same approach and just choosing a different property to display each time depending on
postiont%3
since you want to show 3 properties of each DTO.

How Do I Format Text Like In The Linked Photo?

I have a floorplan type program that allows the user to drag and drop items onto the plan (haven't got the drag and drop working yet). Each item has an associated labor cost and price that is stored in the object class for each item.
When the user clicks on the Get Estimate button I have a new window that pops up as in the following image.
I then want to try and format the text as in the image shown. I have the window working with a JScrollPane and the buttons. I assume when I get the items working that I will pass a reference to my array of items so that I can call item.getPrice() to get each item's cost and then display it as in the image.
What I would like to know is, what is the best way to format and display the data as shown with tabbed columns using the dynamic data?
Thanks!
If the string passed to a JLabel starts and ends with <html> and </html>, it will be interpreted as HTML.
So given the rather complex layout and style elements such as header separated with a horizontal line etc., and assuming that the table, once the window has been created, is static, the easiest way might be to use a single JLabel and format the text as an HTML table.

Related to checkbox in jtable

I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

Categories

Resources