-
July 5th, 2009, 03:22 PM
#16
Re: Jlist help
Originally Posted by Shaken_Earth
Thanks for the advice. I'll keep it in mind for future projects because this project is very close to being done.
Famous last words. Don't stop for gas, we're nearly there
Could you please tell me how to fix the problem?
I don't know - what did you put into the list? Is that what you're casting it to? Are you quite sure you're pulling it out of the right list?
Post up the code around the error line (Phrasebot.java:619) and the lines where you add something to the list.
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
T. Cargill
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 6th, 2009, 08:54 AM
#17
Re: Jlist help
Yes, the famous last words .
Here's the code:
Code:
...
JList list;
...
list = new JList();
DefaultListModel dlm = new DefaultListModel();
item1 = new ListItem("ANV", funny1);
item2 = new ListItem("NAV", funny2);
item3 = new ListItem("AVN", funny3);
item4 = new ListItem("VAN", funny4);
for(int i = 0; i<1; i++){
// add a new ListItem containing display text and a new value
dlm.addElement(item1.getKey());
dlm.addElement(item2.getKey());
dlm.addElement(item3.getKey());
dlm.addElement(item4.getKey());
}
list.setModel(dlm);
...
class listListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
// get the selected item
ListItem item = (ListItem) list.getSelectedValue();
// get the value from the item and print it
p = item.getValue();
}else{
System.out.println("It prevented it =)!");
}
}
}
...
-
July 6th, 2009, 09:26 AM
#18
Re: Jlist help
I guess I should have insisted that you answer my previous questions: "what did you put into the list? Is that what you're casting it to?" - then you would have spotted the problem. It's always better to spot the problem yourself because it saves embarrassment and gives you that "D'oh!" moment that fixes the error in your mind for future reference. Still, never mind.
The fix is to put the ListItems themselves into the list model, and to give the ListItem class a toString() method that returns the display text (as in the code I so generously supplied previously)
That ought to work (famous last words...)
If you're not failing every now and again, it's a sign you're not doing anything very innovative...
W. Allen
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 7th, 2009, 03:07 PM
#19
Re: Jlist help
Thanks!
"But, there's one more thing..."
I want to have a default so everytime the user launches the Phrasebot they don't have to go into Arrangements... and choose what they want. So I give a value to "p" in the phrasemakerListener inner-class. So I tried changing the value of "p" by using the list. But when I have "p" with a default it won't change. When there's no default it changes. I thought the whole point of instance variables is state. Shouldn't I be able to <i> change </i> the state if there's a default? Here's the code:
Code:
//Phrasebot is property of shakenearth.com
//Code by Thomas Lisankie
//You may not use any of this code
//you may not distribute this program either
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.net.*;
public class Phrasebot implements ActionListener {
JTextArea midgui;
JButton phrasemaker;
Box boxleft;
JTextField nounbox1;
JTextField nounbox2;
JTextField nounbox3;
JTextField adjbox1;
JTextField adjbox2;
JTextField adjbox3;
JTextField verbbox1;
JTextField verbbox2;
JTextField verbbox3;
HashSet<String> nouns = new HashSet<String>();
HashSet<String> adjectives = new HashSet<String>();
HashSet<String> verbs = new HashSet<String>();
JList list;
JTextField savefield;
JButton save;
String p;
public static void main(String[] args){
try{
Phrasebot bot = new Phrasebot();
bot.go();
}catch(Error Ex){
Ex.printStackTrace();
}
}
public void go() {
JPanel pcenter = new JPanel();
JFrame frame = new JFrame("Phrasebot");//makes a new JFrame called "Phrasebot"
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets the default close operation to EXIT_ON_CLOSE
boxleft = new Box(BoxLayout.Y_AXIS);
JPanel bigpanel = new JPanel();//makes a new JPanel called bigpanel
bigpanel.setLayout(new BoxLayout(bigpanel,BoxLayout.Y_AXIS));//sets bigpanel's default layout to BoxLayout
JPanel spacer = new JPanel();//makes a new JPanel called spacer
spacer.setLayout(new BoxLayout(spacer, BoxLayout.Y_AXIS));//sets spacer's layout to BoxLayout
JLabel label = new JLabel(" ");//makes a new JLabel
spacer.add(label);//adds label to spacer
bigpanel.add(spacer);//adds spacer to bigpanel
JPanel peast = new JPanel();//makes a new panel called peast
bigpanel.add(peast);//bigpanel adds peast
peast.setLayout(new BoxLayout(peast,BoxLayout.Y_AXIS));//sets peast's layout to boxlayout
JButton morenoun = new JButton("Add a Noun");//makes a new button called more noun with the text "More Nouns"
morenoun.addActionListener(new morenounListener());//morenoun adds an actionlistener which is a morenounlistener()
peast.add(morenoun);//peast adds morenoun
JButton clearnoun = new JButton("Clear Nouns");//makes a new button called clearnoun with the text "Clear Nouns"
clearnoun.addActionListener(new clearnounListener());//clearnoun adds an actionlistener which is a clearnounlistener()
peast.add(clearnoun);//peast adds clearnoun
JLabel spacer2 = new JLabel(" ");//makes a new label called spacer2
peast.add(spacer2);//peast adds spacer2
JButton moreadj = new JButton("Add an Adjective");//makes a new button called moreadj with the text "More Adjectives"
moreadj.addActionListener(new moreadjListener());//moreadj adds an actionlistener which is a moreadjlistener()
peast.add(moreadj);//peast adds moreadj
JButton clearadj = new JButton("Clear Adjectives");//makes a new button called clearadj with the text "Cear Adjectives"
clearadj.addActionListener(new clearadjListener());//clearadj adds an actionlistener which is a clearadjlistener()
peast.add(clearadj);//peast adds clearadj
JLabel spacer3 = new JLabel(" ");//makes a new label called spacer3
peast.add(spacer3);//peast adds
JButton moreverb = new JButton("Add a Verb");//makes a new button called moreverb with the text "More Verbs"
moreverb.addActionListener(new moreverbListener());//moreverb adds an actionlistener which is a moreverblistener()
peast.add(moreverb);//peast adds moreverb
JButton clearverb = new JButton("Clear Verbs");//makes a new button called clearverb with the text "Clear Verbs"
clearverb.addActionListener(new clearverbListener());//clearverb adds an actionlistener which is a clearverbListener()
peast.add(clearverb);//peast adds clearverb
JLabel savespacer = new JLabel(" ");//makes a new label called savespacer
peast.add(savespacer);//peast adds savespacer
JButton savewords = new JButton("Save Words");//makes a new button called save words
savewords.addActionListener(new savewordsListener());//savewords adds an actionlistener which is a savewordsListener()
peast.add(savewords);//peast adds savewords
JButton savesession = new JButton("Save Phrases");//creates a new button called savesession with the text "Save Phrases"
savesession.addActionListener(new savesessionListener());//savesession adds an actionlistener which is a savesessionListener()
peast.add(savesession);//peast adds savesession
JLabel quickspacer = new JLabel(" ");//makes a new label called quickspacer
peast.add(quickspacer);//peast adds quickspacer
JButton quick1 = new JButton("Word Arrangement 1");//makes a new button called quick1 with the text "Quickie Button 1"
quick1.addActionListener(new quick1Listener());//
peast.add(quick1);
JButton quick2 = new JButton("Word Arrangement 2");
quick2.addActionListener(new quick2Listener());
peast.add(quick2);
JButton quick3 = new JButton("Word Arrangement 3");
quick3.addActionListener(new quick3Listener());//100
peast.add(quick3);
JPanel pwest = new JPanel();
pwest.setLayout(new BoxLayout(pwest,BoxLayout.Y_AXIS));
JPanel bigpanel2 = new JPanel();
bigpanel2.add(pwest);
Font midfont = new Font("Comic Sans MS", Font.BOLD, 18);
midgui = new JTextArea(10,20);
midgui.setEditable(false);
midgui.setLineWrap(true);
midgui.setFont(midfont);
JScrollPane scroll = new JScrollPane(midgui);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pcenter.add(scroll);
nounbox1 = new JTextField("Noun ");
nounbox2 = new JTextField("Noun ");
nounbox3 = new JTextField("Noun ");
JLabel spacer4 = new JLabel(" ");
pwest.add(nounbox1);
nounbox1.addActionListener(this);
pwest.add(nounbox2);
nounbox2.addActionListener(new nounbox2Listener());
pwest.add(nounbox3);
nounbox3.addActionListener(new nounbox3listener());
pwest.add(spacer4);
adjbox1 = new JTextField("Adjective ");
adjbox2 = new JTextField("Adjective ");
adjbox3 = new JTextField("Adjective ");
JLabel spacer5 = new JLabel(" ");
pwest.add(adjbox1);
adjbox1.addActionListener(new adjbox1Listener());
pwest.add(adjbox2);
adjbox2.addActionListener(new adjbox2Listener());
pwest.add(adjbox3);
adjbox3.addActionListener(new adjbox3Listener());
pwest.add(spacer5);
verbbox1 = new JTextField("Verb ");
verbbox2 = new JTextField("Verb ");
verbbox3 = new JTextField("Verb ");
pwest.add(verbbox1);
verbbox1.addActionListener(new verbbox1Listener());
pwest.add(verbbox2);
verbbox2.addActionListener(new verbbox2Listener());
pwest.add(verbbox3);
verbbox3.addActionListener(new verbbox3Listener());
JPanel psouth = new JPanel();
JPanel bigpanel3 = new JPanel();
bigpanel3.add(psouth);
phrasemaker = new JButton("Make A Phrase!");
phrasemaker.addActionListener(new phrasemakerListener());
psouth.add(phrasemaker);
boxleft.add(nounbox1);
boxleft.add(nounbox2);
boxleft.add(nounbox3);
Box.createVerticalGlue();
boxleft.add(adjbox1);
boxleft.add(adjbox2);
boxleft.add(adjbox3);
Box.createVerticalGlue();
boxleft.add(verbbox1);
boxleft.add(verbbox2);
boxleft.add(verbbox3);
frame.setSize(800,600);//sets the size of the JFrame
frame.setVisible(true);//sets the ability to see the JFrame
frame.getContentPane().add(BorderLayout.EAST, bigpanel);
frame.getContentPane().add(BorderLayout.WEST, boxleft);
frame.getContentPane().add(BorderLayout.SOUTH, bigpanel3);
frame.getContentPane().add(BorderLayout.CENTER, scroll);
}
public void actionPerformed(ActionEvent e) {
}
class phrasemakerListener implements ActionListener{
public void actionPerformed(ActionEvent ev) {
try{
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
String[] nounarray = nouns.toArray(new String[nouns.size()]);
String[] adjarray = adjectives.toArray(new String[adjectives.size()]);
String[] verbarray = verbs.toArray(new String[verbs.size()]);
int countnoun = nounarray.length;
int countadj = adjarray.length;
int countverb = verbarray.length;
int ran1 = (int) (Math.random() * countverb);
int ran2 = (int) (Math.random() * countadj);
int ran3 = (int) (Math.random() * countnoun);
String funny1 = adjarray [ran2]+ " " + nounarray [ran3]+" " + verbarray [ran1]+" ";
p = funny1;
midgui.append(p + "\n ");
phrasemaker.setText("Make Another!");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
class morenounListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.add(nounbox1.getText());
nounbox1.setText("");
}
}
class clearnounListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class moreadjListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
adjectives.add(adjbox1.getText());
adjbox1.setText("");
}
}
class clearadjListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class moreverbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
verbs.add(verbbox1.getText());
verbbox1.setText("");
}
}
class clearverbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class savewordsListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
FileOutputStream fileout = new FileOutputStream("words.pb");
ObjectOutputStream out = new ObjectOutputStream(fileout);
out.writeObject(nouns);
out.close();
midgui.append("words saved");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
class savesessionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JFrame frame = new JFrame();
savefield = new JTextField("Untitled.txt");
JButton save = new JButton("Save");
save.addActionListener(new savebuttonListener());
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(savefield);
frame.add(save);
frame.setSize(400,75);
}catch(Exception x){
x.printStackTrace();
}
}
}
class quick1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
String[] nounarray = nouns.toArray(new String[nouns.size()]);
String[] adjarray = adjectives.toArray(new String[adjectives.size()]);
String[] verbarray = verbs.toArray(new String[verbs.size()]);
int countnoun = nounarray.length;
int countadj = adjarray.length;
int countverb = verbarray.length;
int ran1 = (int) (Math.random() * countverb);
int ran2 = (int) (Math.random() * countadj);
int ran3 = (int) (Math.random() * countnoun);
String funny1 = adjarray [ran2]+ " " + nounarray [ran3]+" " + verbarray [ran1]+" ";
String funny2 = nounarray [ran3]+ " " +adjarray [ran2]+ " " + verbarray [ran1]+" ";
String funny3 = adjarray [ran2]+ " " + verbarray [ran1]+" "+ nounarray [ran3]+" ";
String funny4 = verbarray[ran1] + " " + adjarray[ran2] + " " + nounarray[ran3]+" ";
JFrame frame = new JFrame("Arrangements");
list = new JList();
DefaultListModel dlm = new DefaultListModel();
ListItem item1 = new ListItem("ANV", funny1);
ListItem item2 = new ListItem("NAV", funny2);
ListItem item3 = new ListItem("AVN", funny3);
ListItem item4 = new ListItem("VAN", funny4);
for(int i = 0; i<1;i++){
// add a new ListItem containing display text and a new value
dlm.addElement(item1);
dlm.addElement(item2);
dlm.addElement(item3);
dlm.addElement(item4);
}
list.setModel(dlm);
HashMap<String, String> map = new HashMap<String, String>();
map.put("ANV", funny1);
map.put("NAV", funny2);
map.put("NAV", funny3);
map.put("VAN", funny4);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.addListSelectionListener(new listListener());
JButton set = new JButton("Done");
set.addActionListener(new setListener());
frame.getContentPane().add(BorderLayout.CENTER, list);
frame.getContentPane().add(BorderLayout.SOUTH, set);
frame.setSize(250,250);
frame.setVisible(true);
}
}
class quick2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class quick3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class nounbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class nounbox3listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class midguiListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class savebuttonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String fileName = savefield.getText();
FileWriter writer = new FileWriter(fileName);
writer.write(midgui.getText());
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public void establishConnection(){
try{
Socket sock = new Socket("192.168.0.195", 5620);
}catch(Exception ex){
midgui.append("Couldn't get connection");
}
}
class setListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class listListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
ListItem selection = (ListItem) list.getSelectedValue();
p = selection.getValue();
}else{
}
}
}
}
-
July 8th, 2009, 05:03 AM
#20
Re: Jlist help
Originally Posted by Shaken_Earth
I want to have a default so everytime the user launches the Phrasebot they don't have to go into Arrangements... and choose what they want. So I give a value to "p" in the phrasemakerListener inner-class. So I tried changing the value of "p" by using the list. But when I have "p" with a default it won't change. When there's no default it changes. I thought the whole point of instance variables is state. Shouldn't I be able to <i> change </i> the state if there's a default?
I have no idea. You've never said what your program is supposed to do, or how it should be used, so I don't know what Arrangements is for, or what 'p' is (I should be able to tell from the name, I suppose ). I don't know what you mean by "when I have "p" with a default it won't change. When there's no default it changes".
That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted...
G. Boole
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 9th, 2009, 09:18 AM
#21
Re: Jlist help
The basic idea behind Phrasebot is to be able to take words the user has entered and make a random 'Phrase' out of them. Since Phrasebot only provides 3 boxes for each type of word you can add to the list and, if you don't like the words you entered you can always clear them with the Clear buttons. You can also save a set of words so you can open them up later or save the current Phrases to a .txt file so you can open them up in a simple text editor. Finally, you can change the arrangement of the words because certain verbs, adjectives, and nouns wouldn't make sense together if they were arranged in a certain way.
You would be most likely using this program if you were with friends or while you're bored and just want to be amused.
p is the current arrangement. That's all p is. What I mean by "when I have "p" with a default it won't change. When there's no default it changes" is that when p is declared inside a method it won't change when a user wants to change the arrangement. But when it equals null it will change whenever the user wants to change the arrangement but they have to go into arrangements to make this happen and they don't have a default. Hope this explained everything
-
July 10th, 2009, 08:58 AM
#22
Re: Jlist help
If I understand you correctly, 'p' is the ordering of adjective, verb, and noun selected in the Arrangements frame. You want to set a default ordering for 'p', but find that when you do, any new selection of 'p' is ignored.
The reason is that when the 'Make Another' button is pressed, the phrasemakerListener is notified, which sets 'p' to the random default - overwiting whatever 'p' was set to in the Arrangements frame.
Two solutions spring to mind:
1. set the 'p' default value in the Phrasebot constructor (which you seem to have substituted with a method called 'go'). I'm sure I mentioned previously that the constructor is where all class fields should be initialised to their default values.
2. test 'p' to see if it's null before setting it to the default value.
I strongly recommend that you choose option 1. It should result in a better design.
I also strongly recommend that you use the Java naming conventions - class names start with an upper-case letter, method and variable names start with a lower-case letter. Constants (final) may be declared all capitalised with underscores separating words.
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 11th, 2009, 04:45 PM
#23
Re: Jlist help
Okay instead of using a method I'm using a constructor now. But it's still not working. I honestly think I'm doing something wrong. I haven't really ever used constructors so I think that's the reason. Can you help? Here's the code:
Code:
//Phrasebot is property of shakenearth.com
//Code by Thomas Lisankie
//You may not use any of this code
//you may not distribute this program either
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.net.*;
public class Phrasebot implements ActionListener {
JTextArea midgui;
JButton phrasemaker;
Box boxleft;
JTextField nounbox1;
JTextField nounbox2;
JTextField nounbox3;
JTextField adjbox1;
JTextField adjbox2;
JTextField adjbox3;
JTextField verbbox1;
JTextField verbbox2;
JTextField verbbox3;
static HashSet<String> nouns = new HashSet<String>();
static HashSet<String> adjectives = new HashSet<String>();
static HashSet<String> verbs = new HashSet<String>();
JList list;
static String funny1;
JTextField savefield;
JButton save;
static String p;
public static void main(String[] args){
try{
Phrasebot bot = new Phrasebot(funny1);
}catch(Error Ex){
Ex.printStackTrace();
}
}
public Phrasebot(String a){
nounbox1 = new JTextField("Noun ");
nounbox2 = new JTextField("Noun ");
nounbox3 = new JTextField("Noun ");
adjbox1 = new JTextField("Adjective ");
adjbox2 = new JTextField("Adjective ");
adjbox3 = new JTextField("Adjective ");
verbbox1 = new JTextField("Verb ");
verbbox2 = new JTextField("Verb ");
verbbox3 = new JTextField("Verb ");
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
String[] nounarray = nouns.toArray(new String[nouns.size()]);
String[] adjarray = adjectives.toArray(new String[adjectives.size()]);
String[] verbarray = verbs.toArray(new String[verbs.size()]);
int countnoun = nounarray.length;
int countadj = adjarray.length;
int countverb = verbarray.length;
int ran1 = (int) (Math.random() * countverb);
int ran2 = (int) (Math.random() * countadj);
int ran3 = (int) (Math.random() * countnoun);
funny1 = adjarray [ran2]+ " " + nounarray [ran3]+" " + verbarray [ran1]+" ";
p = a;
JPanel pcenter = new JPanel();
JFrame frame = new JFrame("Phrasebot");//makes a new JFrame called "Phrasebot"
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets the default close operation to EXIT_ON_CLOSE
boxleft = new Box(BoxLayout.Y_AXIS);
JPanel bigpanel = new JPanel();//makes a new JPanel called bigpanel
bigpanel.setLayout(new BoxLayout(bigpanel,BoxLayout.Y_AXIS));//sets bigpanel's default layout to BoxLayout
JPanel spacer = new JPanel();//makes a new JPanel called spacer
spacer.setLayout(new BoxLayout(spacer, BoxLayout.Y_AXIS));//sets spacer's layout to BoxLayout
JLabel label = new JLabel(" ");//makes a new JLabel
spacer.add(label);//adds label to spacer
bigpanel.add(spacer);//adds spacer to bigpanel
JPanel peast = new JPanel();//makes a new panel called peast
bigpanel.add(peast);//bigpanel adds peast
peast.setLayout(new BoxLayout(peast,BoxLayout.Y_AXIS));//sets peast's layout to boxlayout
JButton morenoun = new JButton("Add a Noun");//makes a new button called more noun with the text "More Nouns"
morenoun.addActionListener(new morenounListener());//morenoun adds an actionlistener which is a morenounlistener()
peast.add(morenoun);//peast adds morenoun
JLabel spacer2 = new JLabel(" ");//makes a new label called spacer2
peast.add(spacer2);//peast adds spacer2
JButton moreadj = new JButton("Add an Adjective");//makes a new button called moreadj with the text "More Adjectives"
moreadj.addActionListener(new moreadjListener());//moreadj adds an actionlistener which is a moreadjlistener()
peast.add(moreadj);//peast adds moreadj
JLabel spacer3 = new JLabel(" ");//makes a new label called spacer3
peast.add(spacer3);//peast adds
JButton moreverb = new JButton("Add a Verb");//makes a new button called moreverb with the text "More Verbs"
moreverb.addActionListener(new moreverbListener());//moreverb adds an actionlistener which is a moreverblistener()
peast.add(moreverb);//peast adds moreverb
JLabel savespacer = new JLabel(" ");//makes a new label called savespacer
peast.add(savespacer);//peast adds savespacer
JButton savewords = new JButton("Save Words");//makes a new button called save words
savewords.addActionListener(new savewordsListener());//savewords adds an actionlistener which is a savewordsListener()
peast.add(savewords);//peast adds savewords
JButton savesession = new JButton("Save Phrases");//creates a new button called savesession with the text "Save Phrases"
savesession.addActionListener(new savesessionListener());//savesession adds an actionlistener which is a savesessionListener()
peast.add(savesession);//peast adds savesession
JLabel quickspacer = new JLabel(" ");//makes a new label called quickspacer
peast.add(quickspacer);//peast adds quickspacer
JButton quick1 = new JButton("Arrangements");//makes a new button called quick1 with the text "Quickie Button 1"
quick1.addActionListener(new quick1Listener());//
peast.add(quick1);
JButton restart = new JButton("Restart");
restart.addActionListener(new quick2Listener());
peast.add(restart);
JPanel pwest = new JPanel();
pwest.setLayout(new BoxLayout(pwest,BoxLayout.Y_AXIS));
JPanel bigpanel2 = new JPanel();
bigpanel2.add(pwest);
Font midfont = new Font("Comic Sans MS", Font.BOLD, 18);
midgui = new JTextArea(10,20);
midgui.setEditable(false);
midgui.setLineWrap(true);
midgui.setFont(midfont);
JScrollPane scroll = new JScrollPane(midgui);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pcenter.add(scroll);
JLabel spacer4 = new JLabel(" ");
pwest.add(nounbox1);
nounbox1.addActionListener(this);
pwest.add(nounbox2);
nounbox2.addActionListener(new nounbox2Listener());
pwest.add(nounbox3);
nounbox3.addActionListener(new nounbox3listener());
pwest.add(spacer4);
JLabel spacer5 = new JLabel(" ");
pwest.add(adjbox1);
adjbox1.addActionListener(new adjbox1Listener());
pwest.add(adjbox2);
adjbox2.addActionListener(new adjbox2Listener());
pwest.add(adjbox3);
adjbox3.addActionListener(new adjbox3Listener());
pwest.add(spacer5);
pwest.add(verbbox1);
verbbox1.addActionListener(new verbbox1Listener());
pwest.add(verbbox2);
verbbox2.addActionListener(new verbbox2Listener());
pwest.add(verbbox3);
verbbox3.addActionListener(new verbbox3Listener());
JPanel psouth = new JPanel();
JPanel bigpanel3 = new JPanel();
bigpanel3.add(psouth);
phrasemaker = new JButton("Make A Phrase!");
phrasemaker.addActionListener(new phrasemakerListener());
psouth.add(phrasemaker);
boxleft.add(nounbox1);
boxleft.add(nounbox2);
boxleft.add(nounbox3);
Box.createVerticalGlue();
boxleft.add(adjbox1);
boxleft.add(adjbox2);
boxleft.add(adjbox3);
Box.createVerticalGlue();
boxleft.add(verbbox1);
boxleft.add(verbbox2);
boxleft.add(verbbox3);
frame.setSize(800,600);//sets the size of the JFrame
frame.setVisible(true);//sets the ability to see the JFrame
frame.getContentPane().add(BorderLayout.EAST, bigpanel);
frame.getContentPane().add(BorderLayout.WEST, boxleft);
frame.getContentPane().add(BorderLayout.SOUTH, bigpanel3);
frame.getContentPane().add(BorderLayout.CENTER, scroll);
}
public void go() {
}
public void actionPerformed(ActionEvent e) {
}
class phrasemakerListener implements ActionListener{
public void actionPerformed(ActionEvent ev) {
try{
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
String[] nounarray = nouns.toArray(new String[nouns.size()]);
String[] adjarray = adjectives.toArray(new String[adjectives.size()]);
String[] verbarray = verbs.toArray(new String[verbs.size()]);
int countnoun = nounarray.length;
int countadj = adjarray.length;
int countverb = verbarray.length;
int ran1 = (int) (Math.random() * countverb);
int ran2 = (int) (Math.random() * countadj);
int ran3 = (int) (Math.random() * countnoun);
String funny1 = adjarray [ran2]+ " " + nounarray [ran3]+" " + verbarray [ran1]+" ";
midgui.append(p + "\n ");
phrasemaker.setText("Make Another!");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
class morenounListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.add(nounbox1.getText());
nounbox1.setText("");
}
}
class moreadjListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
adjectives.add(adjbox1.getText());
adjbox1.setText("");
}
}
class moreverbListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
verbs.add(verbbox1.getText());
verbbox1.setText("");
}
}
class savewordsListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
FileOutputStream fileout = new FileOutputStream("words.pb");
ObjectOutputStream out = new ObjectOutputStream(fileout);
out.writeObject(nouns);
out.close();
midgui.append("words saved");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
class savesessionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
JFrame frame = new JFrame();
savefield = new JTextField("Untitled.txt");
JButton save = new JButton("Save");
save.addActionListener(new savebuttonListener());
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.add(savefield);
frame.add(save);
frame.setSize(400,75);
}catch(Exception x){
x.printStackTrace();
}
}
}
class quick1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.add(nounbox1.getText());
nouns.add(nounbox2.getText());
nouns.add(nounbox3.getText());
adjectives.add(adjbox1.getText());
adjectives.add(adjbox2.getText());
adjectives.add(adjbox3.getText());
verbs.add(verbbox1.getText());
verbs.add(verbbox2.getText());
verbs.add(verbbox3.getText());
String[] nounarray = nouns.toArray(new String[nouns.size()]);
String[] adjarray = adjectives.toArray(new String[adjectives.size()]);
String[] verbarray = verbs.toArray(new String[verbs.size()]);
int countnoun = nounarray.length;
int countadj = adjarray.length;
int countverb = verbarray.length;
int ran1 = (int) (Math.random() * countverb);
int ran2 = (int) (Math.random() * countadj);
int ran3 = (int) (Math.random() * countnoun);
String funny1 = adjarray [ran2]+ " " + nounarray [ran3]+" " + verbarray [ran1]+" ";
String funny2 = nounarray [ran3]+ " " +adjarray [ran2]+ " " + verbarray [ran1]+" ";
String funny3 = adjarray [ran2]+ " " + verbarray [ran1]+" "+ nounarray [ran3]+" ";
String funny4 = verbarray[ran1] + " " + adjarray[ran2] + " " + nounarray[ran3]+" ";
JFrame frame = new JFrame("Arrangements");
list = new JList();
DefaultListModel dlm = new DefaultListModel();
ListItem item1 = new ListItem("ANV", funny1);
ListItem item2 = new ListItem("NAV", funny2);
ListItem item3 = new ListItem("AVN", funny3);
ListItem item4 = new ListItem("VAN", funny4);
for(int i = 0; i<1;i++){
// add a new ListItem containing display text and a new value
dlm.addElement(item1);
dlm.addElement(item2);
dlm.addElement(item3);
dlm.addElement(item4);
}
list.setModel(dlm);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.addListSelectionListener(new listListener());
JButton set = new JButton("Done");
set.addActionListener(new setListener());
frame.getContentPane().add(BorderLayout.CENTER, list);
frame.getContentPane().add(BorderLayout.SOUTH, set);
frame.setSize(250,250);
frame.setVisible(true);
}
}
class quick2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
nouns.clear();
adjectives.clear();
verbs.clear();
nounbox1.setText("");
nounbox2.setText("");
nounbox3.setText("");
adjbox1.setText("");
adjbox2.setText("");
adjbox3.setText("");
verbbox1.setText("");
verbbox2.setText("");
verbbox3.setText("");
midgui.setText("");
}
}
class quick3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class nounbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class nounbox3listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class adjbox3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox1Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox2Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class verbbox3Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class midguiListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class savebuttonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try{
String fileName = savefield.getText();
FileWriter writer = new FileWriter(fileName);
writer.write(midgui.getText());
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public void establishConnection(){
try{
Socket sock = new Socket("192.168.0.195", 5620);
}catch(Exception ex){
midgui.append("Couldn't get connection");
}
}
class setListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class listListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
ListItem selection = (ListItem) list.getSelectedValue();
p = selection.getValue();
}else{
}
}
}
}
-
July 12th, 2009, 03:48 AM
#24
Re: Jlist help
Originally Posted by Shaken_Earth
But it's still not working.
So am I supposed to guess in what way it isn't working? How do you know it isn't working? what are the symptoms?
I honestly think I'm doing something wrong.
If it isn't working, that is the most likely explanation.
I suggest you walk through your program from the very start, checking what the values of the variables are when you encounter them. Pay particular attention to 'funny1'.
This kind of thing, where you try to find out what's wrong with your code and fix it, is known as 'debugging', and it is a crucial skill to learn. Use logic and common-sense to narrow down the possibilities until you find the problem (Java error messages will generally tell you what it is and where it is), then try to understand what's happening and correct it. At it's simplest, it involves taking the role of the computer and stepping through the relevant code by hand, tracking the values of all variables as they are encountered and used.
It's hard enough to find an error in your code when you're looking for it; it's even harder when you've assumed your code is error-free...
S. McConnell
Last edited by dlorde; July 12th, 2009 at 03:51 AM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|