-1
package new1;

import javax.swing.*;

public class New1 {

    JFrame f = new JFrame("demo");
    JPanel p = new JPanel();
    JLabel l = new JLabel("one");
    JLabel la = new JLabel("two");
    JTextField t = new JTextField();
    JTextField ta = new JTextField();
    JScrollPane sc = new JScrollPane(p); // scrollbar with panel as constructor.

    New1() {
        f.add(p); // adding panel o the frame
        p.setVisible(true);
        p.setLayout(null);
        p.setSize(1400, 900);
        l.setBounds(10, 50, 100, 20); // setting dimension of labels
        la.setBounds(10, 100, 100, 20);
        t.setBounds(50, 100, 100, 20);   //setting dimension of Textfields
        tx.setBounds(50, 50, 100, 20);
        p.add(l);
        p.add(t);
        p.add(la);
        p.add(ta);
        p.add(tx);
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(1400, 900);
        sc.setSize(1350, 700);  //setting size of scrollpane
        sc.setVisible(true);
        p.add(sc);       // adding to the panel
    }

    public static void main(String[] args) {
        new New1();
    }
}

Here I am adding some label to the JPanel and want to make this panel scrollable so please help. I am adding scroll pane with panel as constructor so the I can make panel as scrollable but I failed to do.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate of [how to add jscrollpane to jframe?](https://stackoverflow.com/questions/18481241/how-to-add-jscrollpane-to-jframe) – NEKIBUR RAHMAN May 27 '17 at 18:59
  • No but I am using any jpanel m using only frame and then adding some labels and tf. So van u please solve my problem. – Ravi Ranjan Patel May 27 '17 at 19:07
  • I have added all compinent to the panel and panel to the frame but still its not scrollable and i cant automatically resizing it so plz help. In this issue – Ravi Ranjan Patel May 28 '17 at 02:48
  • Show us your code. we will fix it – NEKIBUR RAHMAN May 28 '17 at 04:57
  • public class New1 { JFrame f=new JFrame("demo"); JPanel p=new JPanel(); JLabel l=new JLabel("one"); JTextField t=new JTextField(); JScrollPane sc=new JScrollPane(p); New1() { f.add(p); p.setVisible(true); p.setLayout(null); p.setSize(1400,900); l.setBounds(10, 50, 100, 20); t.setBounds(50,100,100,20); p.add(l); p.add(t); f.setLayout(null); f.setVisible(true); f.setSize(1400,900); sc.setSize(1350, 700); sc.setVisible(true); p.add(sc); } public static void main(String[] args) { new New1();}} – Ravi Ranjan Patel May 28 '17 at 07:10
  • This is my small portion of code please fix it and make panel scrollable. Please. – Ravi Ranjan Patel May 28 '17 at 07:10
  • *"This is my small portion of code.."* 1) Don't add code in comments, where it is virtually illegible, instead [edit] the question to add it. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. .. – Andrew Thompson May 28 '17 at 09:27
  • .. 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). 5) It's not possible to use a scroll pane when using `null` layouts. It is the layout managers that give the scroll pane (`JScrollPane`) the info. it needs to work properly. – Andrew Thompson May 28 '17 at 09:28
  • `tx.setBounds(50, 50, 100, 20);` The `tx` field is not defined in that code, so it would not compile as shown. Please do not post 'something like' the code used, and do not try running code that does not compile cleanly. – Andrew Thompson May 28 '17 at 09:31
  • sorry for that. can u please write that code what i have to insert there. Becasue m beginner so m not able to do this please. – Ravi Ranjan Patel May 28 '17 at 09:52
  • BTW - do **not** use the browser 'back' button to edit the question. It destroys edits made by other people! Instead use the [edit] link below the post. – Andrew Thompson May 28 '17 at 10:32

2 Answers2

1

