Results 1 to 4 of 4
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with a scroll bar...should be working how it is coded

    Need to be able to place text in textarea and it should become scrollable if the text overflows

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class WordSearch implements ActionListener
    {
    	public WordSearch()
    	{
    		JFrame wordSearch = new JFrame("Word Search");//create frame 
    		wordSearch.setLayout(new BorderLayout());//set layout to borderlayout
    		wordSearch.setSize(500, 300);//set size of frame
    		wordSearch.setLocation(200, 200);//set where frame will populate on screen
    		
    		
    		JLabel label = new JLabel("Empty or Copy & Paste Text");//label for top panel
    		JButton clear = new JButton("Clear");//Add clear button for top panel
    		clear.setMnemonic('C');//set c as the mnemonic for clear
    		
    		JTextArea area = new JTextArea(10,40);//set a typable text area
    		area.setLineWrap(true);
          area.setWrapStyleWord(true);
    		JScrollPane jsp = new JScrollPane(area);//add scroll bar
    		jsp.setVisible(true);
    		
    		JTextArea search = new JTextArea(1,10);//set a typable search area
    		search.setVisible(true);
    		search.setEditable(true);//allows user to enter text
    		JButton search1 = new JButton("Search");//creates a search button
    		search1.setMnemonic('S');//set s as the mnemonic for search
    		
    		JLabel found2 = new JLabel("# Found");
    		JTextArea found = new JTextArea(1,5);//sets a found search area
    		found.setText("0");//set text field to 0
    		found.setVisible(true);
    		JButton reset = new JButton("Reset");//creates new button to reset the findings
    		reset.setMnemonic('R');
    		JButton exit = new JButton("Exit");//creates new button to exit program
    		exit.setMnemonic('x');
    		
    		JPanel jpNorth = new JPanel();//create a panel for the top
    		JPanel jpCenter = new JPanel();//create a center panel for text box
    		JPanel jpSouth = new JPanel();//create a panel for the bottom
    		
    		jpNorth.add(label);//places label in top panel
    		jpNorth.add(clear);//places button in top panel
    		
    		jpCenter.add(area);//places text box in center panel
    		jpCenter.add(jsp);//add scroll bar to center panel
    		
    		jpSouth.add(search);//places text box in bottom panel
    		jpSouth.add(search1);//places search button
    		jpSouth.add(found2);//label for found
    		jpSouth.add(found);//places found txt box in bottom panel
    		jpSouth.add(reset);//places reset button in bottom panel
    		jpSouth.add(exit);//places exit button in bottom panel
    		
    		wordSearch.add(jpNorth, BorderLayout.NORTH);//places top panel in north position
    		wordSearch.add(jpCenter, BorderLayout.CENTER);//places center panel in center position
    		wordSearch.add(jpSouth, BorderLayout.SOUTH);//places bottom panel in bottom postion
    		
    		JMenuBar mbar = new JMenuBar();//create menu bar
    		wordSearch.setJMenuBar(mbar);
    		JMenu file = new JMenu("File");//creates a file button on menu bar
    		JMenuItem resetAll = new JMenuItem("ResetAll");//create reset all option under file
    		resetAll.setMnemonic('A');
    		JMenuItem exit1 = new JMenuItem("Exit");//create exit option under file
    		exit1.setMnemonic('x');
    		file.add(resetAll);//add reset all to file option on menu bar
    		file.add(exit1);//add exit to file option on menu bar
    		mbar.add(file);//adds file option to menu bar
    		
    		exit.addActionListener(this);//adds action listener to the exit button
    		exit1.addActionListener(this);//adds action listener to the exit menu item
    		
    		wordSearch.setVisible(true);//allows the frame to be visible
    		
    		Searcher searchIT = new Searcher(search1, reset, exit, resetAll, exit1, area, search, found);//sets listner for search1 JButton
    		
    		wordSearch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//so program will close when x is clicked		
    	}
    	
    	public void actionPerformed(ActionEvent e)//sets an action to be performed for the exit options
    	{
    		if (e.getActionCommand().equals("Exit"))
    		{
    			System.exit(0);
    		}
    		else if (e.getActionCommand().equals("x"))
    		{
    			System.exit(0);
    		}
    	}
    	public static void main(String[] args)
    	{
    		WordSearch word = new WordSearch();
    	}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Need help with a scroll bar...should be working how it is coded

    Delete line 47 "jpCenter.add(area);//places text box in center panel"
    You just need to add the scrollpane to the panel.

    Offtopic: Why do you use a JTextArea for the search field? Would be a JTextField not better?

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with a scroll bar...should be working how it is coded

    The JTextField would definitely be better and will change to that for the search box, and I will remove line 47 and see how it works. Thank you eRaaaa

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with a scroll bar...should be working how it is coded

    it worked!

Similar Threads

  1. JAVA coded button not working on Sharepoint page
    By selmore in forum Java Applets
    Replies: 16
    Last Post: 04-02-2012, 09:10 PM
  2. Coded Life Lesson :)
    By MuslimCoder in forum Forum Lobby
    Replies: 7
    Last Post: 07-29-2009, 08:16 PM
  3. Coded Message... Help Please
    By maxpower1000sa in forum New To Java
    Replies: 2
    Last Post: 05-03-2009, 08:27 PM
  4. Color coded code
    By tim in forum Suggestions & Feedback
    Replies: 11
    Last Post: 06-29-2008, 09:35 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
  •