Redtracer
asked on
How to make radiobuttons and JList do the same thing??
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.setHorizontalT extPositio n( SwingConstants.CENTER );
///// labeloutput.setVerticalTex tPosition( 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(hand ler);
bold.addItemListener(handl er);
italic.addItemListener(han dler);
// 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.set Font(plain Font);
t.setFont(plainFont);
// create a list with the items in the fontNames array
fontList = new JList(fontNames);
fontList.setVisibleRowCoun t(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.addListSelection Listener(
// / new ListSelectionListener() {
private class RadioButtonHandler implements ItemListener {
public void valueChanged(ListSelection Event e)
{
//c.setBackground(
// colors[fontList.getSelecte dIndex()]) ;
if (fontList.getSelectedIndex () == "Plain")
//t = labeloutput;
//labeloutput.setFont(plai nFont);
t.setFont(plainFont);
else if (fontList.getSelectedIndex () == "Bold")
//t = labeloutput;
//labeloutput.setFont(bold Font);
t.setFont(boldFont);
else if (fontList.getSelectedIndex () == "Italic")
//t = labeloutput;
//labeloutput.setFont(ital icFont);
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(plai nFont);
t.setFont(plainFont);
else if (e.getSource() == bold)
//t = labeloutput;
//labeloutput.setFont(bold Font);
t.setFont(boldFont);
else if (e.getSource() == italic)
//t = labeloutput;
//labeloutput.setFont(ital icFont);
t.setFont(italicFont);
//labeloutput.repaint();
t.repaint();
}
}
} // RadioButtonTest class
A fix to the code would be nice..
Thank you!!
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.setHorizontalT
///// labeloutput.setVerticalTex
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(hand
bold.addItemListener(handl
italic.addItemListener(han
// 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.set
t.setFont(plainFont);
// create a list with the items in the fontNames array
fontList = new JList(fontNames);
fontList.setVisibleRowCoun
// do not allow multiple selections
fontList.setSelectionMode(
ListSelectionModel.SINGLE_
// 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
// /fontList.addListSelection
// / new ListSelectionListener() {
private class RadioButtonHandler implements ItemListener {
public void valueChanged(ListSelection
{
//c.setBackground(
// colors[fontList.getSelecte
if (fontList.getSelectedIndex
//t = labeloutput;
//labeloutput.setFont(plai
t.setFont(plainFont);
else if (fontList.getSelectedIndex
//t = labeloutput;
//labeloutput.setFont(bold
t.setFont(boldFont);
else if (fontList.getSelectedIndex
//t = labeloutput;
//labeloutput.setFont(ital
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
{
if (e.getSource() == plain)
//t = labeloutput;
//labeloutput.setFont(plai
t.setFont(plainFont);
else if (e.getSource() == bold)
//t = labeloutput;
//labeloutput.setFont(bold
t.setFont(boldFont);
else if (e.getSource() == italic)
//t = labeloutput;
//labeloutput.setFont(ital
t.setFont(italicFont);
//labeloutput.repaint();
t.repaint();
}
}
} // RadioButtonTest class
A fix to the code would be nice..
Thank you!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}
}
private class RadioButtonHandler implements ItemListener {
public void itemStateChanged(ItemEvent
{int selectedFont= fontList.getSelectedIndex(
if (selectedFont == 0)
t.setFont(plainFont);
else if (selectedFont == 1)
t.setFont(boldFont);
else if (selectedFont == 2)
t.setFont(italicFont);
}
}
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.
> 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("Plai n"))
t.setFont(plainFont);
else if (selectedFont.equals("Bold "))
t.setFont(boldFont);
else if (selectedFont.equals("Ital ic"))
t.setFont(italicFont);
}
}
If you want more flexibility you can change the code to:
private class RadioButtonHandler implements ItemListener {
public void itemStateChanged(ItemEvent
{Object selectedFont= fontList.getSelectedValue(
if (selectedFont.equals("Plai
t.setFont(plainFont);
else if (selectedFont.equals("Bold
t.setFont(boldFont);
else if (selectedFont.equals("Ital
t.setFont(italicFont);
}
}
Thats exactly what I posted??
Except that it will crash if nothing is selected.
Except that it will crash if nothing is selected.
ASKER
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
Redtracer
Why only a 'B'.
Thanks for the points :-)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
Thanks for the points :-)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
(fontList.getSelectedIndex
Instean do:
("Plain".equals(fontList.g