How to check for empty cells in gridbaglayout?
e.g. there are 100 cells, only 10 filled with label or pictures?
Well the GridBagLayout has a getConstraints(Component) method. So you could get all the components in the Container and then get the constraint for each component. The constraint will contain the gridX/gridY value which you could use to populate a 2D array. You may also not to consider the gridWidth/gridHeight values.
So the answer is yes you could derive all the information.
You can use table2gridbag to avoid producing empty cells in the first place. It's a console tool that takes an HTML table, describing the layout, and transforms it into suitable Java source code for configuring the GridbBaglayoutManager to produce an equivalent layout.
Related
I have TreeViewTable like image below:
TreeViewTable
I want to set the second column to be hierarchical (with expander as well) instead of the first column. The first column is vertically aligned and the second column is tree-like displayed.
I have tried for hours but couldn't figure out how. What should be the best approach achieve that?
This is very easy in javafx. You just need to call method treeColumn(col) of TreeTableView
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
I'm working on UI stuff with a JList that can contain at most 8 items. Logically I have prevented the application from adding to the JList once it has reached it's limit, however, I was wondering if there was a way to perhaps explicitly set a limit on the JList itself. Which would help with the next thing I want to do, which is to repaint empty cells (up to 8 cells at most) to indicate that they are empty slots that can be filled. I'm trying to find a way to do this (perhaps by extending ListUI) which does not involve adding placeholder elements into the JList to represent empty spaces.
I think the most simple way is to implement a list model (using DefaultListModel or AbstractListModel) My idea is that the model will always have 8 items. All of them with a empty message.
If you add a new item to the model you can replace the empty text with a specific text for that item. The model will keep the track about how many items are added to the model. If you try to add more that MAX_ITEMS you can thow an exception or something like that.
Implement DefaultListModel is really easy and you have a lot of samples on Internet.
You can try the approach shown here, which limits growth to N entries, with scrolling enabled thereafter. A ListCellRenderer, shown here, may be used to alter the item's appearance.
In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.
I am designing an on-screen form to be filled in, and I think it makes sense to stick it together as a collection of text areas. I note in the documentation of JTextArea that a text area can be subdivided into rows and columns, but I can't find any methods that appear to deal with placing text directly in any specific row/column cell in a text area.
Are there such methods, or is there an alternative text component that would work better for this purpose?
Thanks in advance for any insights.
John Doner
If you want a table, there is JTable but it is a bit more complicated. (Here is a tutorial) Alternatively you could put your JTextAreas into a layout such as GridLayout
A bit hackish, but you could insert (row-1) newlines and (column-1) space characters before your actual content. Of course that would only work on a previously empty text area.
However, you can extend that approach so that you only insert characters if needed, and otherwise just count already existing characters. That is, to go to a row, you skip the first row-1 newlines. Then in that line, you skip the first column-1 characters.
If there aren't enough newlines or characters already in the text area, you add more at the end of the text.
However, it gets trickier if your text content contains newlines itself.
It's doable, but it's gonna be ugly.
The row and columns values are just used to give the text area a preferred size.
I don't see the point of trying to set text at a specific row when creating a form. Generally forms would be designed with label/text field pairs, so the user know where there are entering the text.
If you text area is for output then you just add new lines when you want to display text on a different line.
Forcing a text area to be able to insert text at a given row/column is definitely not the way it was intended to be used.
I don't understand the real requirement so I can't make any other suggestions.