Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

urgent! Help using JScrollPanel in java swing.

Options
  • 08-09-2005 2:56pm
    #1
    Closed Accounts Posts: 779 ✭✭✭


    I am trying to use a JScrollPane to display a number of items which will be the itemName followed by an input text box which the user will fill in.
    eg
    ItemName inputJTextField
    ItemName inputJTextField
    ItemName inputJTextField
    ItemName inputJTextField
    ItemName inputJTextField
    etc........

    There are too many to list one after the other so I need to put them into a scrollable pane. All the examples I have found show only JTextArea going into a JScrollPane which wont work for me because I need JTextFields for the user to enter values into

    I have tried all combinations from putting the ItemName into a JLabel and adding it to a JPanel, then adding the JPanel to the scrollable pane, setting the label as the viewPortView (but it overwrites each label with the next one).

    Any ideas?????


Comments

  • Closed Accounts Posts: 324 ✭✭madramor


    you need to give more details of what you want heres a hack demo
    package test;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class ScrollDemo extends JFrame{
        
        public ScrollDemo() {
            setTitle("JScrollPane demo");
            add(getIt());
            setResizable(false);
            setSize(200,200);
            setVisible(true);
            setLocation(200,50);
        }
        
        private JScrollPane getIt(){
            JPanel p = new JPanel();
            p.setPreferredSize(new Dimension(300,600));
            p.setLayout(new GridLayout(10,1));
            for(int i=0;i<10;i++)
                p.add(new JLabel("Label No. "+i));
            JScrollPane sp = new JScrollPane(p);
            return sp;
                
        }
        public static void main(String args[]){
            JFrame f = new ScrollDemo();
        }
        
    }
    


  • Closed Accounts Posts: 779 ✭✭✭homeOwner


    Thanks madramor.

    That is exactly what I have but for some reason it wont work - must be the code around it that is causing things to go awry - this is the only part of the screen that is not displaying.......i will go back and start adding the components one at a time to see which one is messing it up.

    Funny enough adding the data to a JTable and displaying that seems to work!!


  • Closed Accounts Posts: 324 ✭✭madramor


    the code i posted works beacuse the Jpanel inside is (300,600)
    and the frame is (200,200) so you must have a scroll to display
    the panel inside the frame beacuse it is bigger

    a jtable would work because it does the sizing automatically


  • Closed Accounts Posts: 779 ✭✭✭homeOwner


    The problem was I had set preferred size on the JFrame to be too small, so it looked like the scroll panel wasnt showing. I also took your example of using gridlayout and it all looks much better now.

    I dont normally do swing - first time venturing into that territory.
    Cheers.


Advertisement