0

Hi,
for some reason, my JScrollPane isn't working with mt JTextArea.
Probably the most important part:

JTextArea TextArea;
JScrollPane scroll;
TextArea = new JTextArea();
scroll = new JScrollPane(TextArea);
add(TextArea,BorderLayout.CENTER);
add(scroll,BorderLayout.LINE_START);

Whole code if you're interested in what I'm making:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Formatter;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class Main extends JFrame{
	JPanel icons;
	JTextArea TextArea;
	JScrollPane scroll;
	JMenuBar menuBar;
	JMenu File;
		JMenuItem New;
		JMenuItem Load;
		JMenuItem Save;
		JMenuItem SaveAs;
		JMenuItem Exit;
	JMenu Window;
	JMenu Help;
	
	JFileChooser NF;
	JFileChooser LF;
	JFileChooser SF;
	JFileChooser SFA;
	
	File NewFile;
	File LoadFile;
	File SaveFile;
	Formatter createNewFile;
	

	String filepath;
	
	@SuppressWarnings("unused")
	public static void main(String[] args){
		Main m = new Main();
	}
	public Main(){
		super("LFC Text");
		setSize(500,345);
		setLocation(40,50);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		menuBar.setBorderPainted(false);
		icons = new JPanel();
		add(icons,BorderLayout.PAGE_START);
		TextArea = new JTextArea();
		scroll = new JScrollPane(TextArea);
		TextArea.setLineWrap(true);
		add(TextArea,BorderLayout.CENTER);
		add(scroll,BorderLayout.LINE_END);
		File = new JMenu("File");
			New = new JMenuItem("New File");
			Load = new JMenuItem("Load File");
			Save = new JMenuItem("Save File");
			SaveAs = new JMenuItem("Save File As...");
			Exit = new JMenuItem("Exit");
		Window = new JMenu("Window");
			
		Help = new JMenu("Help");
			
		NF = new JFileChooser();
		LF = new JFileChooser();
		SF = new JFileChooser();
		SFA = new JFileChooser();
		
		menuBar.add(File);
			File.add(New);
			File.add(Load);
			File.add(Save);
			File.add(SaveAs);
			File.addSeparator();
			File.add(Exit);
		menuBar.add(Window);
			
		menuBar.add(Help);
			
		
		
		setVisible(true);
		class NewFileAction implements ActionListener{
			public void actionPerformed(ActionEvent e){
				try {
					makeNewFile(NF);
				} catch (FileNotFoundException e1) {
					
				}
			}
		}class LoadFileAction implements ActionListener{
			public void actionPerformed(ActionEvent arg0) {
				loadFile(LF);
			}
		}class SaveFileAction implements ActionListener{
			public void actionPerformed(ActionEvent e){
				saveFile(SF);
			}
		}class SaveFileAsAction implements ActionListener{
			public void actionPerformed(ActionEvent arg0) {
				saveFileAs(SFA);
			}
		}
		New.addActionListener(new NewFileAction());
		Load.addActionListener(new LoadFileAction());
		Save.addActionListener(new SaveFileAction());
		SaveAs.addActionListener(new SaveFileAsAction());
	}
	public void makeNewFile(JFileChooser fc) throws FileNotFoundException{
		int returnVal = fc.showDialog(this,"Create a new file");
		if(returnVal == JFileChooser.APPROVE_OPTION){
			NewFile = fc.getSelectedFile();
			try{
				createNewFile = new Formatter(NewFile.getAbsolutePath());
			}finally{
				JOptionPane.showMessageDialog(this,NewFile.getName()+" successfully created.");
				setTitle("LFC Text|"+NewFile.getName());
			}
		}
	}
	public void loadFile(JFileChooser fc){
		int returnVal = fc.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION){
			LoadFile = fc.getSelectedFile();
			try{
				TextArea.setText("");
				FileInputStream fstream = new FileInputStream(LoadFile.getAbsolutePath());
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String strline;
				while((strline = br.readLine())!= null){
					TextArea.setText(TextArea.getText()+strline+"\n");
					
				}
				TextArea.setText(TextArea.getText()+"\n\nLiverpool Football Club Is A Win");
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(this,e.getStackTrace());
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this,e.getStackTrace());
			}finally{
				JOptionPane.showMessageDialog(this,LoadFile.getName()+ " was successfully loaded");
				setTitle("LFC Text|"+LoadFile.getName());
			}
		}
	}
	public void saveFile(JFileChooser fc){
		if(filepath == null){
			int returnVal = fc.showDialog(this,"Save File");
			if(returnVal == JFileChooser.APPROVE_OPTION){
				try{
					File file = fc.getSelectedFile();
					filepath = file.getAbsolutePath();
					FileWriter fstream = new FileWriter(filepath);
					BufferedWriter write = new BufferedWriter(fstream);
					PrintWriter print = new PrintWriter(write);
					print.println(TextArea.getText());
					print.close();
					write.close();
					setTitle("LFC Text|"+ file.getAbsoluteFile());
				}catch(IOException e1){JOptionPane.showMessageDialog(this,"Failed to save.\nError: "+
						e1.getStackTrace());}
			}
		}else{
			try{
				FileWriter fstream = new FileWriter(filepath);
				BufferedWriter write = new BufferedWriter(fstream);
				PrintWriter print = new PrintWriter(write);
				print.println(TextArea.getText());
				print.close();
				write.close();
				setTitle("LFC Text|"+ filepath);
			}catch(IOException e1){JOptionPane.showMessageDialog(this,"Failed to save.\nError: "+
					e1.getStackTrace());}
		}
	}public void saveFileAs(JFileChooser fc){
		filepath = null;
		saveFile(fc);
	}
}

Thanks.

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by mangopearapples
0

I would omit the line:
add(TextArea,BorderLayout.CENTER)

Since you created the scroll with the TextArea as argument, you might want to add only the scroll:
add(scroll,BorderLayout.LINE_START)
Because the scroll already has the TextArea.

You can try that for starters.

0

P.S. It might, in the future, help to describe "don't work" precisely, as that is the singularly most useless problem description in the world.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.