So I have this situation:
I have a JList that displays a bunch of Strings.
However, these strings are really long and JList is really narrow. Meaning the strings won't fit.
What I want to do is make each entry to have two rows, like this:
|Word word word |
|word word wor...|
It would do wordWrap for the first row, and then finish the secon't row by cutting the rest of the string and appeinging the three dots to what is left in a way maximum space is filled.
It doesn't really matter what I do, the important thing is that I have to use FontMetrics to measure all this stuff so I can make it work. And that's the catch.
Until whole getListCellRendererComponent(...) method is executed, the component will not be painted, thus having no graphics, thus making any font measurement impossible.
How do I get around it?
P.S. I need to use the JLabel for the visuals.
the component will not be painted, thus having no graphics, thus making any font measurement impossible.
You don't need the Graphics to use the FontMetrics.
See Left Dot Renderer which is used for a JTable, but the concepts should be the same for a list renderer as well.
You need to use JLabels? It'd be a lot easier to have getListCellRendererComponent() return a JTextArea whose height has been set to 2 lines and which has setLineWrap() and setWrapStyleWord() both set to true. Make the JTextArea uneditable, and it'll look like a 2-line JLabel.
Related
This is perhaps more of a whine than a question, and I'm aware there's a workaround to the problem described in this post. But I have a situation where I have to add components to a GridLayout, possibly enlarging the grid as I go; but the components don't get created and inserted in a nice neat order; and I don't know when I'm done being handed components to insert into the grid. In other words, in a 3x3 grid I might get handed a component to put at (0,2) and then another at (1,0) and then one at (0,0). And then I might get one at (5,2) and need to enlarge the grid. And then I might get told to replace the line at (0,0) with something else.
I understand that there's no way to say "put a component at x, y". I get that I'm going to have to build a 2D array to hold my components and then empty and refill the grid from the array, each time it changes, which is going to be quite often in several large grids, so I'd rather not. I get that life is like that and the language is the language and who am I to question why.
But I'm really curious. This seems like the most basic of operations for a grid to support. Not having it feels like I'm working with a spreadsheet that only lets you enter values in order from left to right - it's simple madness.
I haven't taken apart the source code for GridLayout, but any naive understanding of how it "must work" makes me think add(component, row, col) should be trivial to implement. It must not be, because GridLayout isn't exactly new and I can't believe I'm the first to think random access to a grid is a good idea. So it must be really hard. But why?
I realize understanding why the internals of GridLayout can't support this, doesn't solve any real problems, except the problem of me unable to stop thinking about how fundamentally weird the restriction is. In the end I'm going to end up with a parallel array, or messing with the more complex GridBagLayout, unless someone knows a better way. But I just want to know why I have to.
Why not approach the problem from a different angle:
Create your grid using GridLayout
Fill the grid with empty JPanels
JPanels that each uses a BorderLayout
Keep these JPanels where they are
But swap the JPanels that they display within them in each of their BorderLayout.CENTER positions
Alternatively, you could have them use a CardLayout, but the effect would be the same
I have been able to create a grid using an image file (serves as the empty circles), a loop, and GridLayout, but I am well aware that there's more functionality needed (like for dropping the token, though no animation is necessary yet) so I scrapped it and now I'm back to an empty grid. I am stuck and I'm not really sure how I can accomplish this. My code is a mess at the moment so I'm not sure if it'd even make sense for me to post it.
My main problem is how to build a grid, which will then just be filled with a solid color (I'm cancelling using an image file, it seems a little more complicated as far as I'm concerned) with empty circles, that I will be able to fill up with an image file of a token once the player clicks on a button that corresponds to the column he chose (and then reset everything after the game is over). In other words, a rectangle of solid color and with empty circles to be filled up by tokens, but not with solid color, but an image file.
I have been trying to familiarize myself with paint() but I only started learning GUI last week so there are still likely some more things I'll have to learn to probably understand it in a considerable degree.
I am running out of options tantamount to my knowledge of GUI (Swing in particular) and I have been trying to work on this for a week now.
Any hints?
There are multiple possible ways to solve this, but one easy one is to give a JPanel a GridLayout, and then fill it with JLabels with ImageIcons that show empty circles. When the column is selected, the appropriate JLabel is given a new ImageIcon via setIcon that shows a color filled circle.
Also,
Always strive to separate your program logic code from your GUI code, since the better your separation, the easier will be your ability to debug and enhance.
Work on small problems one at a time. Don't move on to the next problem until the current small step is solved.
Work out your logic and ideas on paper first before committing it to code.
Don't "work with paint". If you need to do Swing graphics, you'll want to override a JPanel or JComponent's paintComponent method. The paint method also concerns itself with drawing borders and children, and so overriding it can have nasty and unexpected side effects on these. Also paint is not double buffered by default, and this can lead to bad animation once you start working with animation.
Edit
You state in comment:
Will it be okay to use JButton though? Because that was what i used during my first attempt. I can use setIcon with it too right?
Do you mean use a JButton instead of a JLabel? That would work, and yes you can call setIcon on JButtons, but would make all your rectangles look like buttons. So if that's OK, then do it. Otherwise, you could still use JLabels, and then create a row grid of JButtons to put below or above your game grid, and then have the user press those buttons, and in their ActionListeners have them change the icons of a JLabel in the selected column.
But having said this, I mainly recommend that you use what works best for you. The learning will be in the creating, no matter what you create.
Edit 2
You ask:
do you think it'll be possible/a nice approach to store jlabels in an array and then lay them out in a panel?
Absolutely, either an array of JLabel[] or a List<JLabel> I think is not only possible but in fact essential for this to work well. I think that you're definitely on the right track here.
I have written a hex viewer.
However, I found a limitation in Swing. If you have a 3GB file and you're rendering at 20 pixels per row, the total height of the component is:
(3GB / 16 bytes per row) * 20 pixels per row = 3.75GB
At this point, Swing fails to render anything because the value overflows Integer.MAX_VALUE.
This is my best idea so far:
Clone enough of Java2D and JComponent to make an alternative to JComponent which does all rendering in long coordinates.
Clone JViewport so that you have a way to render the view of the component/
Put that alternative JViewport into a JScrollPane like you would normally do.
This seems like a lot of work, but such a component might be useful for things like large diagrams or large text documents.
I considered just paging it, but it seems like it would be awkward when you're using the Find feature and it matches text crossing a page boundary.
The (non-Java) hex viewer I normally use (Hex Fiend) doesn't use paging either. It just scrolls the whole height of the document as if it's not a problem.
Does anyone know a good way to get around this sort of thing?
You don't need to draw your hex viewer in long coordinates because you only need to draw the part of the file that you want to be visible at the moment. JScrollPane is a very useful tool for scrolling around large components, but you don't need it. If you want to scroll through so much stuff that it's troublesome to have a component big enough to hold it all, then you can simply do the scrolling by making a JScrollBar and painting your component according to the position of the scroll bar.
For my current project i am writing a JTable based GUI. One of the main features is the ability to adjust the sizes of all cells at runtime, depending on the contents (which change over time). Currently all cells have the same height and width, when the application is started. I would like to change that to a more sophisticated approach. I was wondering if it would be somehow possible to determine the space needed by "the content" to be displayed properly. That is without to much empty space or cutting something of.
"The content" is a string for starters. It is loaded from a database and i can't make any assumptions whatsoever about it. It may be null. In this case there should be any kind of default size for the corresponding cell.
In the long run there will be all different kinds of content to be displayed, like pictures, video and so on.
I tried working with FontMetrics to calculate the length of the strings. But since i'm using JTextPanes to display them, i can't get it to work exactly. I think this has to do with JTextPanes automatic word wrapping because sometimes the lines aren't filled up. This screws up my calculations.
Well long story short: I need some kind of design guideline to achieve the feature descriped above. I'm sure one of you clever guys knows just how to do it.
Thanks in advance,
DeKay
Maybe the text pane size calculation in this How can I measure/calculate the size a Document needs to render itself? will help you out.
As you are using JTextPane for rendering, you may find this Q&A helpful.
The conversion textPane.modelToView() always comes out to null.
Note that modelToView "Returns: the coordinates as a rectangle ... or null if the component does not yet have a positive size."
I have still no idea, how to calculate the amount of space needed in general.
IIUC, the key to understanding #camickr's example is the use of setPreferredSize() to include the text pane's changed boundary, followed by validate() which "is used to cause a container to lay out its subcomponents again."
To set the height of a row in a JTable, look at here:
public void setRowHeight(int row, int rowHeight)
To set a column width, you have to look at the TableCOlumn API here:
public void setWidth(int width)
Hope this can help
Before any one suggests HTML, I explain later why thats not an option here. I have a table that contains a column with text cells in it. I need to be able to highlight some of the text in each cell. So for example if the cell contained "cat foo dog"... I might want to highlight foo.
My current method is to use a custom TableCellRenderer that puts html into a JLabel component that gets rendered and for a time it was good. Then I noticed that when the text in the cell became too long to fit in the column width it just truncated the text without the normal "..." that happens normally in this case. Thus users didnt know there was more text they were not seeing. The other problem was that if the original text itself contained HTML, which in my case it does at times, the cell would not render correctly. I know I could just escape the html but I would still have the prevous problem.
If I use a component other than a jlabel then it makes my table's cells look strange. Does any one have any suggestions? Thanks
Well, here is a solution.
In short, you can subclass JLabel to draw the highlight manually. Override the paintComponent method to do the actual drawing and use FontMetrics to calculate where the highlighted region should be drawn.
Here is that answer in excruciating detail:
Basically, you can make a subclass of JLabel that can highlight stuff. I would do that like this; you may want to do it somewhat differently:
Add a method that tells the label which part to highlight. This could be something like this, assuming you just need one highlighted region:
public void highlightRegion(int start, int end) {
// Set some field to tell you where the highlight starts and ends...
}
If you need multiple regions, just have an ArrayList instead of a simple field. A method for dehighlighting would probably be useful too.
Now, you need to override the paintComponent method of JLabel. Here you need to do several discrete steps, which you may want to organize in different methods or something. For simplicity, I'll put it all in the paint method.
#Override
protected void paintComponent(Graphics g) {
...
First, you need to figure out the physical dimensions of the highlight, which you can do using the nice FontMetrics class. Create the FontMetrics class for the Font you're using.
FontMetrics metrics = new FontMetrics(getFont());
Now you can get all the information you need to create a rectangle that will be the highlight. You'll need the starting position, the height and the width. To get this, you'll need two substrings of the JLabel's text as follows:
String start = getText().substring(0, startOfHighlight);
String text = getText().substring(startOfHighlight, endOfHighlight);
//You may also need to account for some offsets here:
int startX = metrics.stringWidth(start);
int startY = 0; //You probably have some vertical offset to add here.
int length = metrics.stringWidth(text);
int height = metrics.getHeight();
Now you can draw the highlighted region before drawing the rest of the label:
g.fillRect(startX, startY, length, height);
super.paintComponent(g);
}
Of course, if you want the highlight to span multiple rows, that will require more work.
If you were wondering, I have actually written something like this before. On a whim, I decided to write my own text area type component from a JPanel, and this was basically the way I handled highlighting. Reinventing the wheel may be stupid in an actual project, but it does teach you random stuff that may come in useful...
Can't resist to throw the SwingX renderer decoration mechanism into the ring: its way to solve the requirement is to implement a Highlighter doing it. Which in fact is already done (though not yet in the officially supported) but hidden in the SwingLabs-Demos project, named X/MatchingTextHighlighter (you would need both) and recently fixed to cope with icons, RToL-ComponentOrientation, alignment, ellipses ..
Thats a great answer, and probably the best solution. But an alternative that some might find simpler is to use a JTextfield instead of a JLabel for rendering then you can use JTextfields highlighting capabilities i.e
void highlightWhitespaceText(JTextField text)
{
text.setHighlighter(AbstractTableCellRenderer.defaultHighlighter);
try
{
Matcher m = AbstractTableCellRenderer.whitespaceStartPattern.matcher(text.getText());
if (m.matches())
{
text.getHighlighter().addHighlight(m.start(1), m.end(1), AbstractTableCellRenderer.highlightPainter);
}
m = AbstractTableCellRenderer.whitespaceEndPattern.matcher(text.getText());
if (m.matches())
{
text.getHighlighter().addHighlight(m.start(1), m.end(1), AbstractTableCellRenderer.highlightPainter);
}
}
catch (BadLocationException ble)
{
//
}
}
You can change the properties of a JTextfield so it looks like a jLabel in other respects.