Solved

How to make radiobuttons and JList do the same thing??

Posted on 2002-07-08
8
293 Views
Last Modified: 2010-08-05
Hi!  I'm trying to take the text and change it to Bold, Plain, or Italic.  I can do this with radio buttons, but I am having trouble trying to make the same thing happen when trying it on the same JFrame with a JList selection.  Also, I'm trying to make the changed text display onto a label which I am also having trouble...can anyone help?  Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pro2 extends JFrame
{
 ///////  private JLabel labeloutput;
   private JTextField t;
   private Font plainFont,
                  boldFont,
                italicFont;
   private JRadioButton plain,
                          bold,
                                    italic;
   private ButtonGroup radioGroup;

    private JList fontList;


      private String fontNames[] =
         {"Plain", "Bold", "Italic"};

   public Pro2()
   {
      super("Font Manipulator");

      Container c = getContentPane();
      c.setLayout(new FlowLayout());


      JMenuBar bar = new JMenuBar();
      JMenu fileMenu = new JMenu( "File" );
      JMenu helpMenu = new JMenu( "Help" );
      bar.add( fileMenu );
      bar.add( helpMenu );
      setJMenuBar( bar );


/////  labeloutput = new JLabel( "", 25);
/////  labeloutput.setHorizontalTextPosition( SwingConstants.CENTER );
/////  labeloutput.setVerticalTextPosition( SwingConstants.TOP );
      t = new JTextField("", 25);
/////  c.add(labeloutput);
      c.add(t);

      // Create radio buttons
      plain = new JRadioButton("Plain", true);
      c.add(plain);
      bold = new JRadioButton("Bold", false);
      c.add(bold);
      italic = new JRadioButton("Italic", false);
      c.add(italic);

      // register events
      RadioButtonHandler handler = new RadioButtonHandler();
      plain.addItemListener(handler);
      bold.addItemListener(handler);
      italic.addItemListener(handler);

      // create logical relationship between JRadioButtons
      radioGroup = new ButtonGroup();
      radioGroup.add(plain);
      radioGroup.add(bold);
      radioGroup.add(italic);

      plainFont = new Font("TimesRoman", Font.PLAIN, 14);
      boldFont = new Font("TimesRoman", Font.BOLD, 14);
      italicFont = new Font("TimesRoman", Font.ITALIC, 14);

  ///////////labeloutput.setFont(plainFont);
      t.setFont(plainFont);



      // create a list with the items in the fontNames array
        fontList = new JList(fontNames);
        fontList.setVisibleRowCount(1);

        // do not allow multiple selections
        fontList.setSelectionMode(
            ListSelectionModel.SINGLE_SELECTION);

        // add a JScrollPane containing the JList
        // to the content pane
        c.add(new JScrollPane(fontList));

              // set up event handler
///   private class RadioButtonHandler implements ItemListener {
///   public void itemStateChanged(ItemEvent e)
       // /fontList.addListSelectionListener(
       // /    new ListSelectionListener() {

      private class RadioButtonHandler implements ItemListener  {
        public void valueChanged(ListSelectionEvent e)
                    {
                       //c.setBackground(
                       //   colors[fontList.getSelectedIndex()]);
                     if (fontList.getSelectedIndex() == "Plain")
                                           //t = labeloutput;
                                     //labeloutput.setFont(plainFont);
                               t.setFont(plainFont);
                          else if (fontList.getSelectedIndex() == "Bold")
                                           //t = labeloutput;
                                           //labeloutput.setFont(boldFont);
                                   t.setFont(boldFont);
                               else if (fontList.getSelectedIndex() == "Italic")
                                           //t = labeloutput;
                                           //labeloutput.setFont(italicFont);
                                   t.setFont(italicFont);

                                        //labeloutput.repaint();
                                 t.repaint();

                    }
                 }
          );

      setSize(400, 400);
      show();
   }  // RadioButtonTest()

   public static void main(String args[])
   {
      Pro2 app = new Pro2();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
               System.exit(0);
            }
         }
      );
   }

   private class RadioButtonHandler implements ItemListener {
      public void itemStateChanged(ItemEvent e)
      {
         if (e.getSource() == plain)
            //t = labeloutput;
            //labeloutput.setFont(plainFont);
            t.setFont(plainFont);
         else if (e.getSource() == bold)
            //t = labeloutput;
            //labeloutput.setFont(boldFont);
            t.setFont(boldFont);
         else if (e.getSource() == italic)
            //t = labeloutput;
            //labeloutput.setFont(italicFont);
            t.setFont(italicFont);

         //labeloutput.repaint();
         t.repaint();
      }
   }
}  // RadioButtonTest class

A fix to the code would be nice..
Thank you!!
0
Comment
Question by:Redtracer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7137154
This won't work:

(fontList.getSelectedIndex() == "Plain")

Instean do:

("Plain".equals(fontList.getSelectedValue())
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 7137165
So the method should look something like this:

public void valueChanged(ListSelectionEvent e)
{
   String selected = fontList.getSelectedValue();
   if ("Plain".equals(selected))
   {
     t.setFont(plainFont);
   }
   else if ("Bold".equals(selected))
   {
     t.setFont(boldFont);
   }
   else if ("Italic".equals(selected))
   {
     t.setFont(italicFont);
   }
   t.repaint();
}
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7138209
The handler would look like this:
private class RadioButtonHandler implements ItemListener {
     public void itemStateChanged(ItemEvent e)
     {int selectedFont= fontList.getSelectedIndex();
        if (selectedFont == 0)
           t.setFont(plainFont);
        else if (selectedFont == 1)
           t.setFont(boldFont);
        else if (selectedFont == 2)
           t.setFont(italicFont);
     }
}
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Expert Comment

by:objects
ID: 7139038
That assumes that the array of font styles remains the same :-) Testing the actual value makes it more flexible, as the array order can be altered without affecting any other code.
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7140335
> That assumes that the array of font styles remains the same

If you want more flexibility you can change the code to:
private class RadioButtonHandler implements ItemListener {
    public void itemStateChanged(ItemEvent e)
    {Object selectedFont= fontList.getSelectedValue();
       if (selectedFont.equals("Plain"))
          t.setFont(plainFont);
       else if (selectedFont.equals("Bold"))
          t.setFont(boldFont);
       else if (selectedFont.equals("Italic"))
          t.setFont(italicFont);
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 7140362
Thats exactly what I posted??
Except that it will crash if nothing is selected.
0
 

Author Comment

by:Redtracer
ID: 7170206
Sorry it took so long to award points!  I just had to try the code and verify it...your solution helped very much.  Thanks

Redtracer
0
 
LVL 92

Expert Comment

by:objects
ID: 7170560
Why only a 'B'.

Thanks for the points :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question