tags:

views:

258

answers:

3

The JScrollpane is not visible at the JTextArea... Can anybody find out what is wrong in my code?
And how to fix it?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.text.DateFormat; 



class PrimaryInterface extends JFrame implements ActionListener 
{ 
        ImageIcon iimages; 
        JLabel  jlimageContainer,jltime,jltitle,jlname1,jlname2; 
        JLabel jlnoValue; 
        JLabel jlnumberOfThreads, jlnameOfthreads,jlstatusOfThreads, jllane1; 
        JButton jbtstartSlideShow,jbtstopSlideShow; 
        Font ftimeFont,ftitleFont,fnameFont; 
        JProgressBar progbarMain,progbarLock; 
        JProgressBar progbarThread[] = new JProgressBar[10]; 
        JButton buttonGo; 
        JButton buttonStart; 
        JButton buttonStop; 
        JPanel panelMain,panelLocker,progPanel,sample;         
        JTextArea textArea; 
        JScrollPane scrPane; 
        JRadioButton radioButton[] = new JRadioButton[10]; 
        ButtonGroup buttonGroup; 
        int threadCount=1,targetThread=0; 
        firstTester ft[] = new firstTester[10]; 
        secondTester st[] = new secondTester[10]; 
        firstLocker first[] = new firstLocker[10]; 




        PrimaryInterface() 
        { 

                super("SLIDE SHOW"); 
                setLayout(null); 
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 

        } 

