Smooth scrolling for JLists like in web browsers - java

I wan't smooth scrolling for a JList that shows some search results. With the default unit increment of the vertical scroll bar the scrollling speed is too slow. When increasing the unit increment the scrolling speed is faster but for the user the scrolling looks very abruptly and not very smooth.
Is there any built-in functionality or any other approach that allows smooth scrolling like in web browsers? Every proposed solution I found so far (e.g. this one) just increases the unit increment but that's not what I really want (or what a user is likely expecting).

JList implements Scrollable to achieve this. You can override getScrollableUnitIncrement() to return your preferred increment for a given orientation and direction.

Related

How to detect the type of scroll and then assign the touch to vertical or the inner horizontal scroll view

I have a RecyclerView which has a several View Holders and some of them are horizontally scrolling. To improve the usability of my application, I need to detect the scroll based on angle (say 30 degrees to X axis) and depending on that, assign that to child's Horizontal scroll or let it's parent's vertical scroll handle it.
From #suragch's answer https://stackoverflow.com/a/46862320/10165076, I understood we can intercept the touch event of the parentView, but I have a hard time coding that. If you can please help me here, it would be much appreciated.
Edit: Going further deep into usability, I need this to work smoothly even when the parent view is moving with the momentum of the fling.
Of the apps that I know, the Play Store app handles this perfectly, I tried to search for a solution online, but none of them could provide an approach that works as well as the Play Store does.

Size/Resize Containers Labels and Buttons in CN1

i have difficulties in resizing elements in CN1, f.e. i want to resize a Button and put an image on it to use in a MenuBar, the Buttons are too big so that it exceeds the space of a Samsung S5 Display. Im working with the Designer and tried a lot with the Themes/Styles but dont seem to find a solution for this.
The MenuBar as in the screen has too large buttons, the left one is the Logo of the Company, the Buttons are three in total but only two can be seen..
For the yellow status icon, same problem same hassle :\
Do i really have to manage this in source code or is there a more convenient way with the UIDesigner.
Regards
EDIT : i now found out that using "icon" in the Designer is rubbish. Instead use this following Trick.
Put in a Text, define a Style in the ThemeEditor, set Font-Size in millimeters. Also set the Background Image and its behaviour to ScaleToFit, it should Resize now. Only Downside i have using this technique is that i have to use Whitespaces instead of a Text, is this really the best practice or does someone have a better approach?
Also another downside is that for every Image you have to Design a single Style
In order to support multi DPI's the icon needs to adjust it's size to the many screen sizes available.
Use multi-image https://www.codenameone.com/how-do-i---fetch-an-image-from-the-resource-file---add-a-multiimage.html

ListView background scroll with data

Is it possible to make a background stretch over the whole ListView, instead of just staying on a fixed position?
I remember in CSS when making websites you could make the background-attachment fixed for a background to stay the same place when scrolling, I am trying to archive the opposite of this, making the background follow the data when you scroll down. Is there such a thing for ListView?
Using a stock ListView to do what you want would not be possible.
ListView only displays a few rows at a time in order to save on View creation/management/etc. that could bog the system down. As a result, the ListView doesn't even know the total height of itself - it doesn't render a row until that row is visible, and rows can vary in height. If it doesn't even know its own height, how could it have a background that spans the entirety of it?
That said, your idea of using a ScrollView would be possible. However, you would then be losing out on ListView's optimization - if you only had few rows, then this isn't a big deal; but if you're talking about dozens of rows (or more), then your app may seriously chug. Also, you don't get ListView's framework, which is geared towards making an easier row-based UI.
It would be possible to write your own ListView subclass (or custom ListView) that can calculate its total height (so long as you know that each row is of a fixed height), then draw the background accordingly. That is probably what I'd do if I was required to do what you describe and had too many rows to just stuff into a ScrollView comfortably.

Why is it frowned upon to use a null layout in Swing?

