April 24th, 2003, 10:33 PM
|
#1 (permalink)
| | Member
Join Date: Nov 2001 Location: Bloomington IN
Posts: 219
| Hey, I need Java Help too!
Hey all, how can I convert this code into an applet, rather than a JFrame? I am seriously DISliking Java right now..... Code: import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/*
* NewsSearchTest.java
*
* Created on April 24, 2003, 7:08 PM
*/
/**
*
* @author Ray Household
*/
public class NewsSearchTest extends JFrame {
private JLabel nyLabel;
private JTextField nyKeyword;
private Container searchArticles;
private JButton beginSearch;
private JTextArea output;
public NewsSearchTest()
{
nyLabel = new JLabel("Enter Keywords");
nyKeyword = new JTextField( 15 );
beginSearch = new JButton( "Search" );
beginSearch.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
StringTokenizer tokens = new StringTokenizer( nyKeyword.getText() );
output.setText( "Number " + tokens.countTokens() + "." );
while ( tokens.hasMoreTokens() )
output.append( tokens.nextToken() + "\n" );
}
}
);
Container searchArticles = getContentPane();
searchArticles.setLayout( new FlowLayout() );
searchArticles.add( nyLabel );
searchArticles.add( nyKeyword );
searchArticles.add( beginSearch );
output = new JTextArea( 10, 20 );
output.setEditable( false );
searchArticles.add( new JScrollPane( output ) );
setSize( 275, 240 );
setVisible( true );
}
/** Creates a new instance of NewsSearchTest */
public static void main( String args[] )
{
NewsSearchTest application = new NewsSearchTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} |
| |
April 25th, 2003, 12:49 AM
|
#2 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Here's a funky way, but probably not what you want. I'll post the better way in a minute Code: import java.applet.Applet;
public class AppAsApplet extends Applet
{
public void init()
{
add(new NewsSearchTest());
}
} You may need to refer to one of my posts in this forum http://www.techimo.com/forum/showthr...301#post592301 to make sure if your putting your applet on the web it's available to all. |
| |
April 25th, 2003, 03:03 AM
|
#3 (permalink)
| | Member
Join Date: Nov 2001 Location: Bloomington IN
Posts: 219
|
Thanks for the info. I started over from scratch and am having reasonable success.
Here is a question, how do you extract a 'value' from a RadioButtoinGroup?
I have 3 buttons, 'AND' 'OR' 'NOT'.
How can I get the actual 'value' of the selected radiobutton after pressing a 'SEARCH' button? |
| |
April 25th, 2003, 03:54 AM
|
#4 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Here's your App as an Applet Code: import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.Applet;
/*
* NewsSearchTest.java
*
* Created on April 24, 2003, 7:08 PM
*/
/**
*
* @author Ray Household
* @modified 25/04/2003 DaveMark : from App 2 Applet!
*/
class NewsSearchTest extends JApplet implements ActionListener {
private JLabel nyLabel;
private JTextField nyKeyword;
private JButton beginSearch;
private JTextArea output;
private Container cp;
public void init()
{
nyLabel = new JLabel( "Enter Keywords" );
nyKeyword = new JTextField( 15 );
beginSearch = new JButton( "Search" );
beginSearch.addActionListener( this );
beginSearch.setMnemonic(KeyEvent.VK_S);
cp = getContentPane();
cp.setLayout( new FlowLayout() );
cp.add( nyLabel );
cp.add( nyKeyword );
cp.add( beginSearch );
output = new JTextArea( 10, 20 );
output.setEditable( false );
cp.add( new JScrollPane(output) );
}
public void actionPerformed( ActionEvent e )
{
StringTokenizer tokens = new StringTokenizer( nyKeyword.getText() );
output.setText( "Number = " + tokens.countTokens()
+ "\n------------\n" );
while ( tokens.hasMoreTokens() )
output.append( tokens.nextToken() + "\n" );
}
/*
* Bung the JApplet in a JFrame and call the init() & start methods
*/
public static void main(String [] args)
{
NewsSearchTest nst = new NewsSearchTest();
nst.init();
nst.start();
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame();
f.getContentPane().add(nst);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setLocation(300, 300);
f.setResizable(false);
f.setVisible(true);
}
} It's easier to design an Applet then add a main method if you want to use or test it as an App. The main method would instantiate the Applet, add it to a frame and explicitly call the init() & start() methods. This wouldn't affect the class still being able to work as an Applet! I'll edit this post l8r to show that in action.
Doing it from App 2 Applet usually means reworking most of the code!
[edit] I've now added the main method to the JApplet class so the
same code will run as an Applet and a standalone Application! [/edit]
Last edited by DJDaveMark : April 25th, 2003 at 10:39 PM.
|
| |
April 25th, 2003, 05:11 AM
|
#5 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
| Quote: Originally posted by tenor_david how do you extract a 'value' from a RadioButtoinGroup?
I have 3 buttons, 'AND' 'OR' 'NOT'.
How can I get the actual 'value' of the selected radiobutton after pressing a 'SEARCH' button? | Here you go.... Code: import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RadioBtns implements ItemListener
{
public void doGUI()
{
CheckboxGroup radios = new CheckboxGroup();
Checkbox cb1 = new Checkbox("AND", radios, true);
Checkbox cb2 = new Checkbox("OR", radios, false);
Checkbox cb3 = new Checkbox("NOT", radios, false);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
JFrame f = new JFrame("Radio Buttons");
Container cp = f.getContentPane();
cp.setLayout(new FlowLayout());
cp.add(cb1);
cp.add(cb2);
cp.add(cb3);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
f.setSize(400, 100);
f.setLocation(300, 300);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
{
System.out.println("`" + event.getItem() + "' selected!");
}
}
public static void main(String [] args)
{
new RadioBtns().doGUI(); //instantiate and call a method
}
} You could also...... button.setActionCommand("WhateverString");
then query it by...... if (event.getActionCommand() == "WhateverString"); |
| |
April 25th, 2003, 07:25 AM
|
#6 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Here's the same thing using the setActionCommand() but with a cool look & feel. Code: import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RadioBtns implements ActionListener
{
String strAND = "AND",
strOR = "OR" ,
strNOT = "NOT";
public void doGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
ButtonGroup grp = new ButtonGroup();
JRadioButton rb1 = new JRadioButton(strAND);
JRadioButton rb2 = new JRadioButton(strOR);
JRadioButton rb3 = new JRadioButton(strNOT);
grp.add(rb1);
grp.add(rb2);
grp.add(rb3);
rb1.setActionCommand(strAND);
rb2.setActionCommand(strOR);
rb3.setActionCommand(strNOT);
rb1.setMnemonic(KeyEvent.VK_A);
rb2.setMnemonic(KeyEvent.VK_O);
rb3.setMnemonic(KeyEvent.VK_N);
rb1.addActionListener(this);
rb2.addActionListener(this);
rb3.addActionListener(this);
rb1.setSelected(true);
JFrame f = new JFrame("Radio Buttons");
Container cp = f.getContentPane();
cp.setLayout(new FlowLayout());
cp.add(rb1);
cp.add(rb2);
cp.add(rb3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 100);
f.setLocation(300, 300);
f.setResizable(false);
f.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
System.out.println("`" + event.getActionCommand() + "' selected!");
}
public static void main(String [] args)
{
new RadioBtns().doGUI(); //instantiate and call a method
}
}
Last edited by DJDaveMark : April 25th, 2003 at 10:09 PM.
|
| |
April 25th, 2003, 11:04 AM
|
#7 (permalink)
| | Member
Join Date: Nov 2001 Location: Bloomington IN
Posts: 219
|
Very much appreciated! Thanks! |
| |
April 25th, 2003, 10:46 PM
|
#8 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Last edited by DJDaveMark : April 25th, 2003 at 10:57 PM.
|
| |
April 27th, 2003, 03:10 PM
|
#9 (permalink)
| | Member
Join Date: Nov 2001 Location: Bloomington IN
Posts: 219
|
Hey there DJDaveMark:
I am going to pick your brain again!
Here is the current status of this project I am working on: http://mentor.ucs.indiana.edu/~s3jm0...chGridBag.html
The only listeners I have succesfully implemented till now are the listeners that write the 2 text fields to the JScrollPane Text Area.
I cannot seem to get the radio button listeners working correctly. Basically, when a user submits a search, the history panel should display: keywords1 + searchLogic + keywords2 + \n
Eventually, we will actually send the search parameters to the appropriate engine and return the results in a pop up window.
Any help w/ the radio button listener would be much appreciated Search Interface Source Date Combo Box Source |
| |
April 27th, 2003, 10:44 PM
|
#10 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
'lo again tenor,
You were so, so close yet that cigar eluded you!
You were only really missing two lines of code. One to get the info & the other to append to the history. You don't need to implement any listeners for the JRadioButtons. When the Search button is clicked you just need to interogate which JRadioButton is selected.....so from this...... Code: //START DM EDIT ===============================
nySearch.setText("Search");
nySearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nySearchHandler(); //no need to pass in ActionEvent
//mySearchHandlerLogic();
//mySearchHandler2();
}
});
//END DM EDIT =============================== ......you call this........ Code: //START DM EDIT ===============================
private void nySearchHandler() {//GEN-FIRST:event_nySearchHandler
StringTokenizer tokens = new StringTokenizer( nyKeywords.getText() );
while ( tokens.hasMoreTokens() )
historyText.append( tokens.nextToken() + " " );
//mySearchHandlerLogic
ButtonModel logicBtn = searchLogic.getSelection();
historyText.append( logicBtn.getActionCommand() + " " );
//mySearchHandler2
StringTokenizer tokens2 = new StringTokenizer( nyKeywords2.getText() );
while ( tokens2.hasMoreTokens() )
historyText.append( tokens2.nextToken() + " ");
historyText.append( "\n" );
}//GEN-LAST:event_nySearchHandler
//END DM EDIT =============================== Also, did you know you named one method "nySearchHandler" with an 'n' and "mySearchHandler2" with an 'm'
The key to the above part working relies on using the setActionCommand(java.lang.String) method correctly.....like so Code: //START DM EDIT ===============================
logicPanel.setMinimumSize(new java.awt.Dimension(300, 34));
logicPanel.setPreferredSize(new java.awt.Dimension(300, 34));
andLogic.setMnemonic('A');
searchLogic.add(andLogic);
andLogic.setActionCommand(strAND);
andLogic.setLabel(strAND);
logicPanel.add(andLogic);
notLogic.setMnemonic('N');
notLogic.setText(strNOT);
searchLogic.add(notLogic);
notLogic.setActionCommand(strNOT);
logicPanel.add(notLogic);
orLogic.setMnemonic('O');
orLogic.setText(strOR);
searchLogic.add(orLogic);
orLogic.setActionCommand(strOR);
logicPanel.add(orLogic);
//andLogic.setSelected(true);
//END DM EDIT =============================== The setActionCommand(java.lang.String) doesn't have to be the same String as the label, it can be any text string imaginable. I used the strAND strNOT strOR just so i didn't miss spell the label from the actionCommand and would only need to update the text in one place if it changed. (I also had to move their String declarations from the constructor method to the class level beside the other class variables)
The only problem I can now see is when you click the search button without selecting AND/OR/NOT. I tried to use andLogic.setSelected(true); but it didn't work for some reason and i've left it in but commented out. I can't see anything in your code that would conflict with it, and don't know why this is happening. I even tried setting the name and default state of the JRadioButton using it's constructor methods but this didn't work as it should have either.
Anyway, if all else fails you could always error trap this in the nySearchHandler() method. I'm sure you've lots to get on with now!!! EDIT: Wait a minute, I don't know how I missed this, but you're using a constructor method to initialize your Applet. Here's a quote from the java.sun.com website Quote: fromhttp://java.sun.com/docs/books/tutor...etMethods.html
The init method is useful for one-time initialization that doesn't take very long. In general, the init method should contain the code that you would normally put into a constructor. The reason applets shouldn't usually have constructors is that an applet isn't guaranteed to have a full environment until its init method is called. For example, the Applet image loading methods simply don't work inside of a applet constructor. The init method, on the other hand, is a great place to call the image loading methods, since the methods return quickly. Every applet that does something after initialization (except in direct response to user actions) must override the start method. The start method either performs the applet's work or (more likely) starts up one or more threads to perform the work
| That could be a contributor to the setSelected(true) trouble
Cheery-Bye
DaveMark
Last edited by DJDaveMark : April 27th, 2003 at 11:35 PM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |