How to set JFace ListViewer size? - java

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.

Related

Wrapping Text in Java Swing

Okay, I am kind of desperate right now. I hope you guys can help.
I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.
I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content
|<---- 30% ------->|<----- 70% -------------------->|
Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout).
If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.
Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.
You can achieve this by using nested BorderLayouts. Start by setting your Panel's layout as BorderLayout.
After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.
Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.

Howto get size of a cell in JGoodies FormLayout

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!

Displaying a Button at particular co-ordinates

How to display Button or a JButton at a particular coordinates say (x,y) of a window ?
Though you can use setLocation(),
I would strongly suggest using a Layout Manager, because while using a layout manager, though you can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container, believe me this can save you at times!
Assuming you have a reason that you don't want to use any of the existing LayoutManagers in the JDK, you might think about creating your own LayoutManager, specific to your application
It is pretty easy - just subclass FlowLayout, and in the doLayout method, call the setBounds for your button (and any other components).
That way you can specify the exact position assuming your expected container size, yet retain control to reposition the button in case the container is reduced or enlarged,
You can use setBounds() to specify x,y position and width and height.
You will have to set Containers' layout to null, then you will have total control of positioning components within that.
See http://zetcode.com/tutorials/javaswingtutorial/firstprograms/ for examples

How do I neatly indent some components using Java Swing layouts

Using Swing, what is the best way to indent some components underneath a checkbox or radio button? I need to make something in the style of Firefox 3.6's Options->Privacy dialog where some checkboxes are indented under a "main" checkbox. I can use any of the standard AWT/Swing layout's including GroupLayout. I also have JGoodies FormLayout available to me. I tried using setLeadingColumn offset in FormLayout at first, but it seemed like it was not going to work well unless I was indenting under a Separator. Maybe I was just doing it wrong?
Is there anything like SWT GridLayout's horizontalIndent setting? That would be perfect.
I am working with JDK1.6.0_23.
Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.
There are several ways to do this:
Set each components border to: new EmptyBorder (0, 10, 0, 0).
Use a GridBagLayout and use an Inset (0, 10, 0, 0) to pad the left side.
Use a GridBagLayout and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.
Supply custom checkbox icons that have some empty space added to their left hand edges.
etc.
My advice would be to learn the GridBagLayout - it is somewhat unwieldy to use but it does give you pretty much all the layout power you could want. The JGoodies stuff is useful for when you want particular automatic column sizing behaviour that GBL won't give you without some additional code on your part.
You should be able to accomplish this just fine with FormLayout, just add another column for the sub items, and have the main item span 2 columns.
Another option is to use SpringLayout and add padding.

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.

Categories

Resources