In a Java project I am working on, my constraints were originally set to:
JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));
From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.")
So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on.
My problem comes when I have a JTextArea in a JScrollPane with rows set to 3:
JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");
When used with a row constraint of []10[] is only 1 row high, yet when I change
String rowConstraints = "[]10[]";
to
String rowConstraints = "[]";
it suddenly uses the full 3 rows height that I specified for the JTextArea.
Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand?
In both cases you are using only one layout row (i.e. row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea, second column).
You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints:
String layoutConstraints = "fill,debug";
And launching application. I really recommend using this setting while working on your layouts.
Rows property that you set for JTextArea have nothing to do with layout rows. This property only tells JTextArea component what size it will try to have, as defined in javadocs:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...)
But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout.
Please, learn more carefully (and experiment!) what exactly JTextArea row property means.
Related
If the JTable is less than the panel size I want to use AUTO_RESIZE_ALL_COLUMNS.
If the table has more columns then I want to keep all the columns to the maximum width with a scrollbar.
But when I try to use the below code, the last column is getting shrunk.
I want to know if I'm missing something.
//Adjust columns according to max width(header,Content)
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
//Adjust the column to max width with double click if the column is shrinked
new ResizeColumnListner(table);
JScrollPane sp = new JScrollPane(table,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
table.getParent().addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(final ComponentEvent e) {
if (table.getPreferredSize().width < table.getParent().getWidth()) {
table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
} else {
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
}
}
});
The table will be auto resized depending mode value. There are 5 modes:
AUTO_RESIZE_OFF: Don't automatically adjust the column's widths at all. Use a horizontal scrollbar to accomodate the columns when their sum exceeds the width of the Viewport. If the JTable is not enclosed in a JScrollPane this may leave parts of the table invisible.
AUTO_RESIZE_NEXT_COLUMN: Use just the column after the resizing column. This results in the "boundary" or divider between adjacent cells being independently adjustable.
AUTO_RESIZE_SUBSEQUENT_COLUMNS: Use all columns after the one being adjusted to absorb the changes. This is the default behavior.
AUTO_RESIZE_LAST_COLUMN: Automatically adjust the size of the last column only. If the bounds of the last column prevent the desired size from being allocated, set the width of the last column to the appropriate limit and make no further adjustments.
AUTO_RESIZE_ALL_COLUMNS: Spread the delta amongst all the columns in the JTable, including the one that is being adjusted.
From your question , it is not clear how you adding the JScrollPane object to the frame window's content pane. Here is an Exploring Swing's Table Component article to read and get understanding how everything works together.
I have created a JPanel inside which there is a JScrollPane. I want to display only 3 items there and exceeding these, I can scroll down to view them.
I have used the code below to create the JPanel and the JScrollPane
JScrollPane slide_scroll = new JScrollPane();
slide_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
slide_scroll.setBounds(1158, 11, 196, 528);
contentPane.add(slide_scroll);
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new GridLayout(5,1));
slide_scroll.setViewportView(scrollPanel);
But after adding a 6th item, all the items are displayed in a tabular way instead of a single column. I want that, when the 6th one is added, I use the scroll bar to be able to view it
From the documentation for GridLayout:
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.
Your layout will always have five rows. The fact that you specified one column is irrelevant.
If you want your layout to always have one column, use new GridLayout(0, 1).
JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.
I have a really huge JTable and I want to add scroll bars to the JFrame so that I can scroll to see the rest of the table instead of resizing the frame.
Problem is, when I add the table and scroll-bars, the width of the columns is shrunk to fit the size of the window. Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?
"Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?"
In this case a better solution than set a fixed (constant) width for columns is set JTable's autoResizeMode property to AUTO_RESIZE_OFF in order to avoid columns be auto-resized to fit the table's visible area:
JTable table = new JTable(15, 25);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
This snippet will produce a 15 rows and 25 columns table that will look like this (note the horizontal scroll bar):
However this approach won't be helpful if the sum of the columns width in your table is less than the table's width. In that case it should be better to let the default AUTO_RESIZE_SUBSEQUENT_COLUMNS policy.
I have configured my layout like that:
panel.add(addButtons(), "wrap");
panel.add(showTable(), "growx, wrap");
So at first I am adding a button group and then I would like to grow my table as large as it can and then wrap the next component in the next "line".
However, my gui looks like that:
Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?
I appreciate your answer!
Here you clearly cannot see any values from the table. Therefore, how to grow the table so that each value can be seen?
As I've said in my comment, I don't think your problem is about columns (preferred | min | max) sizes but the default behavior of your layout manager: MigLayout. As stated in this answer by default rows in MigLayout doesn't fill all available width but just the necessary to display the longest row (based on components width). You can see this fact if you enable "debug" feature when you instantiate your layout:
MigLayout layout = new MigLayout("debug");
As I understand your question you need a combination of both growx and fillx constraint. The first one is a component constraint and the other one is a layout constraint.
That being said, pelase consider the following progression.
1. Adding scroll pane without "growx" constraint
Snippet
MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane);
Screenshot
2. Adding scroll pane with "growx" constraint
Snippet
MigLayout layout = new MigLayout("debug");
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here
Screenshot
3. Adding scroll pane with "growx" and "fillx" contraints
Snippet
MigLayout layout = new MigLayout("debug, fillx"); // Note "fillx" here
JPanel panel = new JPanel(layout);
panel.add(buttonsPanel, "wrap");
panel.add(scrollPane, "growx"); // Note "growx" here
Screenshot
You can use javax.swing.table.TableColumn;
TableColumn custom_column = yourTable.getColumnModel().getColumn(1); // 1 means column 1
custom_column.setPreferredWidth(500);
As you want to widen all columns you can use loop :
TableColumn custom_column ;
int numberOfColumns = yourTable.getColumnModel().getColumnCount();
for(int y= 0;y<numberOfColumns;y++){
custom_column=table.getColumnModel().getColumn(y);
custom_column.setMinWidth(500);
}