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 1 of 1

Thread: how i pass the selected item on the jcombo box into external listener class ?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how i pass the selected item on the jcombo box into external listener class ?

    hey guys

    i'm having trouble getting this jcombo x to work , i modified my code more than 20 times with no lucks can you give me any advice , am i missing something here ?




    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class FileViewer extends JFrame {
    	// Declare attributes
    	private JTextArea jta;
    	private JComboBox fontBox, sizeBox;
    	private JButton JsetFont;
    	private JTextField txt;
    	private JLabel label;
     
    	public FileViewer() {
    		// JFrame Setup
    		setTitle("FileViewer");
    		setSize(600, 400);
    		setLocation(400, 400);
    		Container ct = getContentPane();
     
     
     
                      BLA BLA BLA  GUI component
     
     
     
    		// Create the combo box,.
    		String[] fonts = { "Regular", "Italic", "Bold", "ItalicBold" };
     
     
    		fontBox = new JComboBox(fonts);
    		String[] size = { "10", "12", "15" };
    		sizeBox = new JComboBox(size);
     
     
                      i found this index nethod on javadoc and it doesnt work !
    		sizeBox.setSelectedIndex(2);
     
    		JsetFont = new JButton("Set Font");
     
     
     
    		// Listener Registration
    		FileListener flr = new FileListener(wordCount,jta,JsetFont,fontBox, sizeBox);
    		jmiNew.addActionListener(flr);
    		jmiOpen.addActionListener(flr);
    		jmiSave.addActionListener(flr);
    		jmiSaveAs.addActionListener(flr);
    		jmiExit.addActionListener(flr);
    		fontBox.addItemListener(flr);
    		sizeBox.addActionListener(flr);
    		wordCount.addActionListener(flr);
     
     
     
    		HelpListener help = new HelpListener();
    		jmiAbout.addActionListener(help);
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		FileViewer fv = new FileViewer();
    	}
    }

    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
     
    import java.awt.event.*;
     
    public class FileListener extends JFrame implements ActionListener  , ItemListener {
    	// buffered reader
    	BufferedReader in = null;
    	BufferedWriter out = null;
    	String record = null;
     
    	// attribute
    	private JTextArea jta;
    	private JComboBox fType, size;
    	private JFileChooser fileChoose;
    	private String fileName, textArea, line, output;
    	private File file, dir;
    	private int result; //style;
    	private StringBuffer sb;
    	//private boolean bold, italic;
    	//private Font defaultFont, font;
    	private JButton setFont;
    	private JMenuItem cont;
     
    	public FileListener(JMenuItem wordCount, JTextArea _jta, JButton jsetFont,
    			JComboBox fontType, JComboBox fontSize) {
    		jta = _jta;
    		fType = fontType;
    		size = fontSize;
    		setFont = jsetFont;
    		cont = wordCount;
     
    	}
     
    	public void actionPerformed(ActionEvent ae) {
    		String action = ae.getActionCommand();
     
    		if (action.equals("New")) {
    			if (fileName != null) {
    				jta.setText("");
    				fileName = null;
     
    			} else {
    				jta.setEditable(true);
    			}
    		}
    		if (action.equals("Open")) {
    			// create file chooser
    			dir = new File(".");
    			fileChoose = new JFileChooser(dir);
    			result = fileChoose.showOpenDialog(jta);
    			file = fileChoose.getSelectedFile();
    			if (file != null) {
    				fileName = file.getName();
     
    				if (file != null && result == JFileChooser.APPROVE_OPTION) {
    					try {
    						in = new BufferedReader(new FileReader(file));
    						sb = new StringBuffer(); // holds the incoming data
     
    						// set the text area to editable
    						jta.setEditable(true);
     
    						// read the data file
    						line = null;
    						while ((line = in.readLine()) != null) {
    							sb.append(line + "\n");
    						}
    						// place the data in the text area
    						jta.setText(sb.toString());
    					} catch (IOException ieo) {
    						ieo.printStackTrace();
    					} catch (Exception exc) {
    						exc.printStackTrace();
    					} finally {
    						try {
    							if (in != null)
    								in.close();
    						} catch (IOException ioe) {
    							ioe.printStackTrace();
    						}
    					}
    				}
     
    			}
    		}
    		if (action.equals("Save")) {
    			if (fileName != null) {
    				try {
    					// set the text area to editable
    					jta.setEditable(true);
     
    					out = new BufferedWriter(new FileWriter(fileName));
    					out.write(jta.getText());
    				} catch (IOException ieo) {
    					ieo.printStackTrace();
    				} catch (Exception exc) {
    					exc.printStackTrace();
    				} finally {
    					try {
    						if (out != null)
    							out.close();
    					} catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
     
    			}
    		}
    		if (action.equals("Save As")) {
    			// create file chooser
    			dir = new File(".");
    			fileChoose = new JFileChooser(dir);
    			result = fileChoose.showSaveDialog(jta);
    			file = fileChoose.getSelectedFile();
    			fileName = file.getName();
    			if (file != null && result == JFileChooser.APPROVE_OPTION) {
    				try {
    					out = new BufferedWriter(new FileWriter(fileName));
    					out.write(jta.getText());
    				} catch (IOException ieo) {
    					ieo.printStackTrace();
    				} catch (NullPointerException npe) {
    					npe.printStackTrace();
    				} catch (Exception exc) {
    					exc.printStackTrace();
    				} finally {
    					try {
    						if (out != null)
    							out.close();
    					} catch (IOException ioe) {
    						ioe.printStackTrace();
    					}
    				}
    			}
    		}
    		if(action.equals("Word Count")){
    			String myText = jta.getText();
    			Scanner sc = new Scanner(myText);
     
    			int counter = 0;
    			String words;
    			String storedLine;
     
    			while (sc.hasNextLine()) {
    				storedLine = sc.nextLine();
    				Scanner line = new Scanner(storedLine);
     
    				while (line.hasNext()) {
    					words = line.next();
    					counter++;
    				}
     
     
    			}
    			JOptionPane.showMessageDialog(null, "Word Count:"+ counter);
     
     
     
     
     
    		}
     
    		[COLOR="DarkRed"]if(action.equals("set Font")){
     
     
    			// here is my problem i tried to put the itemevent inside but it wont compile 
                            //  + plus how i can combine the font action at the same time so when the user 
                            //i have to use the Jbutton like in the pic :(
    [/COLOR]
     
     
    		}
    	}
     
     
     
     
     
    	}
    }




    plus i found this code on the Internet and its works fine when i use it with JcheckBox but not with
    the combo
    its driving me crazy because its so short and work perfectly , i don't get ! usually i go like (if ( bold.isSlected set the j textArea to bold then another if else for italic condition and third one for them both , but it never work when ever i start to selcted them both then deselecte etc
    i tryied to check the selection for all the checkboxes like this (if bold.is selected go and check the italic but it works for the first selection, + plus it's really long i wanna do it like this code )


    	public void itemStateChanged(ItemEvent ie) {
     
    		setState();
     
    	}
     
     
     
    private void setState() {
    		String Text;
    		int Style;
    		Font font;
     
    		Text = jta.getText();
    		Style = 0;
    		if (Bold.isSelected()) {
    			Style = Style | Font.BOLD;
    		} else {
    		}
    		if (Italic.isSelected()) {
    			Style = Style | Font.ITALIC;
    		} else {
    		}
     
    		font = new Font("SansSerif", Style, 12);
    		jta.setFont(font);
    		jta.setText(Text);
    Attached Images Attached Images


Similar Threads

  1. 'pass by value' and 'pass by reference'
    By pokuri in forum Object Oriented Programming
    Replies: 5
    Last Post: January 26th, 2011, 11:30 AM
  2. Running a external exe in Mac OS
    By supertreta in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 15th, 2010, 01:32 PM
  3. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  4. Interaction with external application
    By righi in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2010, 08:37 AM
  5. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM