Reputation: 335
If I have the scrollbar, the JTextArea just vanishes. Without it, everything is fine. Can someone please explain this to me? And how to fix it!
Also, as an aside, is it possible to change the color or set a border on the scrollpane?
Here is the relevant code:
//Text Container
JPanel textCon = new JPanel();
textCon.setOpaque(false);
textCon.setLayout(new GridLayout(1,3));
add(textCon);
//Left Filler
JPanel left = new JPanel();
left.setOpaque(false);
textCon.add(left);
//Text area
mainText = new JTextArea("SAMPLE");
mainText.setOpaque(true);
mainText.setSize(50,30);
mainText.setLineWrap(true);
mainText.setWrapStyleWord(true);
textCon.add(mainText);
//Set textAre fonts, colors, border/padding
mainText.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GREEN), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
mainText.setFont(new Font("sansserif", Font.BOLD, 10));
mainText.setForeground(Color.GREEN);
mainText.setBackground(Color.BLACK);
//Scroll Bar
scroller = new JScrollPane(mainText);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainText.add(scroller);
And here is the rest of it, just in case!
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
class ProjectMain extends JFrame
{
//Declare String/Array for mainText
String output = "";
String [] hero;
int page = 0;
JTextArea mainText;
JScrollPane scroller;
public ProjectMain()
{
//Set Background
setTitle("JLA Viewer");
setSize(1920,1080);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setContentPane(new JLabel(new ImageIcon("bg.png")));
setLayout(new GridLayout(3,1));
//Refresh Background
setSize(1919,1079);
setSize(1920,1080);
//Label Container
JPanel labelCon = new JPanel();
labelCon.setOpaque(false);
labelCon.setLayout(new BorderLayout());
add(labelCon);
//Top Label
JLabel topLabel = new JLabel("JLA Profile Viewer");
topLabel.setHorizontalAlignment(SwingConstants.CENTER);
topLabel.setForeground(Color.GREEN);
topLabel.setOpaque(false);
topLabel.setFont(new Font("sansserif", Font.BOLD, 30));
labelCon.add(BorderLayout.NORTH,topLabel);
//Logo
ImageIcon logo = new ImageIcon("logo.jpg");
JLabel logoLabel = new JLabel();
logoLabel.setHorizontalAlignment(SwingConstants.CENTER);
logoLabel.setIcon(logo);
labelCon.add(BorderLayout.SOUTH,logoLabel);
//Text Container
JPanel textCon = new JPanel();
textCon.setOpaque(false);
textCon.setLayout(new GridLayout(1,3));
add(textCon);
//Left Filler
JPanel left = new JPanel();
left.setOpaque(false);
textCon.add(left);
//Text area
mainText = new JTextArea("SAMPLE");
mainText.setOpaque(true);
mainText.setSize(50,30);
mainText.setLineWrap(true);
mainText.setWrapStyleWord(true);
textCon.add(mainText);
//Set textAre fonts, colors, border/padding
mainText.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GREEN), BorderFactory.createEmptyBorder(10, 10, 10, 10)));
mainText.setFont(new Font("sansserif", Font.BOLD, 10));
mainText.setForeground(Color.GREEN);
mainText.setBackground(Color.BLACK);
//Scroll Bar
scroller = new JScrollPane(mainText);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainText.add(scroller);
//Right Filler
JPanel right = new JPanel();
right.setOpaque(false);
textCon.add(right);
//Button Container
JPanel buttonContainer = new JPanel();
buttonContainer.setOpaque(false);
add(BorderLayout.SOUTH,buttonContainer);
//PREV Button
JButton prev = new JButton("PREV");
prev.setOpaque(false);
ActionListener PrevList = new PrevButton(); //Call Button Listener
prev.addActionListener(PrevList);
buttonContainer.add(prev);
//EXIT Button
JButton exit = new JButton("EXIT");
exit.setOpaque(false);
ActionListener ExitList = new ExitButton(); //Call Button Listener
exit.addActionListener(ExitList);
buttonContainer.add(exit);
//NEXT Button
JButton next = new JButton("NEXT");
next.setOpaque(false);
ActionListener NextList = new NextButton(); //Call Button Listener
next.addActionListener(NextList);
buttonContainer.add(next);
//File Handling
try
{
File inputFile = new File ("ProjectInputFile.txt");
Scanner scanner = new Scanner(inputFile);
while(scanner.hasNextLine())
{
output = output + (scanner.nextLine() + "\n");
}
}
catch(IOException ioe)
{
}
//Add split strings to array
hero = output.split("@");
}
//Prev button event listener
public class PrevButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
page = page - 1;
if(page < 0)
{
page = hero.length;
}
mainText.setText(hero[page]);
}
}
//Exit button event listener
public class ExitButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
System.exit(0);
}
}
//Next button event listener
public class NextButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
page++;
if(page > (hero.length))
{
page = 0;
}
mainText.setText(hero[page]);
}
}
public static void main(String args[])
{
new ProjectMain();
}
}
Upvotes: 0
Views: 1446
Reputation: 1
You just need to setBounds(x,y,z,w)
to your JScrollPane
and not your JTextArea
. Then add your JSrcollPane
to your JFrame
of your JPanel
.
Upvotes: -1
Reputation: 159754
This line
mainText.add(scroller);
attempts to add a JScrollPane
to a JTextArea
. You have already set the ViewPortView
of the JScrollPane
so this line is unnecessary.
You will still need to add the JSrolllPane
:
textCon.add(scroller);
Also make sure to call JFrame#setVisible
after all components have been added.
setVisible(true);
is it possible to change the color or set a border on the scrollpane?
Sure. Instead of setting the border on the JTextArea
mainText
, set it on the JScrollPane
.
Side notes:
IOException
without showing the exception contentJFrames
are typically used directly rather than extendingUpvotes: 2