This is a split board - You can return to the Split List for other boards.

Java Coding Help needed - Please help if you can...

  • Topic Archived
You're browsing the GameFAQs Message Boards as a guest. Sign Up for free (or Log In if you already have an account) to be able to post messages, change how messages are displayed, and view media in posts.
  1. Boards
  2. PC
  3. Java Coding Help needed - Please help if you can...

User Info: GeminiX7

GeminiX7
1 month ago#1
So below is a some code that will pull up a window with a text field at the top, and two text windows below. When you type into the field, the text will also appear in real-time on the bottom window, and when entered, the text will then be logged in the top window. Our eventual goal for the assignment is to have the words logged into the top window to also be logged in the bottom, but in reverse order, so that the two windows appeared mirrored. The bottom window should also still show the text field's typing in real time. So far, I've gotten it to do everything except display the entries in reverse order on the bottom window, and I'm stumped as to what I should do. Note: When I say "reverse order" I mean if I typed "Hello", then "Goodbye", on the top window it would say "Hello" on the first line, and "Goodbye" on the second line, but on the bottom window it should say "Goodbye" on the first line, and "Hello" on the second line and so forth.

import java.awt.*;
import java.awt.event.*;

public class TextFrame extends Frame{

public TextFrame(){

setSize(500,500);
setTitle("Text Frame");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
add(new TextPanel(), BorderLayout.CENTER);
setVisible(true);

}

public static void main(String [] args){
new TextFrame();
}

class TextPanel extends Panel implements ActionListener, TextListener{

TextField tf;
TextArea ta1, ta2;
Panel centerPanel;

public TextPanel(){
tf = new TextField();
ta1 = new TextArea();
ta2 = new TextArea();
centerPanel = new Panel();
centerPanel.setLayout(new GridLayout(0,1));
centerPanel.add(ta1);
centerPanel.add(ta2);
setLayout(new BorderLayout());
add(tf, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);

tf.addActionListener(this);
tf.addTextListener(this);
}

public void actionPerformed(ActionEvent ae){
String temp = tf.getText();
String temp2 = ta1.getText();
String result = temp + "\n" + temp2;
ta1.setText(result);
tf.setText("");
}
public void textValueChanged(TextEvent te){
String temp = tf.getText();
ta2.setText(temp);
String temp3 = ta1.getText();
String result = temp + "\n" + temp3;
ta2.setText( result);
}

}

}
MSI Z97| Intel i5-4690 @ 3.5 Ghz | 8GB Cosair Vengence @ 1600 |Radeon R9 390 8GB
Who gave you permission to talk to me, mongrel?

User Info: CELTEKK

CELTEKK
1 month ago#2
You came here for advice on this? ... lol
transcend8/23/15

User Info: GeminiX7

GeminiX7
1 month ago#3
CELTEKK posted...
You came here for advice on this? ... lol

I've seen people help with coding issues on this board in the past.
MSI Z97| Intel i5-4690 @ 3.5 Ghz | 8GB Cosair Vengence @ 1600 |Radeon R9 390 8GB
Who gave you permission to talk to me, mongrel?

User Info: CELTEKK

CELTEKK
1 month ago#4
GeminiX7 posted...
CELTEKK posted...
You came here for advice on this? ... lol

I've seen people help with coding issues on this board in the past.


Me too. Doesn't mean this is the place to go. You'd be better off at stackoverflow.
transcend8/23/15

User Info: ThisIsAKnoife

ThisIsAKnoife
1 month ago#5
GeminiX7 posted...
CELTEKK posted...
You came here for advice on this? ... lol

I've seen people help with coding issues on this board in the past.

i saw a single guy do this in the past, and he got shooed away after like his 5th question. fact is, this is a pc gaming board, hence GAMEfaqs. there are much better places for these questions, such as stackoverflow as someone already suggested.
AFK: Attack, fight, kill!! The healer is telling you to go pull mobs.
WTF: Way to fight! The healer is applauding your tactical genius.

User Info: ElDudorino

ElDudorino
1 month ago#6
In the actionPerformed method, instead of this: ta1.setText(result);

You could do this: ta1.append(temp + "\n");

That will put the text at the beginning instead of the end for ta1.

Problem is, in the textValueChanged method, you're pulling the text directly from ta1 and setting that in ta2, so anything you change in ta1 will also be changed in ta2. There might be a smarter fix for this but an easy hack is just to create a third, undisplayed text area named ta3 and then use *that* as your buffer for ta2 instead of using ta1.

