0

Following Java's official tutorial:(https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)

However, there is no source code for this, picture:

enter image description here

So I did it myself. My stupid idea is that there are 4 areas in here. So BorderLayout does not work well for me. Because I already tried BorderLayout.PAGE_END. It's not working. So I make one panel consolidate all 3 panels except the first scroll pane. It's inefficient, but works.

But now the issue is when I drag(stretch) the frame, the last text-area not stretched, but the second last selection option is stretched.

How to make the last text area stretch when I resize the frame?

Below is my code:

wrong stretch

JRadioButton mulintselRadioButton = new JRadioButton("Multiple Interval Selection");
JRadioButton singleselRadioButton = new JRadioButton("Single Selection");
JRadioButton singleIIntSelRadioButton = new JRadioButton("Single Interval Selection");
JCheckBox rowSelection = new JCheckBox("Row Selection");
JCheckBox columnSelection = new JCheckBox("Column Selection");
JCheckBox cellSelection = new JCheckBox("cell Selection");

ButtonGroup G = new ButtonGroup();
ButtonGroup GButton = new ButtonGroup();
GButton.add(rowSelection);
GButton.add(columnSelection);
GButton.add(cellSelection);
G.add(mulintselRadioButton);
G.add(singleIIntSelRadioButton);
G.add(singleselRadioButton);

JTable table = new JTable(data, columnNames);
TableColumn column = null;
table.setPreferredScrollableViewportSize(new Dimension(400,70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
JPanel selectionMode = new JPanel(new GridLayout(4,1));
JPanel selectionOption = new JPanel(new GridLayout(4,1));
JPanel textAreaPanel = new JPanel(new GridLayout(1,1));
JPanel consolidatedPanel = new JPanel(new BorderLayout());
JLabel selcttionModeTitle = new JLabel("Selection Mode");
JLabel selcttionOptionTitle = new JLabel("Selection Options");
JTextArea textArea = new JTextArea("thsisaskjhkjk  shial");
textAreaPanel.add(textArea);
selectionMode.add(selcttionModeTitle);
selectionMode.add(singleIIntSelRadioButton);
selectionMode.add(singleselRadioButton);
selectionMode.add(mulintselRadioButton);
selectionOption.add(selcttionOptionTitle);
selectionOption.add(rowSelection);
selectionOption.add(columnSelection);
selectionOption.add(cellSelection);

textArea.setText("hsknd hkcjshksdjl sldh RadioButton mulintselRadioButton = new JRadioButton(\"Multiple Interval Selection\");\n" +
        "        JRadioButton singleselRadioButton = new JRadioButton(\"Single Selection\");\n" +
        "        JRadioButton singleIIntSelRadioButton = new JRadioButton(\"Single Interval Selection\");\n" +
        "        JCheckBox rowSelection = new JCheckBox ");

add(scrollPane,BorderLayout.NORTH);
consolidatedPanel.add(selectionMode,BorderLayout.NORTH);
consolidatedPanel.add(selectionOption,BorderLayout.CENTER);
consolidatedPanel.add(textAreaPanel,BorderLayout.SOUTH);
add(consolidatedPanel);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jian
  • 4,119
  • 1
  • 17
  • 32
  • 1
    That’s expected. Components at `BorderLayout.NORTH` and `BorderLayout.SOUTH` always get their minimum height and every extra space is distributed to the `BorderLayout.CENTER` component. But what’s the purpose of this `textAreaPanel` that only contains a single component? Why don’t you add the `textArea` directly to the `consolidatedPanel`? – Holger Jun 23 '21 at 06:59
  • `consolidatedPanel.add(textArea,BorderLayout.CENTER); This works. Seems When I stretch, The Center will stretch. But I want SOUTH stretch. any idea? @Holger – jian Jun 23 '21 at 09:10
  • 1
    Consider using a different layout manager. E.g. `GridLayout` to give all of them the same size or `GridBagLayout` for a more elaborated policy. `BoxLayout` may be an option too. – Holger Jun 23 '21 at 09:29
  • My guess would be three JPanels, with only the JTable JPanel needing to stretch when the JFrame is expanded. The JTanble JPanel would be placed in the CENTER. The main JPanel would use a BorderLayout, and be placed AFTER_LAST_LINE. The control JPanel would be placed in the BEFORE_FIRST_LINE of the main JPanel and the JTextArea JPanel would be placed inside a JScrollPane. The JTextArea JScrollPane would be placed in the CENTER of the main JPanel. See the Oracle tutorial, [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more info. – Gilbert Le Blanc Jun 23 '21 at 12:24

1 Answers1

1

Following the Java's official tutorial (the link of which you have posted in your question), one can read that it says that you can consult the example index... If you follow this link you can find a table which gives you the link to the file you are searching for. Specifically the code you are looking for is in the following link:

https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java

As for your question, you can use a BoxLayout to achieve what you are asking, exactly as the TableSelectionDemo does. The result can strech the table and the text area, but not the middle section.

gthanop
  • 3,035
  • 2
  • 10
  • 27