I am making a GUI Console with Java and Swing. It calls on the scanner to input text, then puts that text into a variable to give to the JFrame. The bolded code is the code in question.
The code works, but when you type in "change text", after putting in the required input, it does not add the JLabel to the JFrame.
Please Help! Thanks!
//prepare all imports
Random rand = new Random();
Scanner input = new Scanner(System.in);
JFrame myframe = new JFrame();
myframe.setSize(300, 300);
myframe.setTitle("Blank Window");
myframe.setResizable(false);
myframe.setLocation(300,300);
//*******************************************
boolean done = false;
boolean winopen = false;
System.out.println("Type commands here. Type 'help' for list of commands.");
while (done == false) {
System.out.print("Console > > > ");
String coninput = input.nextLine();
if (coninput.equals("window open")) {
System.out.println("Opening Window...");
System.out.println("Done!");
winopen = true;
myframe.setVisible(true);
}
if (coninput.equals("window close")) {
System.out.println("Closing Window...");
winopen = false;
myframe.setVisible(false);
System.out.println("Done!");
}
if (coninput.equals("exit")) {
System.out.println("Exiting...");
myframe.dispose();
done = true;
System.out.println("Done!");
}
if (coninput.equals("help")) {
System.out.println("Commands: ");
System.out.println("window open: opens a window");
System.out.println("window close: closes the open window");
System.out.println("exit: shuts down the program");
System.out.println("help: lists commands");
//System.out.println("");
//System.out.println("");
//System.out.println("");
}
**if (coninput.equals("change text") && winopen == true) {
System.out.print("What do you want the text to say > > > ");
JLabel text1 = new JLabel(input.nextLine());
System.out.println("Adding...");
myframe.add(text1);
}**
if (coninput.equals("change text") && winopen == false) {
System.out.print("You have to have a window open.");
}
}
} }
First of, you need to structure your code in a better way. Secondly, it is better to use a swing container where the text (JLabel
) will be added. Below Box is used, which is a container that uses BoxLayout as its layout manager, allowing multiple components (JLabels
in your case) to be laid out either vertically (Y_AXIS
) or horizontally (X_AXIS
). You can then use JScrollPane
to provide a scrollable view of the Box component, and add that JScrollPane instance to your frame. Every time you add a new text, you add it to the Box instance, and then you call repaint();
and revalidate();
on your frame for the text to be shown. If you need only the last text to be shown on the window (and not every text you have added), then uncomment box.removeAll();
as a quick fix. Otherwise, do not use the Box with the JScrollPane, but simply add the label to your frame e.g., myframe.getContentPane().add(lbl, BorderLayout.CENTER);
. Again, remember to call myframe.getContentPane().removeAll()
; before adding a new JLabel, as well as repaint();
and revalidate();
afterwards. Working example below:
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
public class App {
Scanner input;
JFrame myframe;
Box box;
JScrollPane scrollPane;
public App() {
input = new Scanner(System.in);
box = new Box(BoxLayout.Y_AXIS);
scrollPane = new JScrollPane(box);
myframe = new JFrame();
myframe.getContentPane().add(scrollPane, BorderLayout.CENTER);
myframe.setSize(300, 300);
myframe.setTitle("Blank Window");
myframe.setResizable(false);
myframe.setLocation(300, 300);
}
public void go() {
boolean done = false;
boolean winopen = false;
System.out.println("Type commands here. Type 'help' for list of commands.");
while (done == false) {
System.out.print("Console > > > ");
String coninput = input.nextLine();
if (coninput.equals("window open")) {
System.out.println("Opening Window...");
System.out.println("Done!");
winopen = true;
myframe.setVisible(true);
}
if (coninput.equals("window close")) {
System.out.println("Closing Window...");
winopen = false;
myframe.setVisible(false);
System.out.println("Done!");
}
if (coninput.equals("exit")) {
System.out.println("Exiting...");
myframe.dispose();
done = true;
System.out.println("Done!");
}
if (coninput.equals("help")) {
System.out.println("Commands: ");
System.out.println("window open: opens a window");
System.out.println("window close: closes the open window");
System.out.println("exit: shuts down the program");
System.out.println("help: lists commands");
}
if (coninput.equals("change text") && winopen == true) {
System.out.print("What do you want the text to say > > > ");
JLabel lbl = new JLabel(input.nextLine());
System.out.println("Adding...");
// box.removeAll();
box.add(lbl);
myframe.repaint();
myframe.revalidate();
Rectangle bounds = lbl.getBounds();
scrollPane.getViewport().scrollRectToVisible(bounds);// scroll to the new text
}
if (coninput.equals("change text") && winopen == false) {
System.out.print("You have to have a window open.");
}
}
}
public static void main(String[] args) {
new App().go();
}
}