        public void paintImageContainer() 
        { 

               jlname1= new JLabel("THREAD 1"); 
                jlname2=new JLabel("THREAD 2"); 
                jltitle = new JLabel("DEADLOCK APPLICATION",JLabel.CENTER); 
                jltitle.setBounds(350,5,300,50); 

                jlimageContainer = new JLabel();         
                jltime = new JLabel(); 
                jbtstartSlideShow = new JButton("StartShow");                 
                jbtstopSlideShow = new JButton("StopShow"); 
                jlnoValue = new JLabel();         

                ftimeFont = new Font("Arial",1,40); 
                fnameFont = new Font("Arial",1,16); 
                ftitleFont = new Font("Constantia",1,25);         

                jbtstartSlideShow.setBounds(40,300,100,25); 
                jbtstopSlideShow.setBounds(220,300,100,25); 



                jlimageContainer.setBounds(80,80,200,200); 
                jlimageContainer.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
                jlimageContainer.setText("SLIDESHOW AREA"); 
                jltime.setBounds(500,160,300,100); 
                jltime.setForeground(Color.blue); 
                jlname1.setFont(fnameFont); 
                jlname2.setFont(fnameFont); 
                jltime.setFont(ftimeFont); 
                jltitle.setFont(ftitleFont); 
                jlname1.setBounds(10,30,100,30); 
                jlname2.setBounds(500,130,100,30); 
                jltitle.setForeground(Color.red); 
                jlimageContainer.setText("SLIDESHOW AREA"); 

               textArea = new JTextArea(180, 30); 
    scrPane=new JScrollPane(textArea); 


                new DigitalWatch(jltime,jlnoValue); 
                new SlideShow(iimages,jlimageContainer,jbtstartSlideShow,jbtstopSlideShow,jlnoValue); 

    jlnumberOfThreads = new JLabel("Number of Threads:"); 
                jlnameOfthreads = new JLabel(); 
                jlstatusOfThreads = new JLabel("Thread Status:"); 
                jllane1 = new JLabel(); 


                jllane1.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
                jllane1.setBounds(20,340,830,4); 

                jlnameOfthreads.setBorder(BorderFactory.createEtchedBorder(Color.BLACK,Color.WHITE)); 
                jlnameOfthreads.setBounds(15,560,830,140); 


                jlnumberOfThreads.setBounds(20,560,200,20); 
                jlnoValue.setBounds(250,560,200,20); 

                jlstatusOfThreads.setBounds(20,580,200,20); 



                panelMain = new JPanel(new GridLayout(1,1)); 
                panelLocker = new JPanel(new GridLayout(1,1)); 
                progPanel = new JPanel(new GridLayout(10,2)); 





                //panelMain.setLayout(null); 
                //panelLocker.setLayout(null);                 



                panelMain.setBounds(500,260,180,30); 
                panelLocker.setBounds(20,400,180,30); 
                progPanel.setBounds(250,350,300,200); 



                buttonStart = new JButton("START"); 
                buttonStop = new JButton("DEADLOCK"); 

                buttonGroup = new ButtonGroup(); 
                progbarMain = new JProgressBar();                 


                //panelMain.setLayout(null); 
                //panelLocker.setLayout(null); 

                buttonStart.setBounds(600,400,100,30); 
                buttonStop.setBounds(600,450,100,30); 
                textArea.setBounds(50,600,750,80); 
                textArea.setLineWrap(true); 

                panelMain.add(progbarMain); 

                progbarMain.setIndeterminate(true); 
                progbarMain.setStringPainted(true); 
                progbarMain.setString("Main Thread"); 


                progbarLock = new JProgressBar();                 
                progbarLock.setIndeterminate(true); 
                progbarLock.setStringPainted(true); 
                progbarLock.setString("DeadLock Thread"); 
                panelLocker.add(progbarLock); 



                scrPane.setAutoscrolls(true); 





                buttonStop.setEnabled(false); 



                //setContentPane(panelMain); 

                //new PrimaryInterface(); 



                add(buttonStart); 
                add(buttonStop); 

                add(textArea); 

                buttonStart.addActionListener(this); 
                buttonStop.addActionListener(this); 





                add(jlname1); 
                add(jlname2); 
                add(jltitle); 
                add(jlimageContainer); 
                add(jbtstartSlideShow); 
                add(jbtstopSlideShow); 
                add(jltime);                 
                add(jlnumberOfThreads); 
                add(jlnameOfthreads); 
                add(jllane1); 
                add(jlstatusOfThreads); 
                add(jlnoValue); 


                add(progPanel); 
                add(panelMain); 
                add(panelLocker); 


                setSize(900,730); 
                setVisible(true); 
                this.getContentPane().setBackground(Color.yellow.darker()); 

        } 
public void createProgress(int tNum) 
        { 

                radioButton[tNum] = new JRadioButton("Thread "+String.valueOf(tNum)); 
                buttonGroup.add(radioButton[tNum]); 
                progbarThread[tNum] = new JProgressBar(); 
                if(tNum!=0) 
                { 
                progPanel.add(radioButton[tNum]);         
                progPanel.add(progbarThread[tNum]); 
                } 
                radioButton[tNum].addActionListener(this); 

                buttonStop.setEnabled(false); 
                progbarThread[tNum].setIndeterminate(true); 
                progbarThread[tNum].setStringPainted(true); 
                progbarThread[tNum].setString("Thread "+tNum); 
        } 

        public void actionPerformed(ActionEvent ae) 
        { 
                String command = ae.getActionCommand(); 

                if(command.equals("START")) 
                {         

                        textArea.append("Creating a new thread : Thread "+threadCount+"\n"); 
                        ft[threadCount] = new firstTester(); 
                        st[threadCount] = new secondTester(); 

                        first[threadCount] = new firstLocker(String.valueOf(threadCount),ft[threadCount],st[threadCount],false,this); 
                        threadCount++; 

                } 

                if(command.equals("DEADLOCK")) 
                { 
                        textArea.append("Creating Deadlock thread : Thread 0\n"); 
                        progbarThread[targetThread].setIndeterminate(false); 
                        progbarThread[targetThread].setValue(0); 
                        progbarLock.setIndeterminate(false); 
                        progbarLock.setValue(0); 

                        first[targetThread] = new firstLocker("0",ft[targetThread],st[targetThread],true,this); 
                        secondLocker second = new secondLocker("0",ft[targetThread],st[targetThread],this); 
                        radioButton[targetThread].setEnabled(false); 
                        progbarThread[targetThread].setString("Thread "+targetThread+" in deadlock"); 
                }         

                for(int actionCounter = 0;actionCounter<=10;actionCounter++) 
                { 
                        if(command.equals("Thread "+actionCounter)) 
                        { 
                                targetThread = actionCounter; 
                                textArea.append("Thread "+actionCounter+" selected.\n"); 
                                buttonStop.setEnabled(true); 
                                progbarLock.setIndeterminate(true); 
                                progbarLock.setString("DeadLock Thread"); 
                        } 
                }         

        } 

} 


