Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Getting a TextField to activate ItemListener.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Getting a TextField to activate ItemListener.

    I'm having it choose songs from a hymn book and set the page number to a certain page if they chose a certain song name from a JComboBox. This I know how to do. However, I would also like it to do it the other way around by having it, if a certain page number is entered in the text field, get it to set the song selected in the JComboBox. I thought it would be simple, but it's not working out. Do I need a bunch of KeyListeners?

    Also, in that same program, I have fonts that will be selected but I can't figure out how it'll know which font to change size and/or style. What Font method do I call to get the font type from the text area and then call deriveFont() to change the size and/or style of that font? Should I use the getFont() or getFontName() method or some other method?

    Also, I realize now I could have used JComboBoxes for the Colors, but it's too late by the time that occurred to me, plus I was experimenting with JRadioButton and ButtonGroup anyway.

    Also, my JTextField is somehow coming out too big and acting too much like a JTextArea. Except it only has one line, though it can get going on that one line

    like this

    ----------------------------------------------------------------------------------------------------------------------------

    that's why I mean by one line.

    I only set it for 3.

    Must be my layout. I wish I could get a nicer layout, but how do you do that?

    Ok, How do I add a key listener to handle it if two or more keys are typed?

    Like if I typed 12 or 150.

    Is it:

    KeyEvent e;

    if (e.getKeyChar() == VT_A + VT_B)

    for AB or ab ?



    package SongBook;
     
     
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
     
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.util.*;
    import java.io.*;
    import javax.swing.ImageIcon;
     
     
    import java.awt.event.WindowEvent;
    import java.awt.Window;
    import javax.swing.JScrollPane;
    import java.awt.Component;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
     
     
     
     
    public class LivingHymns extends JFrame  {
     
    	/* (non-Javadoc)
    	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    	 */
     
     
    	JTextArea area;
    	JCheckBox box;
    	JComboBox songs, fonts, colors;
    	JScrollPane scrolls, scrolls2;
    	String[] songNames;
    	String[] fontTypes;
    	String[] colorSelector;
    	JButton copy;
    	JTextField pageNumber, verseNumber;
    	JPanel panel;
    	String Bob;
    	JFrame frame, frame2;
    	JRadioButton color, color2, color3, color4, color5, color6, color7, color8, color9, color10,
    	color11, color12, color13;
    	ButtonGroup group, group2;
    	Color red, green, blue, orange, yellow, pink, black, gray, lightGray, darkGray, magenta, cyan,
    	white;
    	JLabel background, textBackground, textForeground, pageNum;
    	JRadioButton color14, color15, color16, color17, color18, color19, color20, color21, color22,
    	color23, color24, color25, color26;
    	Choice choice;
    	Font Aharoni, Andalus, AngsanaNew, AngsanaUPC, ArabicTypesetting, Arial, ArialBlack, ArialNarrow,
    	ArialUnicodeMS, Batang, BatangChe, BlackadderITC, BookAntiqua, BookmanOldStyle, BookshelfSymbol7,
    	BradleyHandITC, BrowalliaNew, BrowalliaUPC, Calibri, Cambria, CambriaMath, Candara, Century, 
    	CenturyGothic, ComicSansMS, Consolas, Constantia, CopperplateGothicBold, CopperplateGothicLight,
    	Corbel, CordiaNew, CordiaUPC, CourierNew, CSD12, CurlzMT, DaunPenh, David, DFKaiSB, Dialog,
    	DialogInput, DilleniaUPC, DokChampa, Dotum, DotumChe, EdwardianScriptITC, EngraversMT, ErasDemiITC,
    	ErasLightITC, EstrangeloEdessa, EucrosiaUPC, Euphemia, Eurostile, FangSong, FelixTitling,
    	FranklinGothicBook, FranklinGothicDemi, FranklinGothicDemiCond, FranklinGothicHeavy, FranklinGothicMedium,
    	FranklinGothicMediumCond, FrankRuehl, FreesiaUPC, FreestyleScript, FrenchScriptMT, Garamond, Gautami,
    	Georgia, GiovanniITCTT, Gisha, Gulim, GulimChe, Gungsuh, GungsuhChe, Impact, IrisUPC, IskoolaPota,
    	JasmineUPC, Jokerman, JuiceITC, KaiTi, Kalinga, Kartika, KodchiangUPC, KristenITC, Latha, Leelawadee,
    	LevenimMT, LilyUPC, LucidaBlackletter, LucidaBright, LucidaCalligraphy, LucidaConsole, LucidaHandwriting,
    	LucidaSans, LucidaSansTypewriter, LucidaSansUnicode, MaiandraGD, MalgunGothic, Mangal, Marlett, 
    	MatisseITC, Meiryo, MeiryoUI, MicrosoftHimalaya, MicrosoftJhengHei, MicrosoftSansSerif, MicrosoftUighur,
    	MicrosoftYaHei, MicrosoftYiBaiti, MingLiU, MingLiUExtB, MingLiU_HKSCS, MingLiU_HKSCSExtB, Miriam, MiriamFixed,
    	Mistral;
    	/* All the font types I can possibly use, it'll take a while to add them all in.  
    	1.) Aharoni
    	2.) Andalus
    	3.) Angsana New
    	4.) AngsanaUPC
    	5.) Arabic Typesetting
    	6.) Arial
    	7.) Arial Black
    	8.) Arial Narrow
    	9.) Arial Unicode MS
    	10.) Batang
    	11.) BatangChe
    	12.) Blackadder ITC
    	13.) Book Antiqua
    	14.) Bookman Old Style
    	15.) Bookshelf Symbol 7
    	16.) Bradley Hand ITC
    	17.) Browallia New
    	18.) BrowalliaUPC
    	19.) Calibri
    	20.) Cambria
    	21.) Cambria Math
    	22.) Candara
    	23.) Century
    	24.) Century Gothic
    	25.) Comic Sans MS
    	26.) Consolas
    	27.) Constantia
    	28.) Copperplate Gothic Bold
    	29.) Copperplate Gothic Light
    	30.) Corbel
    	31.) Cordia New
    	32.) CordiaUPC
    	33.) Courier New
    	34.) CSD12
    	35.) Curlz MT
    	36.) DaunPenh
    	37.) David
    	38.) DFKai-SB
    	39.) Dialog
    	40.) DialogInput
    	41.) DilleniaUPC
    	42.) DokChampa
    	43.) Dotum
    	44.) DotumChe
    	45.) Edwardian Script ITC
    	46.) Engravers MT
    	47.) Eras Demi ITC
    	48.) Eras Light ITC
    	49.) Estrangelo Edessa
    	50.) EucrosiaUPC
    	51.) Euphemia
    	52.) Eurostile
    	53.) FangSong
    	54.) Felix Titling
    	55.) Franklin Gothic Book
    	56.) Franklin Gothic Demi
    	57.) Franklin Gothic Demi Cond
    	58.) Franklin Gothic Heavy
    	59.) Franklin Gothic Medium
    	60.) Franklin Gothic Medium Cond
    	61.) FrankRuehl
    	62.) FreesiaUPC
    	63.) Freestyle Script
    	64.) French Script MT
    	65.) Garamond
    	66.) Gautami
    	67.) Georgia
    	68.) GiovanniITCTT
    	69.) Gisha
    	70.) Gulim
    	71.) GulimChe
    	72.) Gungsuh
    	73.) GungsuhChe
    	74.) Impact
    	75.) IrisUPC
    	76.) Iskoola Pota
    	77.) JasmineUPC
    	78.) Jokerman
    	79.) Juice ITC
    	80.) KaiTi
    	81.) Kalinga
    	82.) Kartika
    	83.) KodchiangUPC
    	84.) Kristen ITC
    	85.) Latha
    	86.) Leelawadee
    	87.) Levenim MT
    	88.) LilyUPC
    	89.) Lucida Blackletter
    	90.) Lucida Bright
    	91.) Lucida Calligraphy
    	92.) Lucida Console
    	93.) Lucida Handwriting
    	94.) Lucida Sans
    	95.) Lucida Sans Typewriter
    	96.) Lucida Sans Unicode
    	97.) Maiandra GD
    	98.) Malgun Gothic
    	99.) Mangal
    	100.) Marlett
    	101.) Matisse ITC
    	102.) Meiryo
    	103.) Meiryo UI
    	104.) Microsoft Himalaya
    	105.) Microsoft JhengHei
    	106.) Microsoft Sans Serif
    	107.) Microsoft Uighur
    	108.) Microsoft YaHei
    	109.) Microsoft Yi Baiti
    	110.) MingLiU
    	111.) MingLiU-ExtB
    	112.) MingLiU_HKSCS
    	113.) MingLiU_HKSCS-ExtB
    	114.) Miriam
    	115.) Miriam Fixed
    	116.) Mistral
    	117.) Mongolian Baiti
    	118.) Monospaced
    	119.) Monotype Corsiva
    	120.) MoolBoran
    	121.) MS Gothic
    	122.) MS Mincho
    	123.) MS PGothic
    	124.) MS PMincho
    	125.) MS Reference 1
    	126.) MS Reference 2
    	127.) MS Reference Sans Serif
    	128.) MS Reference Specialty
    	129.) MS UI Gothic
    	130.) MT Extra
    	131.) MV Boli
    	132.) Narkisim
    	133.) NSimSun
    	134.) Nyala
    	135.) OCR A Extended
    	136.) Palatino Linotype
    	137.) Papyrus
    	138.) Perpetua
    	139.) Plantagenet Cherokee
    	140.) PMingLiU
    	141.) PMingLiU-ExtB
    	142.) Poor Richard
    	143.) Pristina
    	144.) Raavi
    	145.) Rockwell
    	146.) Rockwell Extra Bold
    	147.) Rod
    	148.) SansSerif
    	149.) Segoe Print
    	150.) Segoe Script
    	151.) Segoe UI
    	152.) Serif
    	153.) Shruti
    	154.) SimHei
    	155.) Simplified Arabic
    	156.) Simplified Arabic Fixed
    	157.) SimSun
    	158.) SimSun-ExtB
    	159.) Sylfaen
    	160.) Symbol
    	161.) Tahoma
    	162.) Tempus Sans ITC
    	163.) Times New Roman
    	164.) Traditional Arabic
    	165.) Trebuchet MS
    	166.) Tunga
    	167.) Verdana
    	168.) Viner Hand ITC
    	169.) Vivaldi
    	170.) Vrinda
    	171.) Webdings
    	172.) Wingdings
    	173.) Wingdings 2
    	174.) Wingdings 3
    */
     
     
    	/**
    	 * @param arg0
    	 */
    	/**
    	 * 
    	 */
    	public LivingHymns() {
    		setTitle("Living Hymns Song Selection for Shooting Park Road Baptist Church");
    		Aharoni = new Font("Aharoni", Font.BOLD, 50);
    		Andalus = new Font("Andalus", Font.BOLD, 50);
     
    		frame2 = new JFrame();
    		colorSelector = new String[13];
    		colorSelector[0] = "Red";
    		colorSelector[1] = "Green";
    		colorSelector[2] = "Blue";
    		colorSelector[3] = "Orange";
    		colorSelector[4] = "Yellow";
    		colorSelector[5] = "Pink";
    		colorSelector[6] = "Black";
    		colorSelector[7] = "Gray";
    		colorSelector[8] = "Light Gray";
    		colorSelector[9] = "Dark Gray";
    		colorSelector[10] = "Magenta";
    		colorSelector[11] = "Cyan";
    		colorSelector[12] = "White";
     
    		colors = new JComboBox(colorSelector);
     
    		fontTypes = new String[174];
     
     
     
    		pageNumber = new JTextField(3);
    		pageNum = new JLabel("Enter page number");
    		panel = new JPanel();
    		pageNum.setVisible(true);
    		pageNumber.setVisible(true);
    		panel.add(pageNum);
    		panel.add(pageNumber);
     
     
     
     
     
     
    		frame = new JFrame();
    		background = new JLabel("Set Background Color");
    		color = new JRadioButton("Red");
    		color2 = new JRadioButton("Green");
    		color3 = new JRadioButton("Blue");
    		color4 = new JRadioButton("Orange");
    		color5 = new JRadioButton("Yellow");
    		color6 = new JRadioButton("Pink");
    		color7 = new JRadioButton("Black");
    		color8 = new JRadioButton("Gray");
    		color9 = new JRadioButton("Light Gray");
    		color10 = new JRadioButton("Dark Gray");
    		color11 = new JRadioButton("Magenta");
    		color12 = new JRadioButton("Cyan");
    		color13 = new JRadioButton("White");
    		color14 = new JRadioButton("Red");
    		color15 = new JRadioButton("Green");
    		color16 = new JRadioButton("Blue");
    		color17 = new JRadioButton("Orange");
    		color18 = new JRadioButton("Yellow");
    		color19 = new JRadioButton("Pink");
    		color20 = new JRadioButton("Black");
    		color21 = new JRadioButton("Gray");
    		color22 = new JRadioButton("Light Gray");
    		color23 = new JRadioButton("Dark Gray");
    		color24 = new JRadioButton("Magenta");
    		color25 = new JRadioButton("Cyan");
    		color26 = new JRadioButton("White");
    		group = new ButtonGroup();
    		group.add(color);
    		group.add(color2);
    		group.add(color3);
    		group.add(color4);
    		group.add(color5);
    		group.add(color6);
    		group.add(color7);
    		group.add(color8);
    		group.add(color9);
    		group.add(color10);
    		group.add(color11);
    		group.add(color12);
    		group.add(color13);
     
    		group2 = new ButtonGroup();
    		group2.add(color14);
    		group2.add(color15);
    		group2.add(color16);
    		group2.add(color17);
    		group2.add(color18);
    		group2.add(color19);
    		group2.add(color20);
    		group2.add(color21);
    		group2.add(color22);
    		group2.add(color23);
    		group2.add(color24);
    		group2.add(color25);
    		group2.add(color26);
     
     
     
    		background.setVisible(true);
    		int x = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    		int y = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
    		area = new JTextArea(120, 120);
     
     
    		scrolls = new JScrollPane(area, x, y);
    		panel.add(scrolls);
    		scrolls2 = new JScrollPane(panel, x, y);
    		panel.add(background);
    		panel.add(color);
    		panel.add(color2);
    		panel.add(color3);
    		panel.add(color4);
    		panel.add(color5);
    		panel.add(color6);
    		panel.add(color7);
    		panel.add(color8);
    		panel.add(color9);
    		panel.add(color10);
    		panel.add(color11);
    		panel.add(color12);
    		panel.add(color13);
    		panel.setVisible(true);
    		scrolls.setVisible(true);
    		scrolls2.setVisible(true);
    		songNames = new String[835];
    		box = new JCheckBox("Box");
    		panel.add(box);
    		box.setVisible(true);
    		panel.setVisible(true);
    		panel.add(scrolls);
    		// frame.add(scrolls);
     
     
    		panel.setName("Bob");
    		frame.setTitle("Bob 2");
    		 panel.add(scrolls);
    		 red = Color.RED;
    		 green = Color.GREEN;
    		 blue = Color.BLUE;
    		 orange = Color.ORANGE;
    		 yellow = Color.YELLOW;
    		 black = Color.BLACK;
    		 pink = Color.PINK;
    		 gray = Color.GRAY;
    		 lightGray = Color.LIGHT_GRAY;
    		 darkGray = Color.DARK_GRAY;
    		 magenta = Color.MAGENTA;
    		 white = Color.WHITE;
     
     
    		 copy = new JButton("Copy");
    		 panel.add(copy);
    		copy.setVisible(true);
     
    		textBackground = new JLabel("Set Background Color of Text Area");
    		panel.add(textBackground);
    		panel.add(color14);
    		panel.add(color15);
    		panel.add(color16);
    		panel.add(color17);
    		panel.add(color18);
    		panel.add(color19);
    		panel.add(color20);
    		panel.add(color21);
    		panel.add(color22);
    		panel.add(color23);
    		panel.add(color24);
    		panel.add(color25);
    		panel.add(color26);
     
    		textForeground = new JLabel("Set Color of Text in Text Area");
    		panel.add(textForeground);
    		panel.add(colors);
     
     
     
    		 color.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(red);
    					 }
    				 });
     
    		 color2.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(green);
    					 }
    				 });
     
    		 color3.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(blue);
    					 }
    				 });
     
    		 color4.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(orange);
    					 }
    				 });
     
    		 color5.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(yellow);
    					 }
    				 });
     
    		 color6.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(pink);
    					 }
    				 });
     
    		 color7.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(black);
    					 }
    				 });
     
    		 color8.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(gray);
    					 }
    				 });
     
    		 color9.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(lightGray);
    					 }
    				 });
     
    		 color10.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(darkGray);
    					 }
    				 });
     
    		 color11.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(magenta);
    					 }
    				 });
     
    		 color12.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(cyan);
    					 }
    				 });
     
    		 color13.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 panel.setBackground(white);
    					 }
    				 });
     
    		 color14.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(red);
    					 }
    				 });
     
    		 color15.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(green);
    					 }
    				 });
     
    		 color16.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(blue);
    					 }
    				 });
     
    		 color17.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(orange);
    					 }
    				 });
     
    		 color18.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(yellow);
    					 }
    				 });
     
    		 color19.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(pink);
    					 }
    				 });
     
    		 color20.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(black);
    					 }
    				 });
     
    		 color21.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(gray);
    					 }
    				 });
     
    		 color22.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(lightGray);
    					 }
    				 });
     
    		 color23.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(darkGray);
    					 }
    				 });
     
    		 color24.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(magenta);
    					 }
    				 });
     
    		 color25.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(cyan);
    					 }
    				 });
     
    		 color26.addActionListener(
    				 new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						 area.setBackground(white);
    					 }
    				 });
     
    		 colors.addItemListener(
    				 new ItemListener() {
    					 public void itemStateChanged(ItemEvent e)
    					 { // beginning of method
    						 if (e.getSource() == colors)
    						 { // beginning of if
    							 if (colors.getSelectedItem() == "Red")
    							 { // beginning of if
    								 if(e.getStateChange() == ItemEvent.SELECTED)
    								 { // beginning of if
    									 area.setForeground(red);
    								 } // end of if
     
    								 if (e.getStateChange() == ItemEvent.DESELECTED)
    								 { // beginning of if
    									 // do nothing
    								 } // end of if
     
    							 } // end of if
     
     
    						 if (colors.getSelectedItem() == "Green")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(green);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Blue")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(blue);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Orange")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(orange);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Yellow")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(yellow);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Pink")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(pink);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Black")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(black);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(gray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Light Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(lightGray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Dark Gray")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(darkGray);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Magenta")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(magenta);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "Cyan")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(cyan);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 if (colors.getSelectedItem() == "White")
     
    						 {// beginning of if
    							 if(e.getStateChange() == ItemEvent.SELECTED)
    							 { // beginning of if
    								 area.setForeground(white);
    							 } // end of if
     
    							 if (e.getStateChange() == ItemEvent.DESELECTED)
    							 { // beginning of if
    								 // do nothing
    							 } // end of if
    					 } // end of if
     
    						 } // end of if
    				 }});
     
    		 panel.setLayout(new BoxLayout(panel, 1));
     
    		 addWindowListener(
    				 new WindowListener(){
     
    					@Override
    					public void windowActivated(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowClosed(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowClosing(WindowEvent window) {
    						// TODO Auto-generated method stub
    						Object[] closing=  {"Yes" , "No"};
    						int a = JOptionPane.showOptionDialog(frame2,
    								"Exit.",
    								"Are you sure you want to exit?",
    								JOptionPane.YES_NO_OPTION,
    								JOptionPane.QUESTION_MESSAGE,
    								null,        closing,     closing[0]); 
    						if (a == JOptionPane.YES_OPTION) 
    						{
    							System.exit(0);
    						}
     
    						else if (a == JOptionPane.NO_OPTION)
    						{ // beginning of else if
     
    							setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    						} // end of else if
     
    					}
     
    					@Override
    					public void windowDeactivated(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowDeiconified(WindowEvent window) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowIconified(WindowEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void windowOpened(WindowEvent arg0) {
    						// TODO Auto-generated method stub
    						String str = "Welcome to the hymn book song selector.  Choose from" + "\n" +
    						" different options to set background color, text area color, font color, and font type." + "\n" +
    						"  Chose the songs from the list or enter page number to get songs.  It will show all verses as I don't" + "\n" +
    						" have time to set it to do it for each verse so you have to scroll down to get to the next verses.";
    						JOptionPane.showMessageDialog(null, str, "Welcome", JOptionPane.PLAIN_MESSAGE);
     
     
    					}
     
    				 });
     
    		 setFocusable(true);
    		 addKeyListener(
    				 new KeyListener() {
     
    					@Override
    					public void keyPressed(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    							if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
    							{
    							Object[] Esc=  {"Yes" , "No"};
    							int a = JOptionPane.showOptionDialog(frame2,
    									"Exit.",
    									"Are you sure you want to exit?",
    									JOptionPane.YES_NO_OPTION,
    									JOptionPane.QUESTION_MESSAGE,
    									null,        Esc,     Esc[0]); 
    							if (a == JOptionPane.YES_OPTION) 
    							{
    								System.exit(0);
    							}
     
    							else if (a == JOptionPane.NO_OPTION)
    							{ // beginning of else if
     
    								setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    							} // end of else if
    						}
     
     
    					}
     
    					@Override
    					public void keyReleased(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    					@Override
    					public void keyTyped(KeyEvent arg0) {
    						// TODO Auto-generated method stub
     
    					}
     
    				 });
     
     
    		 	 getContentPane().add(scrolls2);
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    		// TODO Auto-generated constructor stub
    	}
     
    	public void showGUI2(){
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		setLocation(100,100);
    		setVisible(true);
    }
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
         LivingHymns hymn = new LivingHymns();
         hymn.showGUI2();
     
    	}
     
    }
     
    	}
    Last edited by javapenguin; July 17th, 2010 at 10:19 PM. Reason: I realized a key listener would certainly help


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting a TextField to activate ItemListener.

    You can use a DocumentListener to listen for changes to the text inside JTextComponents.

    To add a document listener:

    ((AbstractDocument)theTextField.getDocument()).addDocumentListener(theListener);

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (July 17th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Getting a TextField to activate ItemListener.

    Quote Originally Posted by helloworld922 View Post
    You can use a DocumentListener to listen for changes to the text inside JTextComponents.

    To add a document listener:

    ((AbstractDocument)theTextField.getDocument()).addDocumentListener(theListener);
    Ok, but I have no clue what that is, and does it have anything to do with a text document.

    Would a KeyListener work instead?

    JTextField does have the method addKeyListener(), but how do you get it to listen for more than one key typed or if you type Shift and a key, etc?


  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Getting a TextField to activate ItemListener.

    Quote Originally Posted by javapenguin View Post
    Ok, but I have no clue what that is, and does it have anything to do with a text document.

    Would a KeyListener work instead?

    Ditto helloworlds advice....DocumentListener would work for you...see DocumentListener (Java 2 Platform SE 5.0)
    Just implement the interface to listen for events, updating what you need when the event is fired. KeyListener would work, but could turn into a pain quickly.
    Also, my JTextField is somehow coming out too big and acting too much like a JTextArea. Except it only has one line, though it can get going on that one line
    Try calling setPreferredSize and setMaximumSize to get that JTextField to the dimension you want.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Getting a TextField to activate ItemListener.

    Document listeners allow you to listen for when the text inside the text box actually changes. So, you only have to change the combo box's select when the text changes. You can also just parse the number inside the text field and not worry about any lower-level key press processing which may work, but is very difficult to have it correct in all cases.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (July 20th, 2010)

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Getting a TextField to activate ItemListener.

    Quote Originally Posted by helloworld922 View Post
    Document listeners allow you to listen for when the text inside the text box actually changes. So, you only have to change the combo box's select when the text changes. You can also just parse the number inside the text field and not worry about any lower-level key press processing which may work, but is very difficult to have it correct in all cases.
    The ComboBox is already going to have ItemListeneres. All the KeyListeners would do is call setSelected() for a particular item, and that should work, right?

    If not, JTextField doesn't have a method called addDocumentListener().


Similar Threads

  1. ComboBox and TextField
    By fahad in forum AWT / Java Swing
    Replies: 2
    Last Post: May 31st, 2010, 08:47 AM
  2. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  3. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 12:57 PM
  4. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  5. Item Listener problem with destroying Midlet
    By hanizainol in forum Java Applets
    Replies: 0
    Last Post: July 22nd, 2008, 08:59 PM