Howto get size of a cell in JGoodies FormLayout - java

I want to resize an image inside a JGoodies form layout according to the size the cell has, i.e. so that the image fits perfectly in the cell. If I understand correctly, there is no real component for the cell, the size is known only implicitly by the layout manager, right? Any ideas?

As suggested by kleopatra I found this solution: The image is put inside a scrollPane and I get the viewable size of the cell by
javax.swing.JComponent.getVisibleRect().getWidth();
where the JPanel is a JComponent... I can't paste more code directly, as we use other frameworks that wrap Swing and I didn't test that directly, sorry. If somebody else has this problem and uses this solution please add low level Swing code, thanks.
Thanks for your suggestions!

Related

Vertical JLabel Alignment

I was looking for a way to rotate JLabel vertically and I found that several posts related to this topic suggest to use Graphics2d. But, in this way, the size of my JLabel is inconsistent (widht & height inverted).
I found also that another user, here, suggested this code.
Actually, the code works, but there are no indications about how to align the text of the JLabel, and this is what i get:
Can anyone help with any of the two methods (controlling size in method one or 1 aligning text in method 2)?
Thank you very much in advance.
One way is to create an Icon of the text and rotate the Icon then add the Icon to the label. Then the size of the label will be calculated normally.
Check out the Rotated Icon class for an example of this approach. You will also need the TextIcon class.
These two classes may seem like extra work, but it is an example of how to create reusable classes to you don't do custom painting all the time.

Using JSlider to move JScrollPanel?

Im making a Java application which views a set of images within a timeline. My Idea was to use a JScrollPane with the set of all images set out in a grid-layout, but rather than scrolling the pane using the traditional scroll bars, I wanted to use something like a JSlider.
The purpose of this is to give the user more control and idea of the time at which the subset of images is shown and to give greater effect of a timeline. I have produced a simple visual example of my idea below:
I wondered whether this was even possible as I have not seen any similar examples after extensive research. Any help with any possible solutions is greatly appreciated.
Many thanks.
Some ideas: Put the images into a JPanel and this JPanel into a JScrollPane without scroll bars at all. Then sync the slider (ChangeListener) with the JViewPort.
Or (simpler) use the horizontal scroll bar as the slider and custom a JScrollBar with the labels.

How to set JFace ListViewer size?

OK - I give up.
How can I set width and height of a JFace ListViewer?
This should be self-evident but can't seem to find anything and there's no obvious way of doing it looking at the methods of the class.
Tried this with no luck:
myListViewer.getControl().setSize(1000, 1000);
Any help appreciated.
To do this you can set a layoutdata to your List associated with the ListViewer
myListViewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
to resize the control to fill the cell horizontally and to fit the remaining horizontal space.
You don't set the size explicitly. you use layouts instead. Layouts manage the size and position of your widgets in a container. Read this eclipse article that will help you understand SWT layouts.

Fixing a swing component's height within a Box layout

Good morning,
I am making a GUI thanks to javax.swing.Box class
Inside the panel:
JLabel
JTable with fixed height
JLabel
JTable with automatic height
I tried everything to fix the first JTable height but without any success.
I dedicate a Box.createHorizontalBox() for each component of the above rows and then I add them to the Box.createVerticalBox().
Instead of getting the first result I get a layout where both JTable has a automatic height, and I'd prefered the first JTable to have a fixed height...
Thanks for any answer,
Cheers
I found a solution and I shouldn't have annoyed you with such a silly problem:
For each horizontal box I created, I added an horizontal strut of 10 pixels to show a kind of padding. Thoses struts were the firsts in the rows and it was automaticly taken as the "height reference" for the box layout building, but I'm new to awt/swing layout so I may be mistaking saying that.
I removed those struts and inserted a vertical box which contained a horizontal struts of 10 pixels. It did the job.
Anyway, thanks for your time Markus & Michael, I'll dive deeper in sun's tutorial when my boss will let me the time to do so
Cheers
You can change to row height for example by calling
TableColumn column = table.getColumnModel().getColumn(0);
column.setPreferredWidth(150);
//set all rows height
table.setRowHeight(20);
//set specific row height
table.setRowHeight(2,50);
The table size you can update by calling
setPreferredSize(Dimension preferredSize)
You also have to decide, which layout the panel shoul have. Did you set a layout?
How about showing us the actual code?
It sounds like you're not using layout managers correctly. You should probably use a BorderLayout with the "automatic" table in its CENTER position and the rest inside a second panel in the NORTH position, with that second panel using either a Boxlayout or a FlowLayout.
Sun has a very good tutorial on using Layout managers that can probably help you a lot.

Java - Is it possible to put a JLayeredPane inside JScrollPane?

when I try putting my JLayeredPane inside a JScrollPane I get a blank window (with white background) instead of the content I am trying to render (it could be an image, a button, a canvas). Does anyone know of a problem with layout managers that might cause it? Is it possible?
EDIT:
thanks to camickr help, I can now put a JLayeredPane inside a JScrollPane, though now I am facing a different problem:
I am using a very large image and I am trying to put it inside my JLayeredPane that's inside the JScrollPane. From some reason when I use this large image (I am not receiving a heap overflow exception) I get this blank (white screen). Has anyone experienced something like this?
Well this is pretty much a guess because you haven't provided much information, but it sounds to me like the JLayeredPane's preferred size is (0,0), and the "white" you're seeing is the background of the JScrollPane's JViewport child. Try setting a preferred size on the JLayeredPane as a start.
Read the Swing tutorial on How to Use Layered Panes for a working example.
Change the following line:
// add(layeredPane);
add(new JScrollPane(layeredPane));

Categories

Resources