3

I am creating a base class for the JFrames of my application. I would like to inject a JXSearchField in the top right and corner of all frames that inherit from this class. I have a web background and know CSS fairly well. The effect I am going for is a float:right; or Fixed position, where other elements are not effected by the height of this component.

An example of what I am talking about would be a JFrame with a JTabbedPane aligned at the top. My tab pane only has three tabs but my frame is 800px wide. This gives me plenty of space for my top right aligned search box but my tabbedpane is reserving that space for additional tabs. I want to float in or fix position my searchbox to overlay that space.

user7116
  • 63,008
  • 17
  • 141
  • 172
LDAdams
  • 682
  • 4
  • 18

2 Answers2

4

if I understand correctly, you want to paint the TextField over the JTabbedPane beside the Tabs?

There is no easy way in swing doing this. You can use a glassPane-Component which draw the TextField on top right. And set it to the Frame.

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );

JFrame frame = new JFrame();
frame.setBounds( 50, 50, 800, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

JPanel glasspane = new JPanel(new FlowLayout(FlowLayout.RIGHT));

frame.setGlassPane( glasspane );

glasspane.setOpaque( false );
JTextField textField = new JTextField("Search");
glasspane.add(textField);
glasspane.setVisible( true );

JTabbedPane tabs = new JTabbedPane();
tabs.setBorder( BorderFactory.createEmptyBorder( 10, 5, 5, 5 ) );
tabs.addTab( "Lorem", null );
tabs.addTab( "Ipsum", null );
tabs.addTab( "Dolor", null );
frame.setContentPane( tabs );
frame.setVisible( true );
oliholz
  • 7,447
  • 2
  • 43
  • 82
0

A Good Layout that a lot of people use for organizing SWING components is the GridBagLayout

GridBagLayout

RMT
  • 7,040
  • 4
  • 25
  • 37
  • 1
    @Andrew 1) GBL is useful for certain things. I know its the not best one, but for certain things it is. 2) a lot of people do use it. just because you may think no one does that is your opinion. 3) SWING or Swing doesnt make a different im not writting code here. – RMT May 27 '11 at 14:47
  • 1) Useful for 'certain things' I agree with. I use one in the [Nested Layout Example](http://stackoverflow.com/questions/5621338/about-swing-and-jtable/5630271#5630271) to center an image. 2) I never said 'no one', but it sure is not 'a lot' (just because you keep saying that). 3) Swing is a proper name, not code. `swing` is a package in the J2SE (i.e. might be seen in code). SWING is just SHOUTING. – Andrew Thompson May 27 '11 at 15:03
  • 3
    SWING is not shouting... its putting more emphasis on that word. and yes people do use it even though they dont like it, and i would really appreciate it, if you would stop commenting with useless content. Thank you – RMT May 27 '11 at 15:09