Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Java JScrollPane Help. (mabuang nako)


    mga bossing. here is my program. kanang algorithm in sorting ug search. ang akong problema ba kai kining JScrollPane sa JList di man mugana oe. pls help. mao ni screenshot


    here are my codes. scratch akong paghimo ani kai di ko kamao sa built-in. anyone can help especially Superstar, stealthghost, and etc. pls help. tnx

    pls read the red lines. tnx
    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    
    
    public class SortingGUI extends JFrame{
        
        private JLabel inputL, searchValueL;
        private JTextField inputTF, searchValue;
        private JButton QuickSort, InsertionSort,BinarySearch,
                MergeSort, SelectionSort, BubbleSort;
        private JTextArea output;
        private int array [];
        private Container pane;
        private JList list;
        private JTextArea area;
        
        public SortingGUI(){
            super( "Sorting GUI" );
            
            this.pane = getContentPane();
            this.pane.setBackground( new Color ( 0, 200, 200 ) );
            this.pane.setLayout( null );
            
            
            this.inputTF = new JTextField( );
            this.inputTF.setSize( 500 , 25 );
            this.inputTF.setLocation( 200, 20 );
            this.pane.add( this.inputTF );
            
            this.inputL = new JLabel( "Input values:" );
            this.inputL.setSize( 100, 25 );
            this.inputL.setLocation( 50, 20 );
            this.pane.add( this.inputL );
            
            this.BinarySearch = new JButton( "Binary Search" );
            this.BinarySearch.setSize( 150, 30 );
            this.BinarySearch.setLocation( 20 , 200 );
            this.pane.add( this.BinarySearch );
            
            this.BubbleSort = new JButton( "Bubble Sort" );
            this.BubbleSort.setSize( 150, 30 );
            this.BubbleSort.setLocation( 20, 240 );
            this.pane.add( this.BubbleSort );
            
            this.InsertionSort = new JButton( "Insertion Sort" );
            this.InsertionSort.setSize( 150, 30 );
            this.InsertionSort.setLocation( 20, 280 );
            this.pane.add( this.InsertionSort );
            
            this.MergeSort = new JButton( "Merge Sort" );
            this.MergeSort.setSize( 150, 30 );
            this.MergeSort.setLocation( 20, 320 );
            this.pane.add( this.MergeSort );
            
            this.QuickSort = new JButton( "QuickSort" );
            this.QuickSort.setSize( 150, 30 );
            this.QuickSort.setLocation( 20, 360 );
            this.pane.add( this.QuickSort );
            
            this.SelectionSort = new JButton( "Selection Sort" );
            this.SelectionSort.setSize( 150, 30 );
            this.SelectionSort.setLocation( 20, 400 );
            this.pane.add( this.SelectionSort );
            
            this.searchValue = new JTextField();
            this.searchValue.setSize( 40, 25 );
            this.searchValue.setLocation( 265, 80 );
            this.pane.add( this.searchValue );
            
            this.searchValueL = new JLabel();
            this.searchValueL.setText( "Input a value to for Binary Search:" );
            this.searchValueL.setSize( 300, 25 );
            this.searchValueL.setLocation( 20, 80 );
            this.pane.add( this.searchValueL );
            
            
            this.list = new JList();
            this.list.setSize( 500, 400 );
            this.list.setLocation( 200, 130 );
            
            this.pane.add( this.list  );           //naa dapit dri ni. ako unta ilisan ni na line
                                                           //ug pane.add( new JScollPane( this.list));, //di man mu display
            this.list.setFont( new Font( "Courier", Font.BOLD, 14 ) );
            
            
            
            addMouseMotionListener( new MouseHandler() );
            this.QuickSort.addActionListener( 
                    new ActionListener(){
                        public void actionPerformed( ActionEvent e ){
                            QuickSort test = new QuickSort( array );
                            list.setListData( test.list );
                        }
                    }
                    );
            
            this.SelectionSort.addActionListener( 
                        new ActionListener(){
                            public void actionPerformed( ActionEvent e ){
                                SelectionSort test = new SelectionSort( array );
                                list.setListData( test.list );
                            }
                        }
                    );
            this.BubbleSort.addActionListener(
                        new ActionListener(){
                            public void actionPerformed( ActionEvent e ){
                                BubbleSort test = new BubbleSort( array );
                                list.setListData( test.list );
                            }
                        }
                    );
            this.InsertionSort.addActionListener(
                        new ActionListener(){
                            public void actionPerformed( ActionEvent e ){
                                InsertionSort test = new InsertionSort( array );
                                list.setListData( test.list );
                            }
                        }
                    );
            this.BinarySearch.addActionListener( 
                        new ActionListener(){
                            public void actionPerformed( ActionEvent e ){ 
                                BinarySearch test = new BinarySearch( array, Integer.parseInt( searchValue.getText() ) );
                                list.setListData( test.list );
                            }
                        }
                    );
        }
        
        public void Tokenize( String val ){
            int index = 0;
            StringTokenizer myToken = new StringTokenizer( val, " ," );
            this.array = new int [ myToken.countTokens() ];
            while( myToken.hasMoreTokens() ){
                try{
                this.array [ index ] = Integer.parseInt (myToken.nextToken() );
                index++;
                }catch( NumberFormatException e ){}
            }
        }
    
    
        private class MouseHandler implements MouseMotionListener{
    
    
            public void mouseDragged( MouseEvent e ) {}
    
    
            public void mouseMoved( MouseEvent e ) {
                Tokenize( inputTF.getText() );
            }
        }
        
    }
    here are the subclasses:
    Code:
    import java.util.*;
    
    public class QuickSort extends SortingGUI{
        public Vector list = new Vector();
        private int count;
        
        public QuickSort( int [] array ){
            StringBuilder str = new StringBuilder();
            str.append("Unsorted Array: ");
            for( int a : array )str.append(a+" ");
            list.add(str);
            quick_srt( array, 0, array.length -1 );
        }
        
        public void quick_srt(int a [] ,int left , int right ){
            if( left >= right ) return;
            int pivot = a [ ( left + right ) / 2 ] ;
            int i = left; int j = right;
            
            while( i <= j ){
                while( a [ i ] < pivot && i < j )
                    i++;
                while( a [ j ] > pivot && i < j )
                    j--;
                
                if( i <= j ){
                    int temp = a [ i ];
                    a [ i ] = a [ j ];
                    a [ j ] = temp;
                    i++; j--;
                }
                
            }
            Output( a );
            quick_srt( a, left, j );
            quick_srt( a, i, right );
        }
        
         public void Output( int array [] ){
            this.count++;
            StringBuilder str = new StringBuilder();
            str.append( count+".) ");
            for( int a : array )str.append(a+" ");
            list.add(str);
        }
    }
    Code:
    import java.util.*;
    
    public class BubbleSort extends SortingGUI{
        
        private boolean swapping;
        private int count;
        public Vector list = new Vector();
        
        public BubbleSort( int array [] ){
            StringBuilder str = new StringBuilder();
            str.append("Unsorted Array: ");
            for( int a : array )str.append(a+" ");
            list.add(str);
            Process( array );
            }
        
        public void Process(int a []){
            while( 1 > 0){
            for( int x = 0; x < a.length; x++ ){
                try{
                if( a [ x ] > a [ x + 1] ){
                    this.swapping = true;
                    int temp = a [ x ];
                    a [ x ] = a [ x + 1 ];
                    a [ x + 1 ] = temp;
                    Output( a );
                }
                }catch( Exception e ){}
            }
            
            
            if( this.swapping == false )break;
            else this.swapping = false;
            }
        }
        
        public void Output( int array [] ){
            this.count++;
            StringBuilder str = new StringBuilder();
            str.append( count +".) ");
            for( int a : array )str.append( a+" ");
            this.list.add( str );
            
        }
        
    }
    Code:
    import java.util.*;
    
    
    
    public class SelectionSort extends SortingGUI{
        
        private int count;
        public Vector list = new Vector();
        
        public SelectionSort( int [] array ){
            
            StringBuilder str = new StringBuilder();
            str.append("Unsorted Array: ");
            for( int a : array )str.append(a+" ");
            list.add(str);
            Process( array );
       }
        
        public void Process( int [] a ){
            for( int x = 0; x < a.length ; x++ ){
                for( int y = x +1; y < a.length; y++ ){
                    if( a [ y ] < a [ x ] ){
                        int temp = a [ x ];
                        a [ x ] = a [ y ];
                        a [ y ] = temp; 
                        
                    }
                    Output( a );
                }
                Output( a );
            }
        }
        
        public void Output( int array [] ){
            this.count++;
            StringBuilder str = new StringBuilder();
            str.append( count+".) ");
            for( int a : array )str.append( a+" " );
            list.add(str);
        }
        
    }
    Code:
    import java.util.*;
    
    public class InsertionSort extends SortingGUI{
        private int count;
        public Vector list = new Vector();
        
        public InsertionSort( int array [] ){
            StringBuilder str = new StringBuilder();
            str.append("Unsorted Array: ");
            for( int a : array )str.append(a+" ");
            list.add(str);
            Process( array, array.length );
            }
        
        public void Process( int array [], int length ){
            LinkedList list = new LinkedList();
            boolean isGreater = false;
            list.add( array[0]);
            int index = 0, count=1;
            
            for( int i = 1; i < length; i++ ){
                for( int x = 0; x < list.size(); x++){
                    
                    if( array[i] > (int) list.get(x) )
                        isGreater = true;
                    else{
                        isGreater = false;
                        index = x;
                    break;
                    }
                 }
                
                if( isGreater == false )
                        list.add( index, array [ i ]);
                else
                    list.add( array [ i ]);
                
                    this.list.add( count+".) "+list );
                 count++;
            }
          
         }
        
    }
    Code:
    import java.util.*;
    
    public class BinarySearch extends SortingGUI{
        
        public Vector list = new Vector();
        
        public BinarySearch( int a [], int val ){
            Process( a, val );
        }
        
        public void Process( int a[] ,int val ){
            StringBuilder str = new StringBuilder();
            boolean found = false;
            int first = 0, mid = 0, last = a.length-1;
            while( first <= last ){
                mid = (int) ( (first +last) / 2 );
                
                System.out.println("aw");
                if( a [ mid ] == val ){
                    list.add("\n"+val+" was found in index "+mid);
                    found = true;
                    break;
                }else if( a [ mid ] > val )
                    last = mid - 1;
                else 
                    first = mid + 1;
                list.add("\nPassed in index "+mid);
            }
            if( found == false )
                list.add( "\n"+val+" was not found" );
        }
        
        
    }
    here is the main program
    Code:
    import javax.swing.JFrame;
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    /**
     * 5, 4, 2, 6, 8, 3, 7, 9, 10, 59 ,34, 16, 0, -1, -9, -20
     * @author jairoh
     */
    public class SortingGUITest {
        
        public static void main( String args [] ){
            SortingGUI test = new SortingGUI();
            test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            test.setVisible( true );
            test.setSize( 800, 600 );
        }
    }
    Last edited by jairoh_; 08-18-2012 at 04:58 PM.

  2. #2

    Default Re: Java JScrollPane Help. (mabuang nako)

    Bro wat do u mean di mogana? Meaning di mopakita ang scroll if mapuno na ang text area? Try daw setPrefferredSize(null) ang imong scrollpane.

    Dili nako makit imong code kay mobile ra ko ron.

    I hope ma solve imong problem.

  3. #3

    Default Re: Java JScrollPane Help. (mabuang nako)

    How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    A scroll pane uses a JViewport instance to manage the visible area of the client. The viewport is responsible for positioning and sizing the client, based on the positions of the scroll bars, and displaying it.

    A scroll pane may use two separate instances of JScrollBar for the scroll bars. The scroll bars provide the interface for the user to manipulate the visible area. The following figure shows the three areas of a scroll bar: the knob (sometimes called the thumb), the (arrow) buttons, and the track.

  4. #4
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java JScrollPane Help. (mabuang nako)

    Quote Originally Posted by SuperStar View Post
    Bro wat do u mean di mogana? Meaning di mopakita ang scroll if mapuno na ang text area? Try daw setPrefferredSize(null) ang imong scrollpane.

    Dili nako makit imong code kay mobile ra ko ron.

    I hope ma solve imong problem.
    di man japon bro Superstar.

    Quote Originally Posted by stealthghost View Post
    How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    A scroll pane uses a JViewport instance to manage the visible area of the client. The viewport is responsible for positioning and sizing the client, based on the positions of the scroll bars, and displaying it.

    A scroll pane may use two separate instances of JScrollBar for the scroll bars. The scroll bars provide the interface for the user to manipulate the visible area. The following figure shows the three areas of a scroll bar: the knob (sometimes called the thumb), the (arrow) buttons, and the track.
    di man sad japon bro stealthghost kai murag applicable ra cya sa kung gamit ug BorderLayout which is kung gamiton nako matabunan akong uban nga features or options.

    also the JViewport kai para ra sa visible area, and katong ubos ug sa kilid sa akong JList kai di man makita, so useless japon. Dba supposedly naa mana scrollPane ang JList pero dunno nganung di ko kahimo. pero kung build in na netbeans ang pahimuon nimo, naai scrollbar ila JList, mao nang nalibog jud ko.

  5. #5

    Default Re: Java JScrollPane Help. (mabuang nako)

    Hmm.. Sorry, lisud ko tabang nimo ron bro kay wala ko'y compiler diri.. Naa ko SG ron nangita work..

    Anyways, basin sa heirarchy sa imung GUI? Nag una ang uban objects, natabunan nuon ang scrollpane?

    Try to isolate sa ang codes nimo. Ayaw sa butangi og functions and etc.. Hinay2x og build from ground.. Usually this works for me..

    Sa netbeans man gud, automatic na xa. Try paghimo sa netbeans og java class, ayaw gui, aron ma.trace nimo kung unsa kulang or functions nga pwede sa imu gi.gagmit nga object..

  6. #6

    Default Re: Java JScrollPane Help. (mabuang nako)

    Try this bro..

    JList (Java Platform SE 6)

    JList doesn't implement scrolling directly. To create a list that scrolls, make it the viewport view of a JScrollPane. For example:

    JScrollPane scrollPane = new JScrollPane(myList);
    // Or in two steps:
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().setView(myList);

    Jlist man ang container sa imung unsorted array sa?

  7. #7

    Default Re: Java JScrollPane Help. (mabuang nako)

    Hayahaya ni stealthghost oi. SG na man ang tirada. Hehe. Ako intawn naa ra japon cebu. Huhu.

    Jairoh: pakit-a kono ko sa code gi unsa nimo pag butang ang setPreferredSize(null). Need pa man ka mo instantiate ug JScrollpane nga object ana.

    By the way Jairoh, I suggest to change this JList into TextArea. mas nindot man if Text Area imong gamiton para aning imong app...

    I suggest before after nimo initialize ang imong textarea, put this code below it.

    scrollPane = new JScrollPane();
    scrollPane.setViewportView(this.area);
    scrollPane.setPreferredSize(null);
    this.pane.add(scrollPane);
    Last edited by SuperStar; 08-20-2012 at 05:20 AM.

  8. #8
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java JScrollPane Help. (mabuang nako)

    @stealthghost doesn't work either bro. actually dili ni JFrame sa GUI nya drag and drop bro. class jud ni ako gihimo, scratch jud ni ako, nagtagbaw jud kog positioning ani, makita na nimo sako mga codes, kai katong built in sa netbeans na drag and drop man gud kai gi.taas2x ra ang codes nya di mabasa ang syntax. di ko nahan ato although ang JList ato kai with scroll na pero di mabasa.

    @superstar, i been to JtextArea bro pero wa japon, akong gbuhat balik pero wa japon, mas fit jud ang JList ani. Look at this ss na katong same nako na program pero lahi cya kai fixed na ang iyang unsorted array which is akong gi.usob( the program above). in.ani ta akong ganahan mahitabo. JList japon akong gamit ani,
    Imageshack - screenshotfrom201208191.pngBorderLayout center lng ni. mugana ang scroll niya.

    Uploaded with ImageShack.us
    Last edited by jairoh_; 08-20-2012 at 09:43 AM.

  9. #9

    Default Re: Java JScrollPane Help. (mabuang nako)

    Quote Originally Posted by SuperStar View Post
    Hayahaya ni stealthghost oi. SG na man ang tirada. Hehe. Ako intawn naa ra japon cebu. Huhu.

    Jairoh: pakit-a kono ko sa code gi unsa nimo pag butang ang setPreferredSize(null). Need pa man ka mo instantiate ug JScrollpane nga object ana.

    By the way Jairoh, I suggest to change this JList into TextArea. mas nindot man if Text Area imong gamiton para aning imong app...

    I suggest before after nimo initialize ang imong textarea, put this code below it.

    scrollPane = new JScrollPane();
    scrollPane.setViewportView(this.area);
    scrollPane.setPreferredSize(null);
    this.pane.add(scrollPane);
    OT:
    lisud kaayu diri bai, padung na hurot akong money, wa pa ko'y interview..
    1 month jud dapat sige apply diri, i'm on my 2nd week and my plan is 2 weeks
    unta naa na manawag kay kung wala pa, extend jud ko next week, I might have any luck.

    pinaka lisud diri ang travel and kaun basta wala'y work..

    @Jairoh, unsa d.i imu gi-gamit nga container for your app? naa man gud na heirarchy ang containers, Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Btw, ang kanang private Container pane;, mura'g naglibug ko para asa na? try JPanel?
    Nangita ko sa Container API, pero mura'g naa pa xa sa AWT nga side..

  10. #10
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java JScrollPane Help. (mabuang nako)

    Quote Originally Posted by stealthghost View Post
    OT:
    lisud kaayu diri bai, padung na hurot akong money, wa pa ko'y interview..
    1 month jud dapat sige apply diri, i'm on my 2nd week and my plan is 2 weeks
    unta naa na manawag kay kung wala pa, extend jud ko next week, I might have any luck.

    pinaka lisud diri ang travel and kaun basta wala'y work..

    @Jairoh, unsa d.i imu gi-gamit nga container for your app? naa man gud na heirarchy ang containers, Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Btw, ang kanang private Container pane;, mura'g naglibug ko para asa na? try JPanel?
    Nangita ko sa Container API, pero mura'g naa pa xa sa AWT nga side..
    kanang link imo ghatag bro kai para borderlayout mana, and di na ma.apply sa ako program(katong first post nako sa page 1, katong 1st screenshot), ang akong need ra ato kai no changes na, scrollpane rajud sa JList. kai kung mugamit ko ana borderlayout, mahimo cyag in ani ImageShack® - Online Photo and Video Hosting which is matabunan ang mga buttons and other txtfields, so di jud applicable ang borderlayout. murag complex ra au ang JPanel para ani na program bro.

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. mga master help need nako answers ninyo karon
    By cuteboy_kahoy in forum Computer Hardware
    Replies: 7
    Last Post: 08-05-2011, 03:17 PM
  2. Replies: 3
    Last Post: 03-05-2011, 09:14 AM
  3. Need help in JAVA plsss.. help 25problems..
    By FrozenBoi in forum Programming
    Replies: 89
    Last Post: 02-09-2010, 02:26 PM
  4. Replies: 11
    Last Post: 01-26-2010, 12:51 AM
  5. Java programming help pls...
    By mr_gwen in forum Programming
    Replies: 6
    Last Post: 11-03-2006, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top