Scrollpane and TextArea pls help asap


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Scrollpane and TextArea pls help asap

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    Angry Scrollpane and TextArea pls help asap

    Hi i have coded a chatroom client that connects tot he server i am using a JtextArea with JscrollPane. The user inputs a sentence in the text field when they press enter it goes to the server and the server send it tot he client the client diplays it int he textarea ok the scroll bars are there but they dont scrol down for the latest entry of the user. Just think of any chat room powered by java on the net the user never has to scrold own for every new entry the chat is like auto scroll pls help , Thanks

    Below is some of the code
    text = new JTextArea(10,34); myPane.add(text);
    sp = new JScrollPane(text);
    sp.setPreferredSize(new Dimension (350,200));
    myPane.add(sp, BorderLayout.CENTER);
    botPanel = new JPanel();

  2. #2
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    Hi this might work but I have not tried it. Get the max value of the scrollbar then set the scrollbar value to the max value like this.

    sp.setValue(sp.getMaximum());

    Like i said I have not tried it out, let me know if it works.

  3. #3
    Join Date
    Oct 2005
    Posts
    107
    here's some code I wrote to display text in a chat box style...
    the scrollpane keeps focus on the bottom...


    Code:
    public class CustomGameClient extends JFrame {
    	final GameModel gameModel = new GameModel();
    	CustomGamePanel gamePanel;
    	ActionPanel actionPanel;
    	JTextPane chatPane;
    	Style regularStyle, systemStyle, nameStyle;
    	JTextField chatBox;
    	
    	public CustomGameClient(){
    		super("CGClient");
    		setLayout(null);
    		setSize(610, 595);
    		setResizable(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				
    		Container pane = getContentPane();
    		gamePanel = new CustomGamePanel(this, gameModel);
    		pane.add(gamePanel);
    		gamePanel.setLocation(5,5);
    		gamePanel.setBackground(Color.MAGENTA.darker().darker());
    		chatPane = new JTextPane();
    		JScrollPane scrollPane = new JScrollPane(chatPane);
    		pane.add(scrollPane);
    		chatPane.setEditable(false);
    		scrollPane.setBounds(5, 425, 345, 100);
    		chatPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    		//Set up styles.
    		Style defaultStyle = 
    			StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    
    		regularStyle = chatPane.addStyle(null, defaultStyle);
    		StyleConstants.setFontFamily(regularStyle, "SansSerif");
    
    		systemStyle = chatPane.addStyle(null, regularStyle);
    		StyleConstants.setForeground(systemStyle, Color.green.darker());
    		StyleConstants.setFontSize(systemStyle, 12);
    
    		nameStyle = chatPane.addStyle(null, regularStyle);
    		StyleConstants.setForeground(nameStyle, Color.blue);
    		StyleConstants.setBold(nameStyle, true);
    		
    		chatBox = new JTextField(){
    			 protected void processFocusEvent(FocusEvent fe) {
    			 	if (fe.getID() == FocusEvent.FOCUS_GAINED) {
    			 	selectAll();
    			 	} else if (fe.getID() == FocusEvent.FOCUS_LOST) {
    			 	select(0, 0);
    			 	}
    			 	super.processFocusEvent(fe);
    			 }
    		};
    		pane.add(chatBox);
    		chatBox.setText("enter text here");
    		chatBox.setBounds(5, 530, 345, 25);
    		chatBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    		chatBox.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent arg0) {
    					doChatEntered();		
    			}
    		});
    		
    		actionPanel = new ActionPanel();
    		pane.add(actionPanel);
    		actionPanel.setLocation(5, 372);
    		//actionPanel.setVisible(false);
    		//actionPanel.setBackground(Color.BLUE.darker());
    	}
    	/**
    	 * simply adds to chatArea now, but later will send to server
    	 */
    	protected void doChatEntered() {
    		String text = chatBox.getText();
    		chatBox.setText("");
    		if(text.length() < 1) return;
    		
    		//send to server eventually
    		appendLine("username:"+text);
    	}
    	
    	/**
    	 * Borrowed from = http://www.cs.nyu.edu/courses/fall00/G22.3033-002/http/Chatter.java
    	 * Along with the different style definitions. because I didn't feel
    	 * like looking into it to much just to get different colored text in
    	 * a textpane.
    	 * @param line
    	 */
    	public void appendLine(String line) {
    		try {
    			Document doc = chatPane.getDocument();
    			int end = doc.getLength();
    			line += "\n";
    
    			if (line.startsWith("["))
    				doc.insertString(end, line, systemStyle);
    			else {
    				int ci = line.indexOf(':');
    				if (ci < 0)
    					doc.insertString(end, line, regularStyle);
    				else {
    					doc.insertString(end, line.substring(0, ci+1), nameStyle);
    					doc.insertString(doc.getLength(), line.substring(ci+1), regularStyle);
    				}
    			}
    
    		} catch (BadLocationException e) {
    			// shouldn't be possible
    			e.printStackTrace();
    		}
    	}
    }
    Last edited by Joe Beam; 02-08-2006 at 10:05 PM.

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