How to make JTabbedPane sort automatically according to page size?

I only have a JTabbedPane inside a JFrame. JTabbedPane sets its dimensions to the maximum page width / height. Since pages are of different sizes, is it possible to get JTabbedPane to resize when another page is selected?

http://grab.by/3hIg

The top one is how it behaves now, and the bottom one is how I want it to behave (I manually resized the frame)

+4
source share
3 answers

It is pretty simple. This is due to the dynamic calculation of the differences between the size of your pages and their use to force you to use the JTabbedPane size. I did a quick experiment and it worked. So instead of posting a lot of text here - here is the code. This is not ideal, but you should get an idea. Of course, questions are welcome.

 import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Test { private static int maxW = 0; private static int maxH = 0; public static void main(String[] args) { final JFrame f = new JFrame(); final JTabbedPane tabs = new JTabbedPane(); tabs.add( createPanel(Color.RED, 100, 100), "Red"); tabs.add( createPanel(Color.GREEN, 200, 200), "Green"); tabs.add( createPanel(Color.BLUE, 300, 300), "Blue"); final Dimension originalTabsDim = tabs.getPreferredSize(); tabs.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { Component p = ((JTabbedPane) e.getSource()).getSelectedComponent(); Dimension panelDim = p.getPreferredSize(); Dimension nd = new Dimension( originalTabsDim.width - ( maxW - panelDim.width), originalTabsDim.height - ( maxH - panelDim.height) ); tabs.setPreferredSize(nd); f.pack(); } }); f.setContentPane(tabs); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static final JPanel createPanel( Color color, int w, int h ) { JPanel p = new JPanel(); p.setBackground(color); p.setPreferredSize( new Dimension(w, h)); maxW = Math.max(w, maxW); maxH = Math.max(h, maxH); return p; } } 
+3
source

I think another option is to dynamically change the panels of each tab when selecting a tab:

  • set the listener to choose JTabbedPane
  • set an empty panel on each tab, but the selected tab is the default (containing the real panel for this tab)
  • in the listener of choice:
    • remove the panel from the previously selected tab (i.e. replace it with an empty panel)
    • change the blank panel to the real panel on the newly selected tab
    • calling pack() on a window / dialog containing JTabbedPane

Disclaimer I have not tested this approach, but I believe that it should work in accordance with what you want.

Also note that dynamically resizing a dialog box based on a selected tab is not very convenient in terms of a clean graphical interface.

+3
source

How about this?

 tabbedPane.addChangeListener(new ChangeListener(){ @Override public void stateChanged(ChangeEvent arg0) { Component mCompo=tabbedPane.getSelectedComponent(); tabbedPane.setPreferredSize(mCompo.getPreferredSize()); BasicFrame.this.pack(); } }); 
0
source

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


All Articles