This is the way to go about achieving what you seem to need in this GUI.

    JPanel p = new JPanel(new GridBagLayout());
    ui.add(new JScrollPane(p, 
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(4,4,4,4);
    for (int ii=0; ii<50; ii++) {
        gbc.gridy = ii;
        gbc.gridx = 0;
        p.add(new JLabel("Label " + (ii+1)), gbc);
        gbc.gridx = 1;
        p.add(new JTextField("Text Field " + (ii+1), 20), gbc);
    }

This is how it might look.

enter image description here

Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.

It's also not possible to use a scroll pane when using null layouts. It is the layout managers that give the scroll pane (JScrollPane) the info. it needs to work properly.

Here is the complete code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class New2 {

    private JComponent ui = null;

    New2() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JPanel p = new JPanel(new GridBagLayout());
        ui.add(new JScrollPane(p, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4,4,4,4);
        for (int ii=0; ii<50; ii++) {
            gbc.gridy = ii;
            gbc.gridx = 0;
            p.add(new JLabel("Label " + (ii+1)), gbc);
            gbc.gridx = 1;
            p.add(new JTextField("Text Field " + (ii+1), 20), gbc);
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                New2 o = new New2();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                Dimension d = f.getSize();
                f.setSize(new Dimension(d.width, 400));
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • sorry but i have edited my code with your suggested code . But its still not scrollable. – Ravi Ranjan Patel May 28 '17 at 18:25
  • *"But its still not scrollable."* See the complete code above. *"i have edited my code with your suggested code.."* That code snippet was not intended as a copy/paste example. The intent was to allow you to look carefully at how it achieved the scrolling, and adapt that to *your* code. Copy/pasting random bits of code without understanding them, will not result in a working program, just a mess. – Andrew Thompson May 28 '17 at 19:30
  • Sorry for late rply.... Okay I have used this and its working so thank you so much.... But I have I doubt we added this label but how can we change there name just like I have insert month name like " June 2017" then in next lbl "July 2017" and so on ao can I do this?? – Ravi Ranjan Patel Jun 04 '17 at 18:34
0

but I am using any jpanel m using only frame and then adding some labels and tf

Actually you are using a JPanel. The content pane of a JFrame is a JPanel, so are actually adding the components to the frame.

Read the section from the Swing tutorial on Using Top Level Containers which explains the hierarchy in Swing.

So the simple solution to your problem is to:

  1. add the components to a panel,
  2. create a JScrollPane using the panel as a parameter and
  3. then add the scroll pane to the frame.

This is the way it works, You can't make a frame scrollable.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Okay I have added all component to the panel and added panel to the frame but still its not scrollable. Plz help. – Ravi Ranjan Patel May 28 '17 at 02:47
  • @RaviRanjanPatel `and added panel to the frame` - that is not what I suggested. Read the answer. If you need more help then post a proper [mcve] demonstrating the problem. – camickr May 28 '17 at 03:52
  • public class New1 { JFrame f=new JFrame("demo"); JPanel p=new JPanel(); JLabel l=new JLabel("one"); JTextField t=new JTextField(); JScrollPane sc=new JScrollPane(p); New1() { f.add(p); p.setVisible(true); p.setLayout(null); p.setSize(1400,900); l.setBounds(10, 50, 100, 20); t.setBounds(50,100,100,20); p.add(l); p.add(t); f.setLayout(null); f.setVisible(true); f.setSize(1400,900); sc.setSize(1350, 700); sc.setVisible(true); p.add(sc); } public static void main(String[] args) { new New1();}} – Ravi Ranjan Patel May 28 '17 at 04:36
  • this my small part od code .plz fix it and make it scrollable i will highly oblidge to u – Ravi Ranjan Patel May 28 '17 at 04:39
  • @RaviRanjanPatel, Don't use a null layout. Swing was designed to be used with [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). One of the functions of the layout manager is to determine the `preferred size` of the panel based on the components added to the panel. Then when the preferred size is greater than the size of the scroll pane the scrollbars will appear. – camickr May 28 '17 at 19:36