I have a JTable in which one column represents the size occupied by a folder on disk. Each row represents one folder, and this column has a list of rectangles; the width of each rectangle represents a percentage of the size occupied by one type of file. Here's a picture:
I do this by creating a JLabel for each file type in a row, then, in the getTableCellRenderer() method, I have an array of JLabels, one per file type. That method sets the preferred and maximum sizes of the JLabel to be the column height and the percentage of the column width for each label.
My problem comes when the picture is narrowed; the following was made by narrowing the frame of the program illustrated above:
As you can see, the percentage representation is gone, and the width of the JLabels now appear based on the width of the text, not on the preferred/max sizes set. If I narrow the window further, the percentage calculations again appear to take precedence; it is only in a narrow width range that I see this behavior.
Is there another way to do this? Do I have to draw rectangles and use drawText() instead of using the FlowLayout and text elision that already exists?
Here's the code that sets the characteristics of the JLabels:
sizeBlockDimensions[i].setSize(newWidth, rowHeight);
sizeBlockLabels[i].setPreferredSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setMaximumSize(sizeBlockDimensions[i]);
sizeBlockLabels[i].setBackground(color);
sizeBlockLabels[i].setText(labelText);
sizeBlockLabels[i].setVisible(true);
i++;
instead of using the FlowLayout
You should be able to use the Relative Layout. It was specifically designed for relative sizes and should adjust automatically as the space available changes.
Related
I've been trying to determine a TreeView's preferred width but all I get when I call getPrefWidth() is -1. Is there a reason for that? For me the preferred width would be the width of the widest cell. That value must be stored somewhere since scrollbars are displayed when the TreeView doesn't have enough space to show all of it's content.
So, for example, I created a text field.
Tried to resize it
But once I release the mouse, it gets back to its original size
So how do I do it? How do I resize elements? And yes, I tried to change the minimum size, maximum size, preferred size but it does not work either, nothing happens.
You can do setLayout(null) on the parent or setPreferredSize on the components, either of which will allow you to resize your Components. But the best answer is to set your Font size to a larger size (setFont(...)) which will cause them to become bigger (have larger values in getPreferredSize) automatically.
JTextField tf = ...;
tf.setFont(tf.getFont().deriveFont(tf.getFont().getSize() * 2));
When I make a JTable; I have the columns, and a single row, and then beneath that I have space for ~20 more rows, more if I increase the window size. I cannot have less as if I reduce the window size I just get a vertical scroll bar.
What I would like to do is make it so that the JTable size only goes to the last row.
I thought something like this:
newTable.setMaximumSize(new Dimension(Integer.MAX_VALUE, (newTable.getRowCount() + 1) * newTable.getRowHeight()));
But this does absolutely nothing. Using setPreferred size instead makes a white background about one row bigger, but the actual table is still the same size.
http://i.imgur.com/ClpD1Y3.png
I also tried the same things on the scrollpane that the JTable is in, setting the max / min sizes did nothing, setting the preferred size allowed me to make the table smaller by resizing the window, so I'm guessing that I may be able to do what I want using this and a layout perhaps? But I get class cast exceptions when trying to give a layout to the scrollpane. So I'm unsure what to do.
Set the preferredSize of the container that the JTable is in, ie. the scrollpane
GridLayout creates a grid matrix whose dimension (no of rows and columns) is customizable using two int parameters that is takes as part of its constructor.
Why don't we have such a similar option in GridBagLayout?
In a typical scenario using a Gridbaglayout, If i wanted a component to be placed at say extreme right or middle or at any other arbitrary location wouldn't it help if I know the size of Grid Matrix?.
Each 'row' of a GridBagLayout can have different numbers of columns, so it hardly makes sense to specify it in the constructor.
Because you specify the grid coordinates of a component through GridBagContraints when adding the component. The GridBagLayout’s size is then determined automatically by the largest coordinates (and probably the grid width and grid height of the component at these coordinates).
I have a JPanel with GridBagLayout set. It has 2 columns and 1 row (2 cells). Every cell contains one JPanel which contains one JLabel (type of component is insignificant).
The JLabel in the left cell has width attribute set to 100px. The JLabel in the right cell has width attribute set to 50px. In such case the left cell extends a little and it's wider than the right cell.
I thought that GridBagLayout expands cell only when contained components take too much space.
Is it a GBL bug?
Any idea how I can solve this problem?
Any idea how I can solve this problem?
Java Swing does not work the same way as CSS/HTML. You create the components in Swing and let the GUI worry about the sizing.
Without knowing what you're trying to accomplish, all I can do is say define the Swing components and let the GridBagLayout manage the layout.
If you want the grid areas to be the same size, you would use the GridLayout.
I am afraid this is a difficult question to answer because GridBagLayout doesn't arrange it's columns and rows this way. If you have two columns and two components the columns will size themselves as the sizes of the largest components put into those columns. So your left column is bigger, because your left component is bigger.
You can adjust how much of any spare space a cell takes by adjusting your component's corresponding GridBagConstraints attributes. The weight attributes (weighty & weightx) control how much of any spare space the columns (weightx) and rows (weighty) take up. If for example your JPanels were using GridBagConstraints objects called rightGbc and leftGbc you could use the following code to alter their relative sizes.
leftGbc.weightx = 0.5;
rightGbc.weightx = 1;
This means that the right column will take up twice as much spare space as the left column and therefore (hopefully) make up for the difference in the size of your components.
Using the weight attributes can feel like a very abstract process, and it can take a while to get used to them, but once you start using them for a while you will get it. Depending on which components you are using, there are other quirks which can effect how much affect the weight attributes have on the columns/rows.