class SlideShow implements Runnable,ActionListener 
{         

        ImageIcon iimages; 
        JLabel jlimageContainer,jlnoValue; 
        JButton jbtstartSlideShow,jbtstopSlideShow; 

        Thread t1; 
        String path="h:\\images"; 

        int i = 0; 
        int j = 0; 



        SlideShow(ImageIcon images,JLabel imageContainer,JButton startSlideShow,JButton stopSlideShow, JLabel noValue) 
        { 

                iimages = images; 
                jlimageContainer = imageContainer; 
                jlnoValue = noValue; 
                jbtstartSlideShow = startSlideShow; 
                jbtstopSlideShow = stopSlideShow; 

                jbtstartSlideShow.addActionListener(this); 
                jbtstopSlideShow.addActionListener(this); 

        } 





        public void actionPerformed(ActionEvent ae) 
        { 
                if(ae.getSource() == jbtstartSlideShow) 
                { 
                        t1 = new Thread(this); 
                        t1.start(); 

                        jbtstartSlideShow.setEnabled(false); 
                } 

                if(ae.getSource() == jbtstopSlideShow) 
                { 
                        t1.stop(); 
                        jbtstartSlideShow.setEnabled(true); 
                } 
        } 

        public void startSlideShow() 
        { 

                try 
                { 
                        for(i=1;i<=5;i++) 
                        { 
                                iimages = new ImageIcon(path+"\\"+i+".jpg"); 
                                jlimageContainer.setIcon(iimages); 
                                t1.sleep(2000); 

                        } 
                } 
                catch(Exception e) 
                { 
                }         
        } 

        public void run() 
        { 
                        jlnoValue.setText((1+(Thread.currentThread().activeCount()-4)/2.0)+""); 
                        jlimageContainer.setText("LOADING..."); 


                        for(j=1;j<=2;j++) 
                        { 

                                startSlideShow();         

                                if(j==2) 
                                        j=0; 
                        }                                                         


        } 

} 

class DigitalWatch implements Runnable 
{ 

        static Thread t2; 
        JLabel labelTime,jlnoValue; 
        String sformattedTime=""; 


        DigitalWatch(JLabel objL,JLabel noValue) 
        { 
                labelTime = objL;         
                jlnoValue = noValue; 
                t2 = new Thread(this);         

        } 

        public static void showWatch() 
        { 
                t2.start(); 


        } 

        public void run() 
        { 
                jlnoValue.setText("1");                 
                for(;;) 
                { 
                        Date rawTime=new Date(); 
                        sformattedTime = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(rawTime); 

                        labelTime.setText(sformattedTime); 

                        System.out.println(sformattedTime);                                 

                        try{ 
                                t2.sleep(1000); 
                        } 
                        catch(Exception e) 
                        { 
                        } 
                } 

        } 

} 



//Synchronised Test method 1 
class firstTester{ 

        static int fCount; 
        boolean flagLock; 
        firstTester() 
        { 

                System.out.println("Test Class 1 started"); 
        } 
        synchronized void test1(int tNum,secondTester second,boolean flagLocker,PrimaryInterface pInterface)
        { 
                //pInterface.textArea.append("Thread :"+tNum+" count :"+fCount+"\n"); 
                flagLock = flagLocker; 
                try{ 

                Thread.sleep(150); 
                }catch(Exception e){} 
                fCount++; 

                if(flagLock) 
                { 
                        pInterface.textArea.append("Creating Dead lock...."); 
                        second.test2(tNum,this,flagLock,pInterface); 
                } 

        } 
} 


