Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: two JScrollPane in one JFrame problem

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default two JScrollPane in one JFrame problem

    hiiii i have a jframe .i have created one panel in it named 'totalGUI'.now i have two panels inside 'totalGUI' named 'panelprices' and 'jpanel'..'panelprices is put inside jscrollpane named 'scrollpane' and 'panelprices' is put inside jscrollpane named 'scrollpane2'.Boths the scrollpanes are added inside totalGUI. 'totalGUI' is added inside jframe. But the problem is while displaying i jst see one vertical scrollbar for panelprices jpanel.i cannot see any other scrollbar.
    This is the output what i get.But i want scrollbar on both the sides.This is my code.Please help me solve the problem

    [code=Java]


    public class BarMaker {
    int size = 50000;
    int counter = 1;
    Map<Double, FootPrintPOJO> priceMap;
    Map<Double, Integer> finalMap;
    ArrayList<BarPOJO> barPOJOList;
    int barCounter = 0;
    double latestBid = 0;
    double latestAsk = 0;
    static int count=0;
    public static JFrame frame = new JFrame();
    public static ArrayList<JLabel> jlabelarraylist = new ArrayList<>();


    public static int x=0;
    public static int y=100;
    public static int currentY;
    public static double maxYprice=0;
    public static double minYprice=100000;
    public static int minYposition=y;
    public static int maxYposition=y;
    public static ArrayList<PricePOJO> rowDetails;
    public static JScrollPane scrollpane;
    public static double price[][]=new double[9000][2];
    public static Color color;
    public static JPanel jpanel=new JPanel();
    public static JPanel panelprices= new JPanel();
    public static JPanel totalGUI= new JPanel();

    public static void initializePrices()
    {
    double startprice =300;
    int colno=0;
    x=0;
    for(int i=0;i<9000;i++)
    {
    price[i][0]=startprice;
    price[i][1]=colno;

    startprice=NumberFormatter.formatNumber(startprice-0.05);
    JLabel jLabel = new JLabel(""+startprice);
    jLabel.setBounds(0,colno,65,10);
    panelprices.add(jLabel);
    panelprices.repaint();


    System.out.println(price[i][0]+" "+price[i][1]);
    colno=colno+10;
    }

    }

    public BarMaker() {
    totalGUI.setPreferredSize(new Dimension(100000,100000));
    totalGUI.setLayout(null);
    jpanel.setLayout(null);
    initializePrices();
    x=x+65;

    jpanel.setPreferredSize(new Dimension(50000,50000));
    //jpanel.setLayout(null);
    jpanel.setVisible(true);
    jpanel.setBackground(Color.BLUE);
    jpanel.setOpaque(true);
    jpanel.setVisible(true);
    rowDetails = new ArrayList<>();
    frame.setVisible(true);


    frame.setSize(1000, 900);
    priceMap = new TreeMap<Double, FootPrintPOJO>(Collections.reverseOrder());
    finalMap = new TreeMap<Double, Integer>();
    barPOJOList = new ArrayList<BarPOJO>();

    scrollpane = new JScrollPane(panelprices);
    scrollpane.setSize(50, 800);
    scrollpane.setVisible(true);
    jpanel.setLayout(null);
    panelprices.setLayout(null);
    panelprices.setLocation(0,0);
    panelprices.setPreferredSize(new Dimension(70,10000));
    panelprices.setOpaque(true);
    jpanel.setLocation(90,0);
    jpanel.setOpaque(true);
    jpanel.setPreferredSize(new Dimension(1000,1000));
    JScrollPane scrollpane2=new JScrollPane(jpanel);
    scrollpane2.setSize(20,20);
    totalGUI.add(scrollpane);
    totalGUI.add(scrollpane2);
    //scrollpane.setHorizontalScrollBarPolicy(JScrollPan e.HORIZONTAL_SCROLLBAR_ALWAYS);

    frame.setContentPane(totalGUI);

    }

    public void makeBar(double bid, double ask, double ltp, int volume) {

    if (priceMap.containsKey(ltp)) {
    int newVolume = priceMap.get(ltp).totalVolume;
    int volumeAtAsk = priceMap.get(ltp).volumeAtAsk;
    int volumeAtBid = priceMap.get(ltp).volumeAtBid;
    // ================================================== ====
    newVolume = volume + (int) priceMap.get(ltp).totalVolume;
    if (ltp >= ask) {
    volumeAtAsk = volume + priceMap.get(ltp).volumeAtAsk;
    } else if (ltp <= bid){
    volumeAtBid = volume + priceMap.get(ltp).volumeAtBid;
    }

    FootPrintPOJO footPrintPOJO = new FootPrintPOJO(newVolume, volumeAtAsk, volumeAtBid);
    priceMap.put(ltp, footPrintPOJO);
    int tempcol=0;
    if(footPrintPOJO.deltaVolume>0)
    {
    tempcol=(int) ((footPrintPOJO.volumeAtAsk/(footPrintPOJO.volumeAtBid+1))+1);
    while(tempcol>255)
    {
    tempcol=tempcol-50;
    }
    color=new Color(255,tempcol,tempcol);
    }
    }

    //while calling initialize is called first
    //and then makeBar is called
    //and i have other functions which go on adding jlabel in jpanel named 'jpanel' which i m not //displaying to avoid showing unnecessary code

    [\code]
    Attached Images Attached Images


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: two JScrollPane in one JFrame problem

    I prefer to help someone get their code running, but yours contains a number of bad habits and/or errors that I prefer to steer you away from rather than encourage. The points I'm trying to make in the code study below include:

    1. Learn to appreciate and use the provided layout mangers.
    2. Use static variables (or "class variables") sparingly, favoring none at all. Have a good reason for using them.
    3. Declare variables as needed for visibility outside the constructor, again sparingly.
    4. Initialize and build the class in its constructor.
    5. Set a component's size or preferred size for a reason.
    6. Let Swing do its job by using the pack() method.
    7. Launch Swing applications on the EDT as shown in the main() method.

    I've also demonstrated some points about JScrollPanes using code that has been commented out or could be commented out to change the results. Please explore those options to gain greater understanding.

    Let me know if you have any questions.
    import java.awt.Dimension;
    import java.awt.FlowLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
     
     
    public class TwoScrollPanes
    {
        // note that there are no 'class' or static variables here
     
        // the default constructor
        public TwoScrollPanes()
        {
            JFrame twoScrollPanes = new JFrame( "Two Scroll Panes" );
     
            // experiment with using the default BorderLayout, putting the
            // scroll panes in various locations
            twoScrollPanes.setLayout( new FlowLayout() );
            twoScrollPanes.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
            twoScrollPanes.add( scrollPaneCreator( 0, 1000 ) );
            twoScrollPanes.add( scrollPaneCreator( 1001, 1000 ) );
     
            twoScrollPanes.pack();
     
            twoScrollPanes.setVisible( true );
     
        } // end default constructor TwoScrollPanes()
     
        // method panelCreator() creates a JPanel that contains 'items' sequential
        // numbers beginning with 'begin'
        private JScrollPane scrollPaneCreator( int begin, int items )
        {
            // *** APPROACH 1 *** comment out this code to try APPROACH 2
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setPreferredSize( new Dimension( 200, 500 ) );
     
            JTextArea text = new JTextArea();
     
            // this line is not only unnecessary, but it limits the area shown in
            // the JScrollPane to the size specified. the lines of data exist, but
            // they cannot be viewed because they're outside the area shown in the
            // scroll pane. uncomment it and see what happens
    //        text.setPreferredSize( new Dimension( 200, 500) );
     
            // fill the text area with lines of numbers
            while ( items-- >= 0 )
            {
                text.append( "" + begin++ + "\n" );
            }
     
            // note that the following line sets the viewport of an existing
            // JScrollPane. an alternative that accomplishes the same thing is to
            // do the above steps in a different order as shown in the commented
            // code below labeled APPROACH 2 
            scrollPane.setViewportView( text );
            // *** END OF APPROACH 1 ***
    /*        
            // *** APPROACH 2 *** comment out this code to try APPROACH 1
            JTextArea text = new JTextArea();
     
            // this line is not only unnecessary, but it limits the area shown in
            // the JScrollPane to the size specified. while the lines of data exist,
            // they cannot be viewed because they're outside the area shown in the
            // scroll pane. uncomment it and see what happens
    //        text.setPreferredSize( new Dimension( 200, 500) );
     
            // fill the text areas with lines of numbers
            while ( items-- >= 0 )
            {
                text.append( "" + begin++ + "\n" );
            }
     
            JScrollPane scrollPane = new JScrollPane( text );
            scrollPane.setPreferredSize( new Dimension( 200, 500 ) );
            // *** END OF APPROACH 2 ***
    */
            // note what happens if the following line is commented out
            // move the cursor to the top of the text
            text.setCaretPosition( 0 );
     
            return scrollPane;
     
        } // end method panelCreator()
     
        // a main() method that runs the application on the EDT for test and
        // demonstration purposes
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    new TwoScrollPanes();
                }
            } );
     
        } // end method main()
     
    } // end class TwoScrollPanes

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: two JScrollPane in one JFrame problem

    Thanks for your reply.I would try to folow your guidlines.Niways the problem has been solved.Instead of using null Layout i m currenly using Borderlayout and the application is working the way i want

Similar Threads

  1. problem with my JFrame; JFrame not closing and stay in background
    By golominator in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2012, 08:20 AM
  2. problem with jframe
    By mostafa_mohamed in forum AWT / Java Swing
    Replies: 1
    Last Post: September 7th, 2012, 06:16 PM
  3. add jscrollpane to jframe
    By pravinrasal in forum AWT / Java Swing
    Replies: 1
    Last Post: November 14th, 2011, 07:01 AM
  4. PUTTING JSCROLLPANE INSIDE JFRAME
    By fueledbyjace in forum AWT / Java Swing
    Replies: 0
    Last Post: September 15th, 2011, 07:21 AM
  5. [SOLVED] need help, Problem in JScrollPane
    By sny in forum AWT / Java Swing
    Replies: 5
    Last Post: March 15th, 2010, 07:16 AM

Tags for this Thread