Reputation:
So, I'm attempting to add in a JScrollPane to a JTextArea so that I can scroll down, but no matter what I do, the pane does not show up. Am I in violation of some Swing stuff, or just a fail? Also, this is for a school project so I really need a definitive answer as quickly as possible. Thanks! Here's the code for the class implementing the JScrollPane:
package BLANK.historicalcontext;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import BLANK.historicalcontext.gui.ConsoleJTextArea;
import BLANK.historicalcontext.keys.ConsoleKeyListener;
import BLANK.historicalcontext.story.Backstory;
import BLANK.historicalcontext.story.Story;
public class Window extends JFrame {
public static ConsoleJTextArea console = new ConsoleJTextArea();
public static JTextField inputField = new JTextField();
public static Story currentStory;
public static Font font = new Font("Avenir-Light", Font.PLAIN, 12);
private void addComponentsToPane() {
setLayout(null);
Border border = BorderFactory.createLineBorder(Color.BLACK);
inputField.addKeyListener(new ConsoleKeyListener());
console.setEditable(false);
console.setBackground(new Color(230, 230, 250));
console.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
console.setFont(font);
inputField.setFont(font);
Insets insets = getContentPane().getInsets();
JScrollPane consoleScrollPane = new JScrollPane(console);
consoleScrollPane.setPreferredSize(new Dimension(20 + insets.left, 800 - 20 + insets.top));
getContentPane().add(inputField);
getContentPane().add(consoleScrollPane);
console.setBounds(10 + insets.left, 10 + insets.top, 800 - 20, 600 - 65);
inputField.setBounds(7 + insets.left, (600 - 70) + 20 + insets.top, 800 - 14, 20);
}
private static void createAndShowGUI() {
Window frame = new Window("Historical Context Project");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.addComponentsToPane();
Insets insets = frame.getInsets();
frame.setSize(800 + insets.left + insets.right, 600 + insets.top + insets.bottom);
frame.setVisible(true);
inputField.requestFocusInWindow();
}
public Window(String name) {
super(name);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
console.printToConsole("UnsupportedLookAndFeelException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (IllegalAccessException ex) {
console.printToConsole("IllegalAccessException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (InstantiationException ex) {
console.printToConsole("InstantiationException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (ClassNotFoundException ex) {
console.printToConsole("ClassNotFoundException caught: \n" + ex.getStackTrace().toString() + "\n");
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
console.append("Hello! This is a text-based game about the creation of the creation of Java.");
console.printToConsole("Your name is Patrick Naughton.");
console.printToConsole("Here is a bit of backstory:");
currentStory = new Backstory();
if(currentStory != null) {
currentStory.executeStory();
}
}
public static void failGame() {
console.printToConsole("You failed the game. This application will close once you click the \"Enter\"/\"Return\" key.");
}
}
I imagine you'll also want to see the class that the console is based off of:
package nate.historicalcontext.gui;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class ConsoleJTextArea extends JTextArea {
public void printToConsole(String message) {
this.append("\n"+ message);
}
}
Also, please don't refer me to another question because no two questions are completely alike and my situation may differ from the author of that question's.
Upvotes: 0
Views: 41
Reputation: 347332
static
. static
is not a means for cross object communication. Provide methods which allow you to access the properties of the object that other objects need. Try and be protective of your data/fieldsnull
layouts. You don't control many aspects of the rendering process which can change the requirements of individual components and affect their relationship to other components.console
to the contentPane
AFTER it was previously assigned to the JScrollPane
. A component can only reside within a single container, when you add it to another container, it is removed from the current container firstHave a closer look at How to use scrollpanes for more details
JScrollPane consoleScrollPane = new JScrollPane(console);
//...
// Add the scroll pane...
getContentPane().add(consoleScrollPane);
// Add the console, which WAS on the scrollpane to the content pane
getContentPane().add(console);
Your use of null
layouts is not helping you...
private void addComponentsToPane() {
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(panel);
setLayout(new BorderLayout());
Border border = BorderFactory.createLineBorder(Color.BLACK);
// inputField.addKeyListener(new ConsoleKeyListener());
console.setEditable(false);
JScrollPane consoleScrollPane = new JScrollPane(console);
console.setBackground(new Color(230, 230, 250));
console.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
console.setFont(font);
inputField.setFont(font);
getContentPane().add(inputField, BorderLayout.SOUTH);
getContentPane().add(consoleScrollPane);
// getContentPane().add(console);
Insets insets1 = getContentPane().getInsets();
// console.setBounds(10 + insets1.left, 10 + insets1.top, 800 - 20, 600 - 65);
// inputField.setBounds(7 + insets1.left, (600 - 70) + 20 + insets1.top, 800 - 14, 20);
}
Upvotes: 2