//Synchronised Test method 2 

class secondTester{ 

        boolean flagLock; 
        secondTester() 
        { 

                System.out.println("Test Class 2 started"); 
        } 

        synchronized void test2(int tNum,firstTester first,boolean flagLocker,PrimaryInterface pInterface) 
        { 
                flagLock = flagLocker; 
                try{ 
                Thread.sleep(100); 
                }catch(Exception e){} 

                if(flagLock) 
                { 
                        first.test1(tNum,this,flagLock,pInterface); 
                } 

        } 
} 



class firstLocker implements Runnable{ 
        Thread t; 
        String tName; 
        int tNum; 
        firstTester first; 
        secondTester second; 
        boolean flagReady = false; 
        PrimaryInterface pInterface; 
        firstLocker(String name,firstTester first,secondTester second,boolean flagLock,PrimaryInterface pInterface) 
        { 
                flagReady = flagLock; 
                System.out.println("Starting Thread"+name); 
                this.first = first; 
                this.second = second; 
                this.pInterface = pInterface; 
                tName = name; 
                tNum = Integer.parseInt(tName); 

                        pInterface.createProgress(tNum); 


                t = new Thread(this); 
                t.start(); 
        } 

        public void run() 
        { 
                while(true) 
                { 

                        first.test1(tNum,second,flagReady,pInterface); 
                        second.test2(tNum,first,flagReady,pInterface); 
                } 
        } 
} 


class secondLocker implements Runnable{ 
        Thread t; 
        String tName; 
        int tNum; 
        firstTester first; 
        secondTester second; 
        PrimaryInterface pInterface; 
        secondLocker(String name,firstTester first,secondTester second,PrimaryInterface pInterface) 
        { 
                System.out.println("Starting Thread"+name); 
                this.first = first; 
                this.second = second; 
                this.pInterface = pInterface; 
                tName = name; 
                tNum = Integer.parseInt(tName); 

                t = new Thread(this); 
                t.start(); 
        } 

        public void run() 
        { 
                second.test2(tNum,first,true,pInterface); 
        } 
} 


class Caution 
{         

        public static void main(String args[]) 
        { 

                PrimaryInterface pInterface = new PrimaryInterface(); 


                pInterface.paintImageContainer(); 
                DigitalWatch.showWatch(); 

        } 

}
+2  A: 

Too long; didn't read! :-)

You seem to be adding the textarea to the parent container. You should be adding the scrollpane instead:

You have:

 add(textArea);

Try

add(new JScrollPane(textArea));

Advice for solving these sort of problems:

Create the smallest possible test class you can that demonstrates the problem. Something like:

public class TestFrame { 
    public static void main(String args[]) {
         JFrame frame = new JFrame("Test");

         // add some stuff to the frame 
         frame.add(...); 

         frame.pack(); 
         frame.setVisible(true); 
    } 
}

Often while creating such a minimal test class, I stumble upon the solution. But in any case, its much for us to spot the problem in small code.

Steve McLeod
Sound advice that.
banjollity
+2  A: 

You instantiate the JTextArea and the JScrollPane correctly, and bind them together properly, but then you add the JTextArea directly to the PrimaryInterface. Try adding the JScrollPane instead.

In brief it should be like this:

JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView( textArea );
add( scrollPane );

I notice that you don't use a LayoutManager. Maybe you should use a LayoutManager instead of setting all the bounds yourself. This will reduce instances of unpredictable behaviour, and make your UI behave properly under conditions like the Frame being resized.

banjollity
i tried.. textarea too disappears in tat case..
+1  A: 

As has been pointed out, once you add the textArea to the scrPane, you should then add the scrPane to the container. You should not forget to layout the scrPane as well.

There are two things that you need to change:

textArea.setBounds(50,600,750,80);

should be:

scrPane.setBounds(50,600,750,80);

and

add(textArea);

should be:

add(scrPane);
akf
hey grt help!! thx a lot..