Some help please // with swing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Some help please // with swing

  1. #1
    Join Date
    Aug 2006
    Posts
    4

    Wink Some help please // with swing

    i have a application that is giving me a bit of a migrene ...
    never mind the rest ... the bit that i have the problem with :

    in an applet i wanna list a form .. to get info from the users
    my steps are .. make a jFrame, add a panel to the frame (frame.setContentpane() .. etc ) and in the panel put the necessary components..
    the problem -- i want my frame to be 600/500 .. but i need the panel to be something like 600/1000 .. and i can't get the frame ( or panel) to display in a scrollable manner ...

    boiled down to the essentials .. this is the code :

    public class ScroolTest
    {

    public void createFrame(JScrollPane contentPane) {

    jFrame frame = new jFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBackground(Color.RED);
    frame.setSize(new Dimension(600, 400));
    frame.setResizable(false);
    frame.setContentPane(contentPane);
    frame.pack();
    frame.setVisible(true);

    }// end Frame


    public void createJPanel() {

    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.LIGHT_GRAY);
    panel.setAutoscrolls(true);
    panel.setSize(new Dimension(1000, 1000));

    JScrollPane scr = new JScrollPane();
    scr.setPreferredSize(new Dimension(600, 300));
    scr.add(panel);




    createFrame(scr);

    }// end createPanel






    public static void main(String arg[]) {
    ScroolTest st = new ScroolTest();
    st.createJPanel();


    }// end main


    i understand that JPanel doesn't implement "Scrollable" interface .. did that too .. still doesn't seem to work ..

    some help please .. or other sugestion of implementation ..

    thank's
    Last edited by Bogdan_1; 08-23-2006 at 07:14 AM. Reason: evrica momment

  2. #2
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    Hi there, what you need to do is add a jpanel within another jpanel mand make the first jpanel scrollable. Here is an example snippet of a scrolling jpanel

    http://www.cy2online.net/Category.ph...-Frame#Snippet

    There are lots for snippets and java source downloads that might intrest you to.

  3. #3
    Join Date
    Aug 2006
    Posts
    4
    wow ... now that i think about it .. it make's perfect sence
    thank you very much

  4. #4
    Join Date
    Aug 2006
    Posts
    4

    still on the swing problem


    thank you again. that solved my problem ... but still there is some head scratching i'll like to get rid of ...

    a piece of code from the example :

    Frame frame = new JFrame("Scrolling JPanel");
    JPanel insidePane = new JPanel(new SpringLayout());
    JPanel mainPane = new JPanel();

    insidePane.setLayout(new GridLayout(20,1));


    if i comment out " insidePane.setLayout(new GridLayout(20,1)); " the frame will stop displaying scoll's ... Why do i have to change the layout manager from Spring to Grid in order to get scollable area.

    my first thought was that implementing grid .. forced the pannel to expand to the set number of rows and beeing bigger than the frame is forced to have scoll's ... But even if a set the grid to the smallest number possible .. still the scroll appears.
    now, when i use Spring ( wich i like vry much ) i know ( or hope i know )that the panel is a certain size because i pinpoint the localtion on the panel ..
    can someone "Show me the light"

    thank' you

  5. #5
    Join Date
    Aug 2006
    Posts
    3
    I've posted code below; I think you are looking for this. I don't like the scrolling JPanel link; first of all, the ScrollingJPanel extends JFrame, and in main another JFrame gets created...and this is the one actually used. ScrollingJPanel which is a JFrame never gets touched! As for SpringLayout, there is no problem with using it. The reason why the SpringLayout won't work in the ScrollingJPanel example is b/c there was no call to SpringUtilities.makeCompactGrid( xxx )

    http://java.sun.com/docs/books/tutor...Utilities.java

    As for the ScrollPane in your original examples; if you call pack() before you make your frame visible, all your components will be set to their preferredsize. This defeats the purpose of what I think you are trying to do -- since you are trying to use a ScrollPane, I believe you have predetermined that you want a frame of 600 x 1000, and that if your components total height grows bigger than 1000, you don't want to expand the frame but rather show scrollbars. Does this make sense? For example, if you have 120 JLabels w/ a preferred height = 10, then your min preferred size height will be @ least 1200 and if you call pack() your frame will resize to this.

    Below in my example, I have also set the scrollbar policy; obviously adjust this as you see fit.

    If you don't want your frame to expand, remove the pack() call.

    HTH,
    Stephen
    http://www.anya-and-stephen.com




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

    /**
    * Created by IntelliJ IDEA.
    * User: Stephen Lum
    */
    public class ScrollingJPanelLumExample {

    public static void main (String args[]){
    JFrame frame = new JFrame("ScrollingJPanelLumExample");
    JPanel insidePane = new JPanel(new SpringLayout());
    insidePane.add(new JLabel("Label 1"));
    insidePane.add(new JLabel("Label 2"));
    insidePane.add(new JLabel("Label 3"));
    insidePane.add(new JLabel("Label 4"));
    insidePane.add(new JLabel("Label 5"));
    insidePane.add(new JLabel("Label 6"));
    insidePane.add(new JLabel("Label 7"));
    insidePane.add(new JLabel("Label 8"));
    insidePane.add(new JLabel("Label 9"));
    insidePane.add(new JLabel("Label 10"));
    insidePane.add(new JLabel("Label 11"));
    insidePane.add(new JLabel("Label 12"));
    insidePane.add(new JLabel("Label 13"));
    insidePane.add(new JLabel("Label 14"));
    insidePane.add(new JLabel("Label 15"));
    insidePane.add(new JLabel("Label 16"));
    insidePane.add(new JLabel("Label 17"));
    insidePane.add(new JLabel("Label 18"));
    insidePane.add(new JLabel("Label 19"));
    insidePane.add(new JLabel("Label 20"));
    insidePane.setBackground(new Color(0,0,255));

    SpringUtilities.makeCompactGrid(insidePane, 20, 1, 0, 0, 0, 0);
    JScrollPane sp = new JScrollPane(insidePane,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    frame.getContentPane().add(sp);
    frame.setSize(200,200);
    frame.pack();
    frame.setVisible(true);
    }
    }

  6. #6
    Join Date
    Aug 2006
    Posts
    3
    Btw - I left out a very very important point. You should wrap this code in the EDT - Event Dispatch Thread. I don't really want to get into Swing threading; if you're interested in swing then it's a must read -- but truthfully you probably don't need it to set a frame to visible. You will definitely need it if you are doing time consuming operations...long database writes / reads etc. or your gui will be painfully slow and blocking.

    Runnable runnable = new Runnable() {
    public void run() {
    xxx My Code xxx
    }
    }

    EventQueue.invokeLater(runnable);

  7. #7
    Join Date
    Aug 2006
    Posts
    4
    thank you, don't think i'd ever figured it out on my own ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center