Recently, I started creating a program for the company I work for. Just as background info, I'm still a student and a beginner programmer, so my solution is probably not recommended and I didn't know how to do it otherwise, but it works and I'm not going to be judged for it because it's a student job totally unrelated to programming.
The thing about the program is, it's going to be run on multiple different computer with different screen sizes and resolutions (800x600 and up). To make sure it takes as much of the screen as possible without losing any part of the program, I set the layout to null and hard-coded everything using relative values.
The program is kiosk-style and I first get the screen size values and go from there (for example, off the top of my head, the left-side menu takes an eighth of the screen, the top bar 2%, etc.). I also use font metrics to make sure the components are sized correctly and that everything gets displayed nicely.
My question is: why is it so frowned upon to make the layout null instead of using the layout managers? (I was told on some forums that this is a horrible way of doing things) I know how the layout manager works and know how to use the different layouts, but for the requirements of this program (multiple different resolutions, custom button shapes and placements, text changing on the components when you change language, etc.), I couldn't see myself using the layout managers to do it all.
How do you more experienced programmers use the layout managers in a situation like this? And what do you do when you want a button to be somewhere specific and other components somewhere else specific that don't really match any of the predefined layouts?
If you layer the layout managers correctly the screen will re-flow to different sizes for you, the idea is to use a single set of layout managers on ALL screen sizes.
If you use null you will have to do each screen size yourself. Not only that but if the app can be windowed you have to support every possible size they might scroll to.
That's kind of difficult to do, but the layout mangers are designed to do just that.
There are some common tricks. BorderLayout is a great layout to start with. Sometimes you might use it at multiple levels--often with just 2 or 3 components in it. That's because it's really good at giving all but one area the minimum required area and giving everything else to the CENTER.
FlowLayout can be useful but it's tricky if your components are different sizes.
I wouldn't try GridBagLayout unless you are planning to write code to feed your layout manager (an excellent solution at that!).
I also wouldn't use GUI builders, they don't know the overall way you want to reflow your layout.
In a nutshell: because all the work that you explain above is done (or at least: should be done) by the layout manager.
More often than not, when a null layout is used, it also implies that all positions and sizes are hardcoded to a single value, so no flexibility at all is given. This means that changes in window size, language, font size, display density or any other related parameter have no effect on the layout and you get the usual ugly effects: empty parts of the window; tiny, unresizable lists; buttons with their labels cut off; ...
It sounds like the work you do should really be done by the Layout Manager. Either find one that does that (my personal suggestion would be MiGLayout, which does a lot and is easy to use) or write your own.
You are practically using a layout - your own, with all your sophisticated calculations of positions.
You can move these logic to a custom layout manager class to pacify the critics.
hmmm trick should be by mixing LayoutMangers and by usage of numbers of nested JPanels that each could have diferrent Layout or not, really depends of number of JComponents, that allows you to create GUI that looks like as layed by using AbsoluteLayout but with same look/output to the GUI for every screen resolutions and ratio (4:3, 16:9, 16:10)

Handle overlapping components in java swing

I am working on an app which when used for loading a file and drawing the contained components, may result into painting of overlapping components. For example, consider a big rectangle box containing text line inside it.
Now because these components are overlapping, it is difficult for the user to select the inner text box in this case as it has been overlapped by the rectangle box.
We were thinking of solving this with allowing the users to actually move any component to a layer below the current one. But this has its own limitations on the usability side, as then for every such case the user will have to move the bigger or the most recently painted component to a layer below and then do the other processing on the inner components like dragging etc. There can be more than 2 components at the same 2d (x & y position) in this app.
I am sure that there should be a better solution for this and could someone please provide some pointers on the implemention part of it.
I am not sure, whether i understand your question. Do you have problems, with the action listeners or because of drawing the components?
This How to Use Root Panes may be helps.
You can create a Glasspane in Java
Here's a simple example of an object drawing program that supports click and drag for multiple selections. More advanced programs often implement z-order functionality (Move Forward, Move Backward, Move to Front, Move to Back) by simply re-ordering the node list.
As an alternative to the layered pane approach, also consider JInternalFrame. This example shows How to Use Actions to manage a menu from which hidden windows may be brought to the fore.

Categories