0

i am having problem adding a JScrollPane to a frame that has a panel and i want to keep layout of panel as null.

if i use any layout than JScrollPane appears but not when i use layout as null

this is my code :

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

class make_time_table extends JFrame 

{

Container c;

Choice subject [] = new Choice [40];

Choice teacher [] = new Choice [40];

JScrollPane scrollpane; 

int x=50,y=10;


make_time_table()
{

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    init();
}

public void init()
{ 
     JPanel p = new JPanel();

         p.setSize(600, 400);

     p.setLayout(null);

     for(int i=0;i<40;i++)
     {

        subject[i] = new Choice();

        //subject[i].add("HEllo");

        p.add(subject[i]);

        subject[i].setBounds(x,y,50,100);

        x=x+100;

    }

     scrollpane = new JScrollPane(p);

         getContentPane().add(scrollpane, BorderLayout.CENTER);


}


public static void main(String args[])
{

    make_time_table obj = new make_time_table();

    obj.setVisible(true);

    obj.setExtendedState(JFrame.MAXIMIZED_BOTH);

    obj.setTitle("make time table");



  }

}

please tell me how to add JScrollPane to a null layout

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 6
    The answer to just about any question which asks "how do I do X using a null layout" is going to be that you don't. Use a real layout. – resueman Nov 30 '15 at 15:02
  • 3
    Why the heck do you like to keep it null.... why not be normal like the rest of us, set the correct layout manager... – Petter Friberg Nov 30 '15 at 15:03
  • sir, if i use any layout then space between two Choice List is not what i have set i.e 150 pixel between two list. can you tell me if i use any layout manager then how to set space between two list – mohit kumar Nov 30 '15 at 15:06
  • Thank you sir, i got it. you are right .i should use a layout . i used setLayout(new FlowLayout(int alignment,int hgap,int vgap)); – mohit kumar Nov 30 '15 at 15:12
  • 2
    Any and all Swing experts will agree with @resueman -- that you should use non-null layouts. Understand that you can easily nest JPanels, each using its own layout manager and thereby easily create complex GUI's without much difficulty. – Hovercraft Full Of Eels Nov 30 '15 at 15:49
  • 2
    `150 pixel between two list.` - If that is your requirement, then state that in your question along with your current approach. Then we can provide a better solution.You might also be able to use a [Swing Border](http://docs.oracle.com/javase/tutorial/uiswing/components/border.html). – camickr Nov 30 '15 at 16:02
  • 1
    1) *"150 pixel between two"* add an empty border to one, then drop both in a `FlowLayout`. So 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). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) Since this is Swing, swap the AWT based `Choice` for a Swing [`JComboBox`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.html). – Andrew Thompson Nov 30 '15 at 16:24
  • Take it from some one whose been doing UI design for over 30+ years, Swing for over 16+ years, mixing in VB, C/C++, C#, HTML, iOS, you DON'T want to use `null` layouts, they are a complete pain in the ass and a complete waste of time. You will spend ALL your time reinventing a wheel which has already been solved through the use of millions of man hours of design and development. With the difference in you screen size/resolution, DPI and font metrics, to name a few, you will want a solution which is dynamic enough to manage all this possible use cases, `null` layouts are not that solution – MadProgrammer Nov 30 '15 at 21:34
  • Understand that pixel perfect layouts are an illusion, you need to focus on other aspects of the UI, using layout constraints (where available), like padding or insets and tools like `EmptyBorder` to facilitate your needs. Focus on the readability, the ease of identification of relationships and navigation through the UI – MadProgrammer Nov 30 '15 at 21:37

1 Answers1

2

if i use any layout then space between two Choice List is not what i have set i.e 150 pixel between two list. can you tell me if i use any layout manager then how to set space between two list

Basically, use a layout manager which gives you some control, like a GridBagLayout for example. Or use compounding layouts or use Borders depending on what best suits your needs.

As another example

For example, this simply uses a GridBagLayout and changes the insets properties of the constraints to manage the spacing between the subject and teacher combos (which I'm guessing is what you mean)...

Layout example

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class MakeTimeFrame extends JFrame {

    JComboBox subject[] = new JComboBox[40];
    JComboBox teacher[] = new JComboBox[40];
    JScrollPane scrollpane;

    MakeTimeFrame() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();

        pack();
    }

    public void init() {
        JPanel p = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        Insets left = new Insets(2, 2, 0, 150);
        Insets right = new Insets(2, 2, 0, 0);

        gbc.gridx = 0;
        gbc.gridy = 0;

        for (int i = 0; i < 40; i++) {

            subject[i] = new JComboBox<>();
            teacher[i] = new JComboBox<>();

            //subject[i].add("HEllo");
            gbc.insets = left;
            p.add(subject[i], gbc);
            gbc.insets = right;
            gbc.gridx++;
            p.add(teacher[i], gbc);

            gbc.gridx = 0;
            gbc.gridy++;

        }

        scrollpane = new JScrollPane(p);

        getContentPane().add(scrollpane, BorderLayout.CENTER);

    }

    public static void main(String args[]) {

        MakeTimeFrame obj = new MakeTimeFrame();

        obj.setVisible(true);

        obj.setTitle("make time table");

    }

}

Have a look at How to Use GridBagLayout and Laying Out Components Within a Container for more details.

Don't mix heavyweight (Choice) and lightweight (JPanel and JScrollPane) components, these have a habit of completely screwing up.

Instead, have a look at How to Use Combo Boxes

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366