JSlider Label is not completely visible (java) - java

I have made a player that can display few steps with different images.
Under this player I have added a JSlider, to jump to every step. It should have labels for the first and last steps.
I managed by setting my own LabelTable for the JSlider.
It works very well for numbers under 100. But numbers with three or more digits get are cut off.
See the bottom-right corner of this image. Step 438 is not completely visible:
Is it possible to get the last (step)label completely visible?
I have already tried:
set the (max, min, preferred) size of the slider
get the JLabel over getLabelTable and set the location (a few pixels to the left) of the JLabel, but it is always (0,0).
The JSlider uses the SynthSliderUI.
Thank you! ;)

Related

How do I refer to the distance I’ve scrolled in a JScrollPane?

I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.

How to vertically align two JSlider objects independent of tick labels

I am using several horizontally positioned JSlider objects in my Java application and would like to align their left and right track ends vertically. That means whenever two knobs are positioned on the left end of their track the upper knob should be exactly above the lower one without any displacement in horizontal direction.
The problem I currently have is that long labels at ticks on the left or right end of a track decrease the length of the track and do not allow a nice alignment. As an example, see the following image:
JSlider alignment problem example
Does anyone know how to vertically align the tracks of two horizontally positioned JSlider objects independent of their tick labels?
A solution for my problem might be to left-align/right-align labels at the end of the track (i.e. the label on the far left tick is left-aligned to its tick), but I did not find out how I could do that. Maybe there is also a better solution I did not think about.
Thanks, Sandreal
I do not think that the JSlider labels are accessible in such a way that you can edit their location, so instead you should create separate labels and position then yourself relative to the JSlider, then you can be sure that they will not effect the JSlider (depending on your layout manager).

java - Auto size JFrame

I am creating a minesweeper game in Java, and I want my JFrame to be in the exact size to be able to see all of the buttons and without margin.
I have tried to calculate the size per number of tiles, and it didn't really work. Is there any way I can make the JFrame to "auto size" by the size of the tiles?
You can use .setBounds(x,y,width,height) method for the JFrame. For the width and height, create an int counter which stores the number of tiles you have on screen. I am guessing you are using buttons are the tiles? So if the user chooser the difficulty, they are basically choosing the number of tiles that the board will be. e.g. easy mode: 20x20 tiles, medium mode: 50x50 tiles, etc. So if they choose easy, set the counter number (in the actionListener of the "Easy Mode" button to '20'. Then do yourJFrame.setBounds(0,0,counter,counter); This should work for a square board.
If you want a different shape, change the above counter to "counterX" for x-axis. Then make another int counterY'. This is for the y-axis. In this case, your code will beyourJFrame.setBounds(0,0,counterX,counterY);`
This should work. Have fun!
May be try this.
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.

Java swing GridLayout adjusting cellsize

After starting a JPanel using GridLayout(4,4) i insert a JLabel (and attach an imageicon to it) inside every grid cell with size of (150,150).
when i resize the JLabel to size (100,100) the image get cropped (which is perfectly fine by me), but i get a wierd looking grid (imaged added at the end).
if this helps: i dont actually resize the window, i just need to make sure the the size of the JLabel is set to (100,100) always, no metter what is the original image size.
before:
http://postimg.org/image/iolyeb8e7/
after:
http://postimg.org/image/5j6g87ein/
thanks
Unfortunately you did not say what you expect the grid to look like. I assume you don't want the cells to be so far apart from each other.
The GridLayout documentation states that...
The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
If you shrink the size of each JLabel (i.e. the components in each of those rectangles) you just do that. You shrink the size of the component, not that of the rectangle. The grid does not care if the component is to small to fill the whole rectangle. At the moment you add the component to the grid1, the grid tries to set the components size to best fit the available space. But if you later change the labels size, the grid does not care.
What you probably want is to change the size of the whole grid: If you set the grids size to 400 by 400 it should evenly divide it to all 4 rows and 4 columns, so you get rectangles of size 100 by 100. All labels should automatically be sized accordingly.
1 Probably it is not exactly while adding the labels but while validating the container, but I don't know all the internal details about how and when layouts do there magic.

Vaadin relative sizing with component proportions?

I have a GridLayout I'm making which is populated with a bunch of pictures. The GridLayout itself is set to SizeFull(), as is each individual image in the grid.
The grid with the pictures is within another grid, and that grid has relative sizes set.
With this set up, the grid of pictures stays within the spot I want it to, properly resizing to fit within the space they should, but the pictures do not retain their proper square proportions. They squish fat or skinny however they want. I want them to retain the original proportions, though, so they expand to fill their available area as much as possible while retaining those proportions.
If I set the width to 100%, or the height to 100%, and leave the other undefined, then it retains the proportions, and properly expands to fit the one that is set to 100%, but the other spills outside the nested grid layout's spot in the upper grid layout.
Anyone know how to do this?
Have you tried setting the width and height as percentage?
So on an image of 80 by 120 pixels:
setWidth(66, Unit.PERCENTAGE)
setHeight(100, Unit.PERCENTAGE)

Categories