Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5309

Re: JTabbedPane fails to clip tab text

Newsgroups comp.lang.java.gui
Date 2013-03-06 08:01 -0800
References <fc8f8e86-58b9-44ca-962b-301da5c8710c@googlegroups.com> <nospam-1C7804.10063906032013@news.aioe.org>
Message-ID <27ea8628-9891-4916-9d6d-1db1e40f357c@googlegroups.com> (permalink)
Subject Re: JTabbedPane fails to clip tab text
From FredK <fred.l.kleinschmidt@gmail.com>

Show all headers | View raw


On Wednesday, March 6, 2013 7:06:39 AM UTC-8, John B. Matthews wrote:
> In article <fc8f8e86-58b9-44ca-962b-301da5c8710c@googlegroups.com>, FredK <fred.l.kleinschmidt@gmail.com> wrote: > I have a JTabbedPane with multiple tabs, using WRAP_TAB_LAYOUT. > When the user shrinks the window, the longest tab label draws its > text outside the tab area instead of clipping it. It even runs > over into the next tab. > > Also, when using Numbus LAF, it does not size properly. > What am I missing to get this to work properly? The converse of Knute's key insight is that pack() will cause the JTabbedPane to adopt the preferred size of the _largest_ tab component. In the variation below, each component panel has a random initial width. Change the L&F and/or resize the frame to see the effect. If you can test on Mac OS X, note that AquaLookAndFeel clips the long label at small sizes. import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; public class TabTester extends JPanel { private static final Random r = new Random(); public TabTester() { setLayout(new BorderLayout()); JTabbedPane pane = new JTabbedPane( JTabbedPane.TOP, JTabbedPane.WRAP_TAB_LAYOUT); pane.addTab( "this is tab number 1, long label", createPane("p1")); pane.addTab("tab number 2", createPane("p2")); pane.addTab("this is tab 3", createPane("p3")); pane.addTab("tab 4", createPane("p4")); pane.addTab("another tab", createPane("p5")); add(pane, BorderLayout.CENTER); } private JPanel createPane(final String name) { final Dimension d = new Dimension(300 + r.nextInt(42), 200); final JPanel p = new JPanel(new GridLayout()) { @Override public Dimension getPreferredSize() { return d; } }; final JLabel label = new JLabel(name, JLabel.CENTER); p.add(label); p.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { label.setText(name + ": width: " + p.getWidth()); } }); return p; } // http://stackoverflow.com/a/11949899/230513 private static JToolBar createToolBar(final Component parent) { final UIManager.LookAndFeelInfo[] available = UIManager.getInstalledLookAndFeels(); List<String> names = new ArrayList<String>(); for (LookAndFeelInfo info : available) { names.add(info.getName()); } final JComboBox combo = new JComboBox(names.toArray()); String current = UIManager.getLookAndFeel().getName(); combo.setSelectedItem(current); combo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { int index = combo.getSelectedIndex(); try { UIManager.setLookAndFeel( available[index].getClassName()); SwingUtilities.updateComponentTreeUI(parent); } catch (Exception e) { e.printStackTrace(System.err); } } }); JToolBar bar = new JToolBar("L&F"); bar.setLayout(new FlowLayout(FlowLayout.LEFT)); bar.add(combo); return bar; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TabTester t = new TabTester(); t.add(createToolBar(frame), BorderLayout.NORTH); frame.add(t); frame.pack(); frame.setVisible(true); } }); } } -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthe

I still think that the text in a tab should NOT be drawn outside
the boundaries of the tab. If the text is too long to fit, 
it should be clipped. That is what happens in the Motif XmNotebook widsget.

When using John's code, I notice that the Windows and Windows Classic LAF
almost does this (it draws the text one pixel past the tab edge and clips
the rest), while in all other LAF's the text bleeds over the tab bounds 
up to the container's insets.

Nimbus is the worst - when the container shrinks so that is is narrower 
than the longest tab, all tabs suddenly revert to a single row, and the
first tab and part of the second are visible with the text of the 
first (long) tab bleeding over into the second tab.

-- 
Fred K

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

JTabbedPane fails to clip tab text FredK <fred.l.kleinschmidt@gmail.com> - 2013-03-05 10:51 -0800
  Re: JTabbedPane fails to clip tab text Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2013-03-05 12:47 -0800
  Re: JTabbedPane fails to clip tab text "John B. Matthews" <nospam@nospam.invalid> - 2013-03-06 10:06 -0500
    Re: JTabbedPane fails to clip tab text FredK <fred.l.kleinschmidt@gmail.com> - 2013-03-06 08:01 -0800
      Re: JTabbedPane fails to clip tab text "John B. Matthews" <nospam@nospam.invalid> - 2013-03-08 04:04 -0500
        Re: JTabbedPane fails to clip tab text Knute Johnson <nospam@knutejohnson.com> - 2013-03-08 09:33 -0800

csiph-web