I got a little problem which occupies me for hours.
I want the player to make an input during the game that I will then further use. But I don't know how to do this...
Tried JOptionPane, JTextField and Scanner. Scanner worked, but I want it without the use of the console :I
So, here's my code:
Window:
package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Window
{
public static JFrame frame = new JFrame("Z-Stories");
public static JLabel Label = new JLabel ("<html></html>", JLabel.CENTER);
public static String LabelText;
public Window()
{
Label.setVerticalAlignment(JLabel.TOP);
frame.setSize(1920, 1080);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(Label, BorderLayout.CENTER);
frame.setIconImage(new ImageIcon(getImage()).getImage());
frame.pack();
frame.setVisible(true);
}
protected static Image getImage()
{
java.net.URL imgURL = Window.class.getResource("Logo32.png");
if (imgURL != null)
{
return new ImageIcon(imgURL).getImage();
} else
{
return null;
}
}
public static void addText(String Text)
{
LabelText = Label.getText();
LabelText = LabelText.replace("</html>", "");
if(Text != null)
{
Label.setText(LabelText + "<br/>" + Text + "</html>");
}else
{
Label.setText(LabelText + "<br/><br/></html>");
}
System.out.println(Label.getText());
Label.validate();
}
public static int InputInt()
{
//User Input here
//Maybe parse into Int
return output;
}
public static String InputText()
{
//User Input here
//Maybe convert to String
return outputText;
}
}
And the Game.java
...
public void StartGame()
{
ErstesSpiel = 1;
Window.addText("Wähle deine Sprache | Select your language");
Window.addText("");
Window.addText("Deutsch (1) | English (2)");
Window.addText("");
int var3 = Window.InputInt();
Window.addText("");
....
You can use a JTextField and get input when the user presses enter by using an actionlistener or by a JButton click
Create this object
JTextField UserInputField = new JTextField("");
Call this method when user presses enter with an Action Listener (more on Action Listeners)
public static int InputInt()
{
String sInput = UserInputField.getText(); //Gets the string from the JTextField
int output = Integer.parseInt(sInput.trim()); //Parse string to int
return output; //Return as int
}
If what you want is for the method to auto-run whenever the user inputs a number, you can use a keyboard listener
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Related
I have two different FileMenuHandler's for a GUI, I need a way to use the data stored in the TreeMap from FileMenuHadler in EditMenuHandler. EditMenuHandler is supposed to ask the user to enter a word and search in the TreeMap if the word exists.
I tried to create an instance of FMH in EMH but the Tree was always empty, how can I save the values of the tree once the file is opened and then use it for EditMenuHandler?
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileMenuHandler implements ActionListener{
JFrame jframe;//creating a local JFrame
public FileMenuHandler (JFrame jf){//passing WordGUI Jframe
jframe = jf;
}
private Container myContentPane;
private TextArea myTextArea1;
private TextArea myTextArea2;
protected ArrayList<Word> uwl = new ArrayList<Word>();
protected TreeMap<Word, String> tree;
private void readSource(File choosenFile){
String choosenFileName = choosenFile.getName();
TextFileInput inFile = new TextFileInput(choosenFileName);
myContentPane = jframe.getContentPane();
myTextArea1 = new TextArea();
myTextArea2 = new TextArea();
myTextArea1.setForeground(Color.blue);
myTextArea2.setForeground(Color.blue);
Font font = new Font("Times", Font.BOLD, 20);
myTextArea1.setFont(font);
myTextArea2.setFont(font);
myTextArea1.setBackground(Color.yellow);
myTextArea2.setBackground(Color.yellow);
String paragraph = "";
String line = inFile.readLine();
while(line != null){
paragraph += line + " ";
line = inFile.readLine();
}
StringTokenizer st = new StringTokenizer(paragraph);
tree = new TreeMap<Word,String>();
while(st.hasMoreTokens()){
String word = st.nextToken();
Word w = new Word(word);
uwl.add(w);
tree.put(w,w.data);
}
for(int i = 0; i < uwl.size(); i++){
myTextArea1.append(uwl.get(i).data + "\n");
}
myTextArea2.append(tree + "\n");
myContentPane.add(myTextArea1);
myContentPane.add(myTextArea2);
jframe.setVisible(true);
}
private void openFile(){
int status;
JFileChooser chooser = new JFileChooser("./");
status = chooser.showOpenDialog(null);
readSource(chooser.getSelectedFile());
}
//instance of edit menu handler
public void actionPerformed(ActionEvent event) {
String menuName = event.getActionCommand();
if (menuName.equals("Open")){
openFile();
}
else if (menuName.equals("Quit")){
System.exit(0);
}
} //actionPerformed
}
//
import java.awt.event.*;
public class EditMenuHandler implements ActionListener {
JFrame jframe;
public EditMenuHandler(JFrame jf) {
jframe = jf;
}
public void actionPerformed(ActionEvent event) {
String menuName = event.getActionCommand();
if (menuName.equals("Search")) {
JOptionPane.showMessageDialog(null, "Search");
}
}
}
there are many ways to do this,
you can declare a static filed (not recommended)
use RXJava or LiveData
use EventBus
use interface as a listener
....
I'm working on a simple GUI project to get the foundations of Java Swing.
I created a Rock Paper Scissors game which you play against the computer which i implemented a GUI for
My text based GUI Form for Rock, Paper,Scissors
My problem is that once either my score or the computers score reach a value of 3, i want the text on the frame to change. I've tried to implement the code to check each time the variable increases in the button function but it still does not works, neither does implementing the function in the main, the game doesn't change any text or stop once the scores reach 3.
chooseRock.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String cc = getCompChoice();
if (cc.equalsIgnoreCase("Paper")) {
compcount++;
if(compcount == 3){
winner.setText("Computer Wins");
}
compChoice.setText("Computers Choice: " + cc);
This code shows the GUI object and the listener for selecting "Rock", the code is the same for both paper and Scissors. Both compchoice and playchoice are declared with the other attributes at the top.
gameScreen(String title){
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(gameBoard);
this.pack();
chooseRock.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String cc = getCompChoice();
if (cc.equalsIgnoreCase("Paper")) {
compcount++;
compChoice.setText("Computers Choice: " + cc);
compScore.setText("Computer Score: " + compcount);
} else if (cc.equalsIgnoreCase("Scissors")) {
playcount++;
compChoice.setText("Computers Choice: " + cc);
playerScore.setText("Your Score: " + playcount);
} else {
compChoice.setText("Computers Choice: " + cc + " Its a DRAW!");
}
}
});
This is a function I've written to check the scores and display the winner, the 'winner' text is displayed at the top of the panel and has a placeholder.
public void checkScore(){
if(playcount == 3 ){
winner.setText("GAME OVER - PLAYER WINS");
chooseRock.setEnabled(false);
chooseScissors.setEnabled(false);
choosePaper.setEnabled(false);
}else if(compcount == 3 ){
winner.setText("GAME OVER - COMPUTER WINS! BETTER LUCK NEXT TIME");
chooseRock.setEnabled(false);
chooseScissors.setEnabled(false);
choosePaper.setEnabled(false);
}
}
Is there any way to take the variable written inside the listener and use it to change the text field or is there some way conditions like this should be implemented?
You should call checkScore() within the ActionListeners actionPerformed method so that the score is calculated and acted upon with each button press.
Interesting aside:
One way to simplify your listeners is to create an enum, one that checks for win, something like:
public enum RPS {
ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors");
private String text;
private RPS(String text) {
this.text = text;
}
public String getText() {
return text;
}
#Override
public String toString() {
return text;
}
This way, your check for win can be as simple as adding a comparing method to the enum:
// returns 1 for win, -1 for loss, 0 for tie
public int compare(RPS other) {
int length = values().length;
int delta = (length + ordinal() - other.ordinal()) % length;
return delta != 2 ? delta : -1;
}
}
Each enum value has an ordinal() method that returns its order of declaration, 0 for ROCK, 1 for PAPER, and 2 for SCISSORS. The delta equation simply gets the difference between these ordinal values, adds 3 (the "size" of the enum) and checks the remainder of the calculation. If it is equal to 1, then that enum "wins" the battle, if 0, then its a tie and if 2, then this enum "loses". This can simplify your listeners.
For example, here I create a single listener class for all buttons:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
#SuppressWarnings("serial")
public class RockPaperScissors extends JPanel {
public static final int MAX_SCORE = 3;
private static final int PANEL_WIDTH = 600;
private int myScore;
private int compScore;
private JLabel statusLabel = new JLabel(" ");
private JLabel myChoiceLabel = new JLabel();
private JLabel compChoiceLabel = new JLabel();
private JLabel myScoreLabel = new JLabel();
private JLabel compScoreLabel = new JLabel();
private List<Action> actionList = new ArrayList<>();
public RockPaperScissors() {
JPanel statusPanel = new JPanel();
statusPanel.setBorder(BorderFactory.createTitledBorder("Status"));
statusPanel.add(statusLabel);
JPanel scorePanel = new JPanel();
scorePanel.setBorder(BorderFactory.createTitledBorder("Score"));
scorePanel.add(new JLabel("My Score:"));
scorePanel.add(myScoreLabel);
scorePanel.add(Box.createHorizontalStrut(15));
scorePanel.add(new JLabel("Comp Score:"));
scorePanel.add(compScoreLabel);
JPanel selectionPanel = new JPanel();
selectionPanel.setBorder(BorderFactory.createTitledBorder("Selections"));
selectionPanel.add(new JLabel("My Choice:"));
selectionPanel.add(myChoiceLabel);
selectionPanel.add(Box.createHorizontalStrut(15));
selectionPanel.add(new JLabel("Comp Choice:"));
selectionPanel.add(compChoiceLabel);
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 3, 0));
for (RPS rps : RPS.values()) {
Action action = new ButtonAction(rps);
actionList.add(action);
JButton button = new JButton(action);
btnPanel.add(button);
}
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(statusPanel);
add(scorePanel);
add(selectionPanel);
add(btnPanel);
}
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int height = superSize.height;
int width = Math.max(superSize.width, PANEL_WIDTH);
return new Dimension(width, height);
}
private class ButtonAction extends AbstractAction {
private RPS rps;
public ButtonAction(RPS rps) {
super(rps.getText());
this.rps = rps;
}
#Override
public void actionPerformed(ActionEvent e) {
int randomValue = (int) (RPS.values().length * Math.random());
RPS compChoice = RPS.values()[randomValue];
myChoiceLabel.setText(rps.getText());
compChoiceLabel.setText(compChoice.getText());
if (rps.compare(compChoice) > 0) {
statusLabel.setText("I Win");
myScore++;
} else if (rps.compare(compChoice) < 0) {
statusLabel.setText("Computer Wins");
compScore++;
} else {
statusLabel.setText("Draw");
}
myScoreLabel.setText(String.valueOf(myScore));
compScoreLabel.setText(String.valueOf(compScore));
if (myScore >= MAX_SCORE) {
statusLabel.setText("I Win the Game");
} else if (compScore >= MAX_SCORE) {
statusLabel.setText("Computer Wins the Game");
}
if (myScore >= MAX_SCORE || compScore >= MAX_SCORE) {
for (Action action : actionList) {
action.setEnabled(false);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
RockPaperScissors mainPanel = new RockPaperScissors();
JFrame frame = new JFrame("RockPaperScissors");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
enum RPS {
ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors");
private String text;
private RPS(String text) {
this.text = text;
}
public String getText() {
return text;
}
#Override
public String toString() {
return text;
}
public int compare(RPS other) {
int length = values().length;
int delta = (length + ordinal() - other.ordinal()) % length;
return delta != 2 ? delta : -1;
}
}
I am trying to write a GUI temperature converter. It has one JTextField and two JButtons. TextField accepts the temperature which the user wants to convert and the user presses the appropriate button. Whenever I click on anyone of the buttons, I get a "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String" error. Please Help!
public class tempcon extends JFrame {
private JPanel panel;
private JLabel messageLabel;
public JTextField tempC;
private JButton calcButton, calcButton1;
private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 140;
public tempcon() {
setTitle("Temperature convertion");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
public double getTempC(){
return Double.parseDouble(tempC.getText());
}
private void buildPanel() {
tempC = new JTextField(10);
messageLabel = new JLabel("Enter tempurture");
calcButton = new JButton("Convert to Fahrenheit");
calcButton1 = new JButton("Convert to Celcius");
calcButton.addActionListener(new CalcButtonListener());
calcButton1.addActionListener(new CalcButtonListener1());
panel = new JPanel();
panel.add(messageLabel);
panel.add(tempC);
panel.add(calcButton);
panel.add(calcButton1);
}
public static void main(String[] args){
new tempcon().buildPanel();
}
}
class CalcButtonListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
double input;
double temp;
input = new tempcon().getTempC();
temp = input * 1.8 + 32;
JOptionPane.showMessageDialog(null, "That is " + temp + "
degrees Celsius.");
}
}
class CalcButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double input;
double temp;
input = new tempcon().getTempC();
temp = (input - 32)*1.8;
JOptionPane.showMessageDialog(null, "That is " + temp + "
degrees Fehrenheit.");
}
public static void main(String[] args) {
tempcon myTempWindowInstance = new tempcon();
}
}
The problem is that you are recreating a new frame in your action listeners : new tempcon().getTempC() .
The textfields in these new frames are obviously empty and you get your error.
Consider referring to the same instance of tempcon everywhere, that is simply replace
new tempcon().getTempC();
with
getTempC();
, which will call the getTempC() method of the outer tempcon instance .
I am trying to create an java/swing based application which shows weather. So far I have created background and textfield + button to get the location but I do not know how to connect it so it shows and changes the background to another image. I am sorry if that's a noob question, but I never did java before (just processing and arduino plus web design) and my uni forced me to use advanced java with knowledge I never did anything like that before.
Here is my code so far:
package AppPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ApplicationWidget extends JFrame implements ActionListener {
ImageIcon basic;
JLabel label1;
JFrame frame;
JLabel label;
JTextField textfield;
JButton button;
public static void main (String[]args){
ApplicationWidget gui = new ApplicationWidget();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(320, 480);
}
public ApplicationWidget() {
setLayout(new FlowLayout());
WeatherAPI weather = new WeatherAPI("44418");
System.out.println(WeatherAPI.theWeatherRSS);
for(int i = 0; i < WeatherAPI.weatherForecastList.size(); i++)
{
System.out.println(WeatherAPI.weatherForecastList.get(i).lowTemp + " " +
WeatherAPI.weatherForecastList.get(i).highTemp);
}
label = new JLabel("Welcome! Please Enter your location");
add(label);
textfield = new JTextField(15);
add(textfield);
for(int i = 0; i < WeatherAPI.weatherForecastList.size(); i++)
{
System.out.println(WeatherAPI.weatherForecastList.get(i).lowTemp + " " +
WeatherAPI.weatherForecastList.get(i).highTemp);
}
button = new JButton("Check weather");
add(button);
basic = new ImageIcon(getClass().getResource("basicback.jpg"));
label1 = new JLabel(basic);
add(label1);
/*add design here*/
/*add mouse interaction*/
/*add image capture*/
}
#Override
public void actionPerformed(ActionEvent e){
JButton button = (JButton) e.getSource();
if (e.getSource() == button){
String data = textfield.getText();
System.out.println(data);
}
}
}
And the WeatherAPI code:
package AppPackage;
import java.net.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.io.*;
public class WeatherAPI
{
static String theWeatherRSS;
static String theCity;
static ArrayList<Forecast> weatherForecastList;
//WeatherAPI(String string) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
public class Forecast
{
String lowTemp;
String highTemp;
}
/**
*
* #param city
*/
public WeatherAPI(String city)
{
theCity = city;
theWeatherRSS = getWeatherAsRSS(city);
parseWeather(theWeatherRSS);
}
void parseWeather(String weatherHTML)
{
weatherForecastList = new ArrayList<Forecast>();
int startIndex = 0;
while(startIndex != -1)
{
startIndex = weatherHTML.indexOf("<yweather:forecast", startIndex);
if(startIndex != -1)
{ // found a weather forecast
int endIndex = weatherHTML.indexOf(">", startIndex);
String weatherForecast = weatherHTML.substring(startIndex, endIndex+1);
// get temp forecast
String lowString = getValueForKey(weatherForecast, "low");
String highString = getValueForKey(weatherForecast, "high");
Forecast fore = new Forecast();
fore.lowTemp = lowString;
fore.highTemp = highString;
weatherForecastList.add(fore);
// move to end of this forecast
startIndex = endIndex;
}
}
}
String getValueForKey(String theString, String keyString)
{
int startIndex = theString.indexOf(keyString);
startIndex = theString.indexOf("\"", startIndex);
int endIndex = theString.indexOf("\"", startIndex+1);
String resultString = theString.substring(startIndex+1, endIndex);
return resultString;
}
String getWeatherAsRSS(String city)
{
try{
/*
Adapted from: http://stackoverflow.com/questions/1381617/simplest-way-to-correctly-load-html-from-web-page-into-a-string-in-java
Answer provided by: erickson
*/
URL url = new URL("http://weather.yahooapis.com/forecastrss?w="+city+"&u=c");
URLConnection con = url.openConnection();
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and
* hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
int ch = r.read();
if (ch < 0)
break;
buf.append((char) ch);
}
String str = buf.toString();
return(str);
}
catch(Exception e) {System.err.println("Weather API Exception: "+e);}
return null;
}
}
Thanks for any help, I am truly desperate because I have mixed up the dates of submission and I have not much time left....
Assuming label1 is your background label, just use label1.setIcon(...). What you will pass to it is a new ImageIcon
Also you haven't registered the ActionListener to your button. If you don't register a listener to the button, it won't do anything. Do this
button = new JButton("Check weather");
button.addActionListener(this);
add(button);
You haven't specified where the new image is coming from, so I really can't help you any further than this.
I am calling a method that returns a string (edited text from a webpage) and I want to print that string onto my JTextArea. I know that string I am sending to my JTextArea is correct because it will print correctly to the command line, but will not print to the JTextArea. It must be something I am doing wrong in my adding it to the TextArea. Any help would be appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BrowserPanel extends JPanel {
private JTextField textField;
private String urlText;
private JTextArea textArea;
private BrowserPageReader myModel;
private String pageContent;
private BrowserFrame myFrame;
private String pageTitle;
private String pageBody;
public BrowserPanel(JTextField myTextField, BrowserPageReader model,
BrowserFrame frame)
{
myFrame = frame;
myModel = model;
textField = myTextField;
textField.addActionListener(new InputHandler());
/*JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250,250));*/
textArea = new JTextArea(20,40);
textArea.setEditable(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textArea);
add(scroll);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
private class InputHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
urlText = textField.getText();
//textArea.append(urlText);
myModel.setURL(urlText);
pageTitle = myModel.getTitle();
myFrame.setTitle(pageTitle);
pageBody = myModel.getBody();
textArea.setText(pageBody);
System.out.println(pageBody); //This prints out exactly what Im wanting
// Its just a test
textArea.repaint();
}
}
}
I'm guessing I maybe need to add something to my paintComponent since my TextArea is in a scrollPane that is attached to my Panel. I just really cant figure out what is wrong. If i put textArea.setText("blah"); it does what it should. The variable I am sending in is a very large string, its an entire webpage. Could that be the problem? With the code as is the textArea remains blank and what i'm wanting it to show prints correctly to the command line. HELP!
Edit here is the rest of my code
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class BrowserFrame extends JFrame{
public BrowserFrame()
{
BrowserPageReader myModel = new BrowserPageReader();
setTitle("My Browser");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize =kit.getScreenSize();
setSize(screenSize.width/2,screenSize.height-500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JTextField textField = new JTextField(20);
BrowserPanel myPanel = new BrowserPanel(textField,myModel,this);
contentPane.add(myPanel);
contentPane.add(textField,BorderLayout.PAGE_START);
setVisible(true);
}
}
import javax.swing.*;
import java.io.*;
import java.net.*;
public class BrowserPageReader {
private URL myURL;
//private String webURL;
private String totalWebContent;
private String htmlString;
private String contentToPrint = " ";
private String urlPath;
private String urlHost;
private String pageTitle;
private String pageBody;
private String formattedBody;
public void setURL (String webURL)
{
try{
myURL = new URL(webURL);
urlPath = myURL.getPath();
urlHost = myURL.getHost();
}
catch(MalformedURLException e)
{
JOptionPane.showMessageDialog(null,"URL is incorrectly formatted");
}
}
public void retrieveContent()
{
try{
Socket socket = new Socket(urlHost,80);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new
BufferedReader(new InputStreamReader(socket.getInputStream()));
out.print("GET " + urlPath + " HTTP/1.1\n");
out.print("Host: "+ urlHost + "\n");
out.print("\n");
out.flush();
while((totalWebContent = in.readLine()) != null)
{
//System.out.println(totalWebContent);
htmlString = htmlString + totalWebContent;
//System.out.println(contentToPrint);
}
//System.out.println("htmlString\n" + htmlString);
}
catch(Exception e){
e.printStackTrace();
}
}
public String getTitle()
{
retrieveContent();
//System.out.println(htmlString);
pageTitle = htmlString.substring(htmlString.indexOf("<title>")+ 7,
htmlString.indexOf("</title>"));
return pageTitle;
}
public String getBody()
{
String toDelete;
String edited;
retrieveContent();
pageBody = htmlString.substring(htmlString.indexOf("<body")+5,
htmlString.indexOf("</body>"));
toDelete = pageBody.substring(0,pageBody.indexOf('<'));
edited = pageBody.replace(toDelete,"");
pageBody = edited
formattedBody = pageBody.replaceAll("<[^>]*>", "");
//System.out.println(formattedBody);
return formattedBody;
}
Since your posted code is not an SSCCE, a small self-contained program that we can compile, run, and test, I don't think that we answer this without guessing. And so my guess: the JTextArea that you're adding text to is not the same one as is being displayed in a JFrame.
To be able to answer this with confidence though, we need that SSCCE, especially the code showing where you create the class above and where you add it to the JFrame that is displayed.
For instance, if I create a small SSCCE with mock BrowserFrame JFrame and BrowserPageReader model classes, everything seems to work fine:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BrowserPanel extends JPanel {
private JTextField textField;
private String urlText;
private JTextArea textArea;
private BrowserPageReader myModel;
private String pageContent;
private BrowserFrame myFrame;
private String pageTitle;
private String pageBody;
public BrowserPanel(JTextField myTextField, BrowserPageReader model,
BrowserFrame frame) {
myFrame = frame;
myModel = model;
textField = myTextField;
textField.addActionListener(new InputHandler());
/*
* JScrollPane areaScrollPane = new JScrollPane(textArea);
* areaScrollPane.setVerticalScrollBarPolicy(
* JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
*
* areaScrollPane.setPreferredSize(new Dimension(250,250));
*/
textArea = new JTextArea(20, 40);
textArea.setEditable(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textArea);
add(scroll);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
private class InputHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
urlText = textField.getText();
// textArea.append(urlText);
myModel.setURL(urlText);
pageTitle = myModel.getTitle();
myFrame.setTitle(pageTitle);
pageBody = myModel.getBody();
textArea.setText(pageBody);
System.out.println(pageBody); // This prints out exactly what Im
// wanting
// Its just a test
textArea.repaint();
}
}
private static void createAndShowGui() {
BrowserFrame frame = new BrowserFrame();
JTextField textField = new JTextField(10);
BrowserPageReader myModel = new BrowserPageReader();
BrowserPanel mainPanel = new BrowserPanel(textField, myModel, frame);
frame.add(textField, BorderLayout.NORTH);
frame.add(mainPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class BrowserFrame extends JFrame {
}
class BrowserPageReader {
public void setURL(String urlText) {
// does nothing for now. for testing purposes.
}
public String getBody() {
return "body"; // for testing purposes
}
public String getTitle() {
return "title"; // for testing purposes
}
}
Since my code "works" it proves that the error is not in the code you've posted above.
Your job is to post similar code that doesn't work fine, that instead demonstrates your problem. I'm guessing that if you put in the effort to create such a program, you'll isolate the error, you'll see where you've likely got two BrowserPanels, one displayed and one that is not displayed but is getting its text changed in the handler, and you'll be able to solve your error without our direct help.
Edit
SwingWorker e.g.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
public class BrowserPanel extends JPanel {
private JTextField textField;
private String urlText;
private JTextArea textArea;
private BrowserPageReader myModel;
private String pageContent;
private BrowserFrame myFrame;
private String pageTitle;
private String pageBody;
public BrowserPanel(JTextField myTextField, BrowserPageReader model,
BrowserFrame frame) {
myFrame = frame;
myModel = model;
textField = myTextField;
textField.addActionListener(new InputHandler());
/*
* JScrollPane areaScrollPane = new JScrollPane(textArea);
* areaScrollPane.setVerticalScrollBarPolicy(
* JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
*
* areaScrollPane.setPreferredSize(new Dimension(250,250));
*/
textArea = new JTextArea(20, 40);
textArea.setEditable(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textArea);
add(scroll);
}
private class InputHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
urlText = textField.getText();
// textArea.append(urlText);
System.out.println(urlText);
myModel.setURL(urlText);
myModel.getContent(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (SwingWorker.StateValue.DONE == evt.getNewValue()) {
pageTitle = myModel.getTitle();
myFrame.setTitle(pageTitle);
pageBody = myModel.getBody();
textArea.setText(pageBody);
System.out.println(pageBody);
}
}
});
// textArea.repaint();
}
}
private static void createAndShowGui() {
BrowserFrame frame = new BrowserFrame();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class BrowserFrame extends JFrame {
public BrowserFrame() {
BrowserPageReader myModel = new BrowserPageReader();
setTitle("My Browser");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
setSize(screenSize.width / 2, screenSize.height - 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JTextField textField = new JTextField(20);
BrowserPanel myPanel = new BrowserPanel(textField, myModel, this);
contentPane.add(myPanel);
contentPane.add(textField, BorderLayout.PAGE_START);
setVisible(true);
}
}
class BrowserPageReader {
private URL myURL;
// private String webURL;
private String totalWebContent;
private String htmlString;
private String contentToPrint = " ";
private String urlPath;
private String urlHost;
private String pageTitle;
private String pageBody;
private String formattedBody;
public void setURL(String webURL) {
try {
myURL = new URL(webURL);
urlPath = myURL.getPath();
urlHost = myURL.getHost();
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "URL is incorrectly formatted");
}
}
public void getContent(PropertyChangeListener listener) {
RetrieveWorker worker = new RetrieveWorker();
worker.addPropertyChangeListener(listener);
worker.execute();
}
private void retrieveContent() {
try {
Socket socket = new Socket(urlHost, 80);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out.print("GET " + urlPath + " HTTP/1.1\n");
out.print("Host: " + urlHost + "\n");
out.print("\n");
out.flush();
while ((totalWebContent = in.readLine()) != null) {
// System.out.println(totalWebContent);
htmlString = htmlString + totalWebContent;
// System.out.println(contentToPrint);
}
// System.out.println("htmlString\n" + htmlString);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTitle() {
// !! retrieveContent();
System.out.println(htmlString);
pageTitle = htmlString.substring(htmlString.indexOf("<title>") + 7,
htmlString.indexOf("</title>"));
return pageTitle;
}
public String getBody() {
String toDelete;
String edited;
// !! retrieveContent();
pageBody = htmlString.substring(htmlString.indexOf("<body") + 5,
htmlString.indexOf("</body>"));
toDelete = pageBody.substring(0, pageBody.indexOf('<'));
edited = pageBody.replace(toDelete, "");
pageBody = edited;
formattedBody = pageBody.replaceAll("<[^>]*>", "");
// System.out.println(formattedBody);
return formattedBody;
}
private class RetrieveWorker extends SwingWorker<Void, Void> {
#Override
protected Void doInBackground() throws Exception {
retrieveContent();
return null;
}
}
}
I bet your input handler is getting called multiple times. The text could be set to the body text, then print, then be set back to empty for some reason. Test this in your system.out.println statment by adding
System.out.println("outputStart: " + pagebody + " :END");
Then you'll be able to tell how many times your input handler ran.