0

How to add jScrollPane to Null Layout of jPanel in netbeans ? i am using Null Layout for jPanel and i searched out on different sources but still I am failing to add jScrollPane to Null Layout of jPanel.

Please help with source code if possible.

Thanks for all replies commenter and also I am accepting advice and not talking about advice.

Here I seem when someone asks a question about any thing that they need but some people here discourage to someone who asks help instead of helping...

The basic need my using of null layout is, I am adding background image to jpanel using null layout with jlabel coz i am using netbeans and I am beginner of java...

I am requesting all moderators please don't remove my Edit words... people may understand my feeling coz i am java with netbeans learner.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Ayaz Ali
  • 311
  • 2
  • 7
  • 16
  • what is null layout? do you mean default one? – Nikolay Kuznetsov Dec 11 '12 at 12:44
  • thanks for reply....means like one " jPanel2.setLayout(null); " it call Null Layout of jPanel to which when i add the jScrollPane so its not working coz of PreferredSize is goes to zero... – Ayaz Ali Dec 11 '12 at 12:50
  • @NikolayKuznetsov null layout is not a default layout. Default layout for `JPanel` is a `FlowLayout`. @Ayaz Ali What component you are adding on `JScrollPane`? – Branislav Lazic Dec 11 '12 at 12:56
  • @brano88, what is the use of it then? manually layouting components? – Nikolay Kuznetsov Dec 11 '12 at 13:04
  • @brano88 thanks for reply. sir i want to add jScrollPane to null layout jpanel coz i have large school admission form which is longer then from my laptop desktop screen so for that i am adding jScrollPane to null layout of jpanel....my admission form size is Hight 1900 and width 1200 so for this i want to adding jScrollPane. – Ayaz Ali Dec 11 '12 at 13:06
  • @AyazAli What type of swing component is your form? – Branislav Lazic Dec 11 '12 at 13:13
  • 3
    Don't use null-layout. Use an appropriate LayoutManager – Guillaume Polet Dec 11 '12 at 13:18
  • 1
    -1 for insisting not using a layoutManager – kleopatra Dec 11 '12 at 13:28
  • thanks for replies and also thanks for -1 (downvote) @kleopatra but same time a person need some function for her/him base of need so at this time i am also need this layout and also i need solution for this problem.... – Ayaz Ali Dec 11 '12 at 13:43
  • thanks for replies and also thanks for -1 (downvote) @guillaume-polet but same time a person need some function for her/him base of need so at this time i am also need this layout and also i need solution for this problem.... – Ayaz Ali Dec 11 '12 at 13:44
  • @AyazAli It's your choice. You ask for advice, we provide what we think is the best option. If you don't want to take, just don't. FWIW: I can't think of any case where absolute layout was a good choice. Either clearly show us whyt you need null-layout or reconsider your position on that matter and use one. – Guillaume Polet Dec 11 '12 at 14:30
  • thanks for reply @guillaume-polet and also yes i am accepting advice and not talking about advice. here i seem when a guys ask a question about any thing that her/him need but some guys here discourage to a guys who ask help instead of helping... the basic need my using of null layout is, i am adding background image to jpanel using null layout with jlabel coz i am using netbeans and also i am beginner of java...sory for weak english – Ayaz Ali Dec 11 '12 at 14:49
  • 1
    @AyazAli For a background image take a look at [this answer](http://stackoverflow.com/a/13792503/928711). Then use an appropriate LayoutManager and you will do just fine. Honestly, not using a LayoutManager is just looking for problems and make you life miserable by having to maintain tedious code. – Guillaume Polet Dec 11 '12 at 15:07
  • @guillaume-polet thanks for help... sir actually i am beginner and i am trying to learn here from you guys... i added the code in jFrame using netbeans below the and replace the path with my image but not showing image in jpanel that added on jframe....so how to set it with jpanel in netbeans. – Ayaz Ali Dec 11 '12 at 15:26

2 Answers2

5

Instead of setting null layout for your JPanel, set BorderLayout and everything should work fine like in this example:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JPanel;

public class App extends JFrame{
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea(10,50);


    public App(){
        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(textarea),BorderLayout.CENTER);
        add(panel);
    }

    public static void main( String[] args ){

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                App a = new App();
                a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                a.pack();
                //a.setSize(1900,1200);
                a.setVisible(true);
            }
        });    
    }
}

So, just add your form instead of JTextArea.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
2
JScrollPane pane = new JScrollPane();
JPanel panel = new JPanel();
panel.add(pane);
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • thanks for reply but its not working with Null Layout of jPanel....means like one " jPanel2.setLayout(null); " it call Null Layout of jPanel to which when i add the jScrollPane so its not working coz of PreferredSize is goes to zero... – Ayaz Ali Dec 11 '12 at 12:52
  • 1
    you should add this: panel.setBounds(new Rectangle(160, 10, 110, 40)); – wxyz Dec 11 '12 at 12:56
  • 3
    @wxyz no - you should _never_ do without a layoutManager, sizing/locating components is its exclusive responsibility – kleopatra Dec 11 '12 at 13:27