Other than the two lines to declare and construct ta3, I only had to modify 3 lines in the actionPerformed method and 1 line in the textValueChanged method to get the proper result, assuming I understood correctly what you wanted to do: The top will read like 1 2 3 4 5 and the bottom will read like 5 4 3 2 1, so they're mirrors.

User Info: GeminiX7

GeminiX7
1 month ago#7
ElDudorino posted...
In the actionPerformed method, instead of this: ta1.setText(result);

You could do this: ta1.append(temp + "\n");

That will put the text at the beginning instead of the end for ta1.

Problem is, in the textValueChanged method, you're pulling the text directly from ta1 and setting that in ta2, so anything you change in ta1 will also be changed in ta2. There might be a smarter fix for this but an easy hack is just to create a third, undisplayed text area named ta3 and then use *that* as your buffer for ta2 instead of using ta1.

Other than the two lines to declare and construct ta3, I only had to modify 3 lines in the actionPerformed method and 1 line in the textValueChanged method to get the proper result, assuming I understood correctly what you wanted to do: The top will read like 1 2 3 4 5 and the bottom will read like 5 4 3 2 1, so they're mirrors.

Thank you very much. I will attempt this when I return home from the office and tell you how it works out. The third text area might be the best choice, as my professor did say we'd need to "think outside the box" to find the right solution. He tries to be clever like that sometimes.
MSI Z97| Intel i5-4690 @ 3.5 Ghz | 8GB Cosair Vengence @ 1600 |Radeon R9 390 8GB
Who gave you permission to talk to me, mongrel?

User Info: GeminiX7

GeminiX7
1 month ago#8
ThisIsAKnoife posted...
GeminiX7 posted...
CELTEKK posted...
You came here for advice on this? ... lol

I've seen people help with coding issues on this board in the past.

i saw a single guy do this in the past, and he got shooed away after like his 5th question. fact is, this is a pc gaming board, hence GAMEfaqs. there are much better places for these questions, such as stackoverflow as someone already suggested.

Fair enough, but I thought I might as well ask in multiple places, and like I've said before, I've seen solid answers given(and someone has given one in this topic already) for coding questions. I think the main thing is that you don't ask people to do your homework for you entirely. That kind of questioning ends up getting shut down, and I've personally been one of the people wielding the swatter.
MSI Z97| Intel i5-4690 @ 3.5 Ghz | 8GB Cosair Vengence @ 1600 |Radeon R9 390 8GB
Who gave you permission to talk to me, mongrel?

User Info: ElDudorino

ElDudorino
1 month ago#9
No problem... if it doesn't work, show your code here and I'll tell you what's wrong with it. I think you should be able to figure it out pretty quickly though since you're already 90% there.

By the way, I don't think this is required for your assignment but it will look nicer if you put the work for actionPerformed inside of if (temp.matches("[a-zA-Z0-9]+"))
{
(your work here)
}

This will check to make sure that there's actual text in tf before it executes any of the changes, so you can't make the boxes ugly by hitting enter when there's only blank spaces in the textfield.

User Info: GeminiX7

GeminiX7
1 month ago#10
ElDudorino posted...
No problem... if it doesn't work, show your code here and I'll tell you what's wrong with it. I think you should be able to figure it out pretty quickly though since you're already 90% there.

By the way, I don't think this is required for your assignment but it will look nicer if you put the work for actionPerformed inside of if (temp.matches("[a-zA-Z0-9]+"))
{
(your work here)
}

This will check to make sure that there's actual text in tf before it executes any of the changes, so you can't make the boxes ugly by hitting enter when there's only blank spaces in the textfield.

Well, I'm avoiding if statements only because we haven't used them in the class so far. It's my second Java class, so we are expected to know them, but it's my first Java Applications course, so I don't know if there is supposed to be some method of solving the issue using only the methods and the like shown in the code.
MSI Z97| Intel i5-4690 @ 3.5 Ghz | 8GB Cosair Vengence @ 1600 |Radeon R9 390 8GB
Who gave you permission to talk to me, mongrel?
  1. Boards
  2. PC
  3. Java Coding Help needed - Please help if you can...

Report Message

Terms of Use Violations:

Etiquette Issues:

Notes (optional; required for "Other"):
Add user to Ignore List after reporting

Topic Sticky

You are not allowed to request a sticky.

  • Topic Archived