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

Thread: Combobox problem

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Combobox problem

    Hello,

    My idea is to create a drop down menu from where I can choose between a few fonts and font styles.

    Here is the code which I use:

    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    import java.io.*;
    import javax.swing.*; 
    import javax.swing.text.*;
    import java.util.*;
    import java.awt.Font;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class appleted extends Applet implements ActionListener
    {
    TextArea t;
     
    Button copy,cut,paste;
    // String[] description = { "Courier", "Sans Serif",
        //"Monospaced","Symbol" };
     
    String selection;
     
      private JComboBox fontComboBox;
      public void FontComboBox() 
      {
       // setTitle("ComboBoxTest");
       // setSize(300, 200);
        //fontComboBox.addActionListener(this);
     
     
     
       /* JPanel p = new JPanel();
        p.add(fontComboBox);
        getContentPane().add(p, "North");
        getContentPane().add(fontLabel, "Center");*/
    this.validate();
      }
     
    public void init()
    {
       setBackground(Color.white);
    t=new TextArea(10,40);
    add(t);
     
     
    copy=new Button("copy");
    add(copy);
    copy.addActionListener(this);
     
    cut=new Button("cut");
    add(cut);
    cut.addActionListener(this);
     
    paste=new Button("paste");
    add(paste);
    paste.addActionListener(this);
     
    fontComboBox = new JComboBox();
        fontComboBox.setEditable(true);
        fontComboBox.addItem("Serif");
        fontComboBox.addItem("SansSerif");
        fontComboBox.addItem("Monospaced");
        fontComboBox.addItem("Dialog");
        fontComboBox.addItem("DialogInput");
        fontComboBox.addActionListener(this);
    this.validate();
     
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==cut) cutAction();
    else if (e.getSource()==copy) copyAction();
    else if (e.getSource()==paste) pasteAction();
    this.validate();
    }
    private void copyAction()
    {
    selection=t.getSelectedText();
    }
    private void cutAction()
    {
    int start=t.getSelectionStart(),
    end=t.getSelectionEnd();
    selection=t.getSelectedText();
    t.replaceRange("",start,end);
    t.setSelectionEnd(start);
    }
    private void pasteAction()
    {
    int start=t.getSelectionStart(),
    end=t.getSelectionEnd();
    t.replaceRange(selection,start,end);
    }
     
    }

    Then I include the java applet into a html page. The problem is that the menu for changing the font and its style never show up altough the compiler says no errors.

    Can somebody help me?

    Thank you very much in advance.
    Last edited by Norm; April 4th, 2013 at 09:02 AM. Reason: Change quote to code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Combobox problem

    Please edit the code in the post and give it proper formatting. It has too many statements starting in the first column. Statements within {}s should be indented 3-4 spaces to show nesting levels and make the code easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Facing problem with combobox
    By Chandu311 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 16th, 2012, 05:52 PM
  2. Combobox Problem
    By hurleyga in forum AWT / Java Swing
    Replies: 1
    Last Post: December 7th, 2011, 08:55 AM
  3. [SOLVED] Populating second combobox from a combobox help!
    By ComputerSaysNo in forum AWT / Java Swing
    Replies: 7
    Last Post: October 18th, 2011, 09:01 AM
  4. problem related combobox nd database
    By harsimran in forum Java Theory & Questions
    Replies: 1
    Last Post: September 9th, 2011, 02:27 PM
  5. combobox unicode problem
    By trexi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 18th, 2011, 11:06 AM