Round 2: save preferred component sizes in layout, with scroll and bookmark

This is a continuation of the question " Keeping preferred component sizes in the center of BorderLayout ", which I don’t know what to do here. If someone wants to edit the link and send me a message, somehow say how to do this, I would appreciate it.

In this question, I asked and received an answer on how to save my components in their preferred sizes and, in addition, how to place a panel that is in the center of the BorderLayout in the upper left corner of the center of this layout (instead of floating in the middle.

I have simplified my real problem to put it into question, and now I have problems with generalizing it. The following code is similar, except that it places a tab in the center of the border layout.

I cut something out: in my application, the tabbed panel is in half of the split panel, and I have the toolbar above (a separate borderlayout panel) and the navigation buttons below. I cut it all out from here because I can demo without it, but that means I have a reason to use the border layout, mainly because the panels at the center of it all are the ones I want to use in any additional which the user must provide them.

In this code, we get a tabbed panel, but the two panels inside it float to the middle of their extra space if the window gives them. In one version (see Alternative lines at the bottom of the code) we get scrollbars for them, and in the other - no.

I would like the components in the tabbed panels to keep their preferred sizes. This code does it now, plush each panel in the gridbaglayout panel, but I have other effects that I don't want.

I would like the contents of each tab bar to remain in the upper left corner of the space if it is larger than the bar requires; I would like scrollbars to appear when there is not enough space.

Are there any other opinions on how I can do this?

package example; import java.awt.BorderLayout; import java.awt.GridBagLayout; import javax.swing.BoxLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; public class BorderAndBox extends JFrame { public static void main(String args[]) { BorderAndBox bnb = new BorderAndBox(); bnb.createUI(); bnb.setLocationByPlatform(true); bnb.setVisible(true); } public void createUI() { JPanel borderPanel = new JPanel(new BorderLayout()); JPanel innerPanelOne = new JPanel(); innerPanelOne.setLayout(new BoxLayout(innerPanelOne, BoxLayout.LINE_AXIS)); String[] firstChoices = { "first", "uno", "UN" }; String[] secondChoices = { "second", "dos", "zwei" }; String[] thirdChoices = { "third", "tres", "drei" }; JComboBox firstCombo = new JComboBox(firstChoices); JComboBox secondCombo = new JComboBox(secondChoices); JComboBox thirdCombo = new JComboBox(thirdChoices); innerPanelOne.add(firstCombo); innerPanelOne.add(secondCombo); innerPanelOne.add(thirdCombo); JPanel innerPanelTwo = new JPanel(); innerPanelTwo.setLayout(new BoxLayout(innerPanelTwo, BoxLayout.PAGE_AXIS)); innerPanelTwo.add(new JLabel("additionalLabel")); JPanel panelA = new JPanel(new GridBagLayout()); panelA.add(innerPanelOne); JPanel innerPanelThree = new JPanel(); innerPanelThree.setLayout(new BoxLayout(innerPanelThree, BoxLayout.PAGE_AXIS)); JLabel lblFirst = new JLabel("first in line"); JLabel lblSecond = new JLabel("second to none"); JLabel lblThird = new JLabel("third the bird"); JLabel lblFourth = new JLabel("fourth"); JLabel lblFifth = new JLabel("fifth"); JLabel lblSixth = new JLabel("sixth"); innerPanelThree.add(lblFirst); innerPanelThree.add(lblSecond); innerPanelThree.add(lblThird); innerPanelThree.add(lblFourth); innerPanelThree.add(lblFifth); innerPanelThree.add(lblSixth); JPanel panelB = new JPanel(new GridBagLayout()); panelB.add(innerPanelThree); JScrollPane scrollPane1 = new JScrollPane(panelA); JScrollPane scrollPane2 = new JScrollPane(panelB); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.add("one", scrollPane1); tabbedPane.add("two", scrollPane2); JPanel westPanel = new JPanel(new BorderLayout()); JPanel northPanel = new JPanel(new BorderLayout()); northPanel.add(tabbedPane, BorderLayout.NORTH); westPanel.add(northPanel, BorderLayout.WEST); // the following lines are mutually exclusive; // use only the first, you have no scroll bars, // use only the second, you do have them borderPanel.add(westPanel, BorderLayout.CENTER); // borderPanel.add(tabbedPane, BorderLayout.CENTER); getContentPane().add(tabbedPane); pack(); } } 
+4
source share
1 answer

Perhaps I am missing what you are trying to do, but what seems to be missing is codE, which sets the GridBagConstraints to add components to the GridBagLayout container. For instance,

  GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0); panelA.add(innerPanelOne, gbc); //... panelB.add(innerPanelThree, gbc); 
+4
source

Source: https://habr.com/ru/post/1399494/


All Articles