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);