I had a look through the documentation but could not find an answer to this... Obviously, it becomes impractical to use a JComboBox if the number of fields becomes too high, but all the same, in theory, do JComboBoxes have a maximum number of fields?
Methods like setSelectedIndex(...) and getItemAt(...) take an int variable as a parameter. This would indicate that the ComboBoxModel can support a "theoretical" maximum of INTEGER.MAX_VALUE items.
However, the items must also be rendered and ultimately displayed in a JScrollPane. The scroll pane and vertical JScrollBar also use an int variable to specify the pixel location of the scrollbar.
So you must also consider the height of each rendered item in the scroll pane.
Therefore a more reasonable "theoretical" maximum using the standard Swing components would be INTEGER.MAX_VALUE / rendered-row-height.
However, I suppose you could create a custom scroll pane that only displayed the items in blocks. So as you scroll towards the end of one block you preload the next block. Theoretically this would allow you to display all INTEGER.MAX_VALUE items in the combo box.
I don't know if there would be any other limitations for the "theoretical" maximum number of items.
In any case I'm sure we all agree a combo box would become unusable well before the "theoretical" maximum is reached.
Not to my knowledge, like you said it might become more useful to use another interface tool if you end up having to scroll for too much. My reasoning is because you can dynamically populate it which needs to be as flexible as your data.
Following the answer #camickr gave you, I created an array with the hightest integer value in java (INTEGER.MAX_VALUE, which is 2147483647) and then after waiting for ten seconds the program throwed me an Requested array size exceeds VM limim error. According to plumbr.io that means:
Java has got a limit on the maximum array size your program can
allocate. The exact limit is platform-specific but is generally
somewhere between 1 and 2.1 billion elements.
Then I decided to try just with 1.800.000 elements and that worked, it lasts 10 secs approximately and when I clicked the combo box it took like 5 seconds to load all the elements, but it worked with no issue.
Related
I am writing a ultimate monopoly game and it is nearly finished.
But in a lot of cases I want a position input from the user so the user needs to learn the number of the squares.But as you can see in the picture I dont ve enough space to write 120 different positions.My question is can I create a window which do not have a fixed size?So user can check that box when he wants to enter a position and shrink it when he/she does not need it.
You can make the label sizes relative and give priority to certain ones, for depending on which row they're on that row is larger, and shrink the other squares based on an offset relative to that.
When using "Grab Excess Horizontal Space" on multiple SWT controls within the same space, the default behavior does not divide the space between them exactly equally. Some sort of behind-the-scenes calculation seems to be done to divide it "sort of" equally, but giving a higher ratio to larger controls.
In my example here, I have created a custom table-like control using grid layouts in which the user can add any number of rows, as well as any number of boxes (custom canvases) for each row individually. My intent is to have all boxes within a given row be of equal size - and by that virtue, all rows with an equal number of boxes will have equally-sized boxes, despite being separate. In my example, however, you can see that the one box that has label text within it grabs more space than those on the same row, due to the calculation believing that it "needs more" than the others.
What would be the best way to tackle this issue?
You can try to use makeColumnsEqualWidth from GridLayout.
Apparently if the columns size is more than 70 the field get's displayed with a size where you cannot even type a single character?
I'm talking about:
new JTextField(70);
Passing in the number of columns adjusts the text field's preferred size. As camickr points out, you may be using a layout that under certain circumstances will use the minimum size which is zero.
If you wish to force the minimum size to be the preferred size (and that may or may not be a good idea), you can do:
textField.setMinimumSize(textField.getPreferredSize());
My guess is that you are using a GridBagLayout. If there is not enough space to display the component at its preferred size the component shrinks to its minimum size which I believe is 0. Try giving your component a minimum size or use a different layout manager.
If you need more help post your SSCCE.
First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space).
Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size).
Then, a final situation: Linebreaks. Fit some text in the largest possible way within e.g. 400 x 150 pixels.
An obvious way is to simply start with point 1, and then increase until you can't fit it anymore. This would work for all three problems, but would be very crude. The fitting of a single line within bounds could be done by writing it with some fixed point size, check the resulting pixel bounds of the text, and then simply scale it with a transform (the text scales properly too then, check out TransformUI).
Any ideas of other ways to attack this would be greatly appreciated!
As what you are modelling is complex, especially with line breaks, then your initial proposal of trying all sizes is along the right lines, especially if it needs to be accurate.
However, rather than testing each value, you can use a binary search to find the appropriate font size. You know the size is somewhere between 1 and 100 (your upper range). using a binary search, each test sets the font size and checks the resulting layout. If the text is too large, then we search the lower half of the current range of possible values. If the font size fits, then we search the upper half. Your search will use at most 7 attempts (100 log base 2 rounded up), it will be exact, finding the largest size without going over, and it will be flexible if you need to add more requirements later, such as a mix of fonts or more stringent constraints on the layout.
I'm assuming you are using a text component that does line wrapping, and that you can set the maximum width to 400. So, you set the font size and it does the layout giving you back the required height, laying out text within the given width.
You can use hints to try to guide the algorithm to the result quicker, such as making your first guess close to the expected size, but text rendering is fast, that the performance increase may not be worth the implementation effort.
See Wikipedia - Binary Search Algorithm
I would do the following:
Assume you want W pixels wide text.
Pick an arbitrary size, say 10pt, and see what bounding box the text-string gets for that size. Lets say it gets N pixels wide.
Set the new size to 10pt * W/N, and repeat from step one, until you get within a reasonable threshold. (Hopefully it would work within one iteration.)
This relies on the fact that the width of the string, is roughly proportional to the size of the font.
I'd instantiate the Font at the largest desired size: say 72 for one inch glyphs at 72 dpi. Use TextLayout to get the bounds and scale using AffineTransform (direct) or AffineTransformOp (offscreen), while preserving the aspect ratio. Suitable RenderingHints help, too.
I'm making an application that has many lines of data coming back from a Database stub(which will become an Oracle database), and for some reason the scroll bar stops at about the 500th element. I'm wondering if there's anyway to have all the elements show within the scroll bar.
I'm assuming here that you're using Windows, because there is a fairly general problem with scrollbars on Windows: the maximum value is a short int, 32,768. Therefore, if the height of the inner composite of a ScrolledComposite is greater than 32,768 pixels, the composite will be clipped.
I haven't found a robust way of fixing this, but there is a workaround: separate the scrollbar from the composite that you wish to scroll. You can't create a ScrollBar, but you can make a ScrolledComposite that is precisely as wide as a ScrollBar, then attach a ScrollListener to it and have it adjust the layout position of the scrolling composite.
Somewhere I have a snippet, but I'm not even exactly sure if this diagnosis applies to your scenario.
You might need to set the minimum and maximum values of the ScrollBar. You would use the setMinimum() and setMaximum() methods, respectively.
It's also a good idea to set the page increment. This is the number of scroll lines that the selected value changes by when the user clicks the area between the thumb and the arrow buttons, or presses the Page Up or Page Down buttons. You would use the setPageIncrement() method.
Finally, Oracle may impose a maximum number of rows you can retrieve from a table. I believe the default is 500 rows.