6 Replies - 6539 Views - Last Post: 17 June 2010 - 08:18 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

JSeparator not showing

Posted 16 June 2010 - 10:51 AM

Hello..
I was trying to learn some new Swing features and im really getting frustrated with JSeparator.. Can anyone explain why it is not showing? Thank you...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Test{

    private JFrame frame;
    private JButton button1, button2;
    
    public Test(){
        createButtons();
        createFrame();
        
    }
    
    public void createFrame(){
        frame = new JFrame("This is the title");
        Container cp = frame.getContentPane();
        
        JPanel panel = new JPanel();
        panel.add(button1);
        
        panel.add(new JSeparator(SwingConstants.VERTICAL));
        panel.add(button2);
        
        cp.add(panel,BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);
    }
    
    public void createButtons(){
        button1 = new JButton("Test");
        button2 = new JButton("Test 2");
    }





}//end class




Is This A Good Question/Topic? 0
  • +

Replies To: JSeparator not showing

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: JSeparator not showing

Posted 16 June 2010 - 01:53 PM

Add it to the frame.

Instead of this,

panel.add(new JSeparator(SwingConstants.VERTICAL));

do this

frame.add(new JSeparator(JSeparator.CENTER));
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: JSeparator not showing

Posted 16 June 2010 - 07:38 PM

First time I see using a JSeparator() that way
ususally they are created by JMenu.addSeparator()
didn't know they can work in a regular JPanel
they must be like struts or glue in Box.

It is surely a quite rare used feature, better to focus on more useful/common JComponent
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: JSeparator not showing

Posted 17 June 2010 - 12:13 PM

Hello..
I have changed the code as megaz suggested, however it is still not showing.. : (.. Any other ideas?

Im sorry.. i messed up.. instead of adding it to the JFrame, i added it to the panel.. it does show up as megaz suggested.. however.. i wanted to split two JButtons and the separator is showing below the two buttons not between them...
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: JSeparator not showing

Posted 17 June 2010 - 03:30 PM

Put the 2 buttons in a Box.createHorizontalBox(); instead of a JPanel and put a Box.createHorizontalGlue(); between them
Was This Post Helpful? 0
  • +
  • -

#6 !good  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-June 10

Re: JSeparator not showing

Posted 17 June 2010 - 08:08 PM

If you did what megaz was saying then
it shows bellow because the 2 buttons are in a panel(which panel is in the content pane)
and the seperator in the content pane(so it goes below the above panel)

To seperate the buttons, the way i hope i understood you want to do, they must all 3 be in the same container,

try to set a size for a seperator and if it does not work,

try to put it in its own panel and then add the panel between the buttons
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: JSeparator not showing

Posted 17 June 2010 - 08:18 PM

As I already mentionned JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items.
They are not for JButton, JLabel and other components in a JPanel they are for lines components
The Box class provides all the separators you need for that: glue, strut, rigidArea
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1