0

I want to have a jpanel with fixed size without layout manager in jscrollpane (which will be also without layout manager). I cant use any layout manager because i need to create an rectangle/circle at a location where user clicks (and allow drag & drop for all the created emelents)

    setLocationRelativeTo(null);
    setSize(300,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setName("dnd test");


    int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;


    JScrollPane scroll = new JScrollPane(panel,v,h);
    scroll.setLayout(null);
    scroll.setPreferredSize(new Dimension(1000, 1000));
    scroll.setBounds(0, 0, 1000, 1000);
    panel = buildComponentOnAbsoluteLayout(new JPanel(),scroll,0,0,500,500);
    panel.setBackground(Color.darkGray);
    panel.setSize(350,350);
    panel.setLayout(null);
    buildComponentOnAbsoluteLayout(new JButton("button 1"),panel,10,10,120,30);
    buildComponentOnAbsoluteLayout(new JButton("button 1"),panel,50,50,120,30);

    add(scroll);
    setVisible(true);

    public static  <T extends JComponent> T buildComponentOnAbsoluteLayout(T t,Container holder,int x, int y,int width, int heigth){
        t.setBounds(x,y,width,heigth);
        holder.add(t);
    return t;
}

Scrollbar will never pop up.

hnnn
  • 504
  • 2
  • 7
  • 24
  • 3
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 20 '14 at 11:22
  • i cant use layout manager. im trying to write a tool which is very similar to some graphical uml/er modelers, Basically user can create objects on working space (jpanel), connect objects to anothers etc... – hnnn Oct 20 '14 at 14:19
  • It's not a problem. Implement drag and drop and add components dynamically to whatever layout manager you want to use. With null layout you it will probably *not* work. To draw shapes use a graphics object which has nothing to do with layout. – user1803551 Oct 20 '14 at 17:35
  • *"i cant use layout manager"* Any logic whatsoever that is used to layout a container can be encoded in a ***custom layout*** ..and you've already proven you can't get it working without one. – Andrew Thompson Oct 20 '14 at 20:39

1 Answers1

0

I cant use any layout manager because i need to create an rectangle/circle at a location where user clicks (and allow drag & drop for all the created emelents)

Well, you can't use a standard layout manager from the JDK, but yes you SHOULD be using a layout manager because a layout manager does more than just set the location of a component.

Scrollbar will never pop up.

Your scrolling doesn't work because the panel doesn't have a proper preferred size based on the components added to the panel.

Check out the Drag Layout which is a layout manager designed for this purpose. It allows random placement but it still calculates a preferred size so scrolling will work.

camickr
  • 321,443
  • 19
  • 166
  • 288