-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
1.2.0, 1.2.2
-
x86
-
windows_nt
the JTextArea is not updated. However, if the font size or the font name is also
modified, then the Font in the JTextArea is correctly updated.
Demo program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaDialog extends JDialog
{
private JComboBox cmbFontNames = null;
private JComboBox cmbFontSize = null;
private JCheckBox chkItalic = null;
private JCheckBox chkBold = null;
private JTextArea txtArea = null;
/**
* Constructor
**/
public JTextAreaDialog ()
{
super ((JFrame) null, "TextArea Demo", true);
displayInterface ();
}
/**
* displays graphical interface
**/
private void displayInterface ()
{
JPanel panel = new JPanel (new BorderLayout ());
// create text area
txtArea = new JTextArea ();
JScrollPane scrollPane = new JScrollPane (txtArea);
panel.add (scrollPane, BorderLayout.CENTER);
// create combo-box for font names
cmbFontNames = new JComboBox (GraphicsEnvironment.
getLocalGraphicsEnvironment ().getAvailableFontFamilyNames ());
// create combo-box for font sizes
final Integer [] fontSizes = new Integer [50];
for (int i=0; i < 50; i++)
fontSizes [i] = new Integer ((i+1) * 2);
cmbFontSize = new JComboBox (fontSizes);
cmbFontSize.setSelectedItem (new Integer (12));
// create check boxes for bold and italic
chkBold = new JCheckBox ("Bold");
chkItalic = new JCheckBox ("Italic");
// create font change listener
ActionListener fontChangeListener = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
txtArea.setFont (createFont ());
txtArea.repaint ();
}
};
cmbFontNames.addActionListener (fontChangeListener);
cmbFontSize.addActionListener (fontChangeListener);
chkItalic.addActionListener (fontChangeListener);
chkBold.addActionListener (fontChangeListener);
// add the selection components to GUI
JPanel subPanel = new JPanel (new FlowLayout (FlowLayout.CENTER));
subPanel.add (cmbFontNames);
subPanel.add (cmbFontSize);
subPanel.add (chkItalic);
subPanel.add (chkBold);
panel.add (subPanel, BorderLayout.NORTH);
// display graphical components
getContentPane ().add (panel);
setSize (520, 220);
setVisible (true);
txtArea.setFont (createFont ());
txtArea.requestFocus ();
}
/**
* creates a font object from the current user-selection
**/
private Font createFont ()
{
int fontType;
if (chkBold.isSelected () && chkItalic.isSelected ())
fontType = Font.BOLD | Font.ITALIC;
else if (chkBold.isSelected () && ! chkItalic.isSelected ())
fontType = Font.BOLD;
else if (! chkBold.isSelected () && chkItalic.isSelected ())
fontType = Font.ITALIC;
else
fontType = Font.PLAIN;
Font f = new Font (
(String) cmbFontNames.getSelectedItem (),
fontType,
((Integer) cmbFontSize.getSelectedItem ()).intValue ());
System.out.println ("Font created: " + f.toString ());
return f;
}
/**
* Main method for testing purposes.
**/
public static void main (String [] args)
{
new JTextAreaDialog ();
System.exit (0);
}
}
(Review ID: 99455)
======================================================================
Name: dbT83986 Date: 12/16/98
Create a JTextarea and then 3 JComboBoxes to
select font family, font style and font size.
Add actionlisteners for combo boxes and an action
event handler which builds a new font from your
selections, sets the textarea font to the new font
and then repaints the text area.
Type some stuff in the text area. Select a
different font family, this should work OK and
your text will appear in the new font.
Select a different font size. This should work OK
and you will see a larger font.
Select a different font style (bold, italic,
plain) and no change occurs. This will happen
regardless how many times you select a new font
style. However, selecting any of the other combo
boxes will update the font and include your style
selection!
The section of code below is the action event
handler which sets up the new font. Putting debug
into every line shows that all is working as
expected, and all variables for the font are being
set correctly. Even the font style selection is
being correctly interpreted.
//dgfonty is an int declared elsewhere but not touched elsewhere.
//dgCB is the family name combo box
//cbFontType is the font style (plain, bold etc) combo box
//cbFontSize is the font size combo box
//Check if a combo box is the culprit
if (obj.equals(dgCB) || obj.equals(cbFontType) || obj.equals(cbFontSize)) //ComboBoxes
{
if( cbFontType.getSelectedItem().equals("Plain"))
dgfonty = Font.PLAIN;
else
if(cbFontType.getSelectedItem().equals("Bold"))
dgfonty = Font.BOLD;
else
if(cbFontType.getSelectedItem().equals("Italic"))
dgfonty = Font.ITALIC;
editWindowFont = new Font( (String)dgCB.getSelectedItem(), dgfonty, Integer.parseInt((String)cbFontSize.getSelectedItem()));
editWindow.setFont(editWindowFont);
editWindow.repaint();
}
=========================
REVIEW NOTE 12/16/98 - User responded with additional info
I get a list of system fonts for the font names combo box at launch
time. I have found that the one called "default" causes the style
selection to work. At first I thought "Maybe it's just the fonts I'm
selecting" but it can't be that because if I chose one of the other
fonts and change the size for example, it suddenly implements the style
change as well. However just changing the style does nothing.
Other things I may not have mentioned in the original report are :-
1) I'm using Win NT 4 service pack 4.
2) Kawa IDE to kick off the prog although that is set to use the Sun
JDK1.2 as it doesn't have one of its own.
2 source files follow:
/* Text editor
Dave Gabol - DHL Ireland - 11/12/1998
Learning/testing JDK1.2 final release
Requires JDK1.2 to compile
Requires JDK/JRE 1.2 to run
Parameters - None
*/
import javax.swing.*;//Lightweight components
import java.awt.*;//Other GUI items
import java.awt.event.*;//Handle GUI events
import java.io.*;//File I/O
import java.awt.print.*;
import java.awt.Toolkit.*;
import javax.accessibility.*;
//Main class
public class dgLocalTextEdit extends JFrame implements ActionListener
{
//Classes available to all methods
dgTextArea editWindow = new dgTextArea();
JScrollPane editWindowScroller = new JScrollPane(editWindow);
JPanel editPanel = new JPanel();//Panel to hold all items
File saveFile;//File to save
File openFile;//File to open
JMenuBar mainMenuBar = new JMenuBar();//Main menu bar
JMenu fileMenu = new JMenu("File", true);//File menu
JMenu editMenu = new JMenu("Edit", true);//Edit menu
JToolBar tools = new JToolBar();//Main toolbar
JPanel toolPan = new JPanel();//Panel to hold toolbar
JPanel topPan = new JPanel();//Panel to hold toolbar panel and menubar
JButton cutBut;//Cut button
JButton copyBut;//Copy
JButton pasteBut;//Paste
JButton openBut;//Open file
JButton saveBut;//Save file
JButton printBut;//Print
String[] dgFonts;//Stores list of fonts available
GraphicsEnvironment dgGraphicsEnvironment;//To get list of fonts
JComboBox dgCB;//Display list of font families
JComboBox cbFontType;//Selects font type (plain, bold etc)
JComboBox cbFontSize;//Selects font size
int dgfonty = Font.PLAIN;//Integer for font style
Font editWindowFont;//Font for the edit window
JMenuItem openIt;//Items for the menus
JMenuItem closeIt;
JMenuItem saveIt;
JMenuItem cutIt;
JMenuItem copyIt;
JMenuItem pasteIt;
Insets edWinInset = new Insets(6,6,6,6);
//Start program
public static void main(String[] args)
{
//Instantiate an object
dgLocalTextEdit textEd1 = new dgLocalTextEdit();
//Set window to dispose on close
textEd1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
textEd1.init();//Run the init method to set everything up
textEd1.setSize(600,500);//Resize the frame
textEd1.setVisible(true);//Make it visible
}
//Initialise application
public void init()
{
setSize(500,450);//Set applet size
//Set the layout of the panel and the toolbar
toolPan.setLayout(new BorderLayout());
tools.setLayout( new GridLayout(1,8));
toolPan.add("North", tools);//Add toolbar to panel
//Get the toolkit for the local machine
Toolkit dgToolkit = Toolkit.getDefaultToolkit();
//Create a couple of buttons just with icons for the savebar and add them
//Use the toolkit to get the images from disk
//Use createImage rather than getImage as recommended by Sun
openBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\dir.gif")));
openBut.setToolTipText("Open file");
saveBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\disk.gif")));
saveBut.setToolTipText("Save file");
printBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\print.gif")));
printBut.setToolTipText("Print file");
tools.add(openBut);//Add to toolbar
tools.add(saveBut);
tools.add(printBut);
//Create some buttons just with icons for the editbar and add them
cutBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\cut.gif")));
cutBut.setToolTipText("Cut");
copyBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\copy.gif")));
copyBut.setToolTipText("Copy");
pasteBut = new JButton(new ImageIcon(dgToolkit.createImage("c:\\javaprojects\\dgTextEdit\\paste.gif")));
pasteBut.setToolTipText("Paste");
tools.add(cutBut);//Add to toolbar
tools.add(copyBut);
tools.add(pasteBut);
//Get list of fonts on system
dgGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
dgFonts = dgGraphicsEnvironment.getAvailableFontFamilyNames();
dgCB = new JComboBox( (Object[]) dgFonts);
tools.add(dgCB);
//make list of font types
String[] dgFontTypes = {"Plain","Bold","Italic"};
cbFontType = new JComboBox(dgFontTypes);
tools.add(cbFontType);
//Make list of font sizes
Object[] dgFontSize = {"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29"};
cbFontSize = new JComboBox(dgFontSize);
cbFontSize.setSelectedItem("14");
tools.add(cbFontSize);
//Set default font for editor window
editWindowFont = new Font( (String)dgCB.getSelectedItem(), Font.PLAIN, Integer.parseInt((String)cbFontSize.getSelectedItem()));
editWindow.setFont(editWindowFont);
editWindow.setMargin(edWinInset);
//Add main menu and tool panel to top panel
topPan.setLayout(new GridLayout(2,1));
topPan.add(mainMenuBar);
topPan.add(toolPan);
//Set up main panel and add objects
editPanel.setLayout(new BorderLayout());
editPanel.add("North", topPan);//Contains menu and toolbar
editPanel.add("Center", editWindowScroller);//Contains text editor
//Set up menus
openIt = new JMenuItem("Open");
closeIt = new JMenuItem("Close");
saveIt = new JMenuItem("Save");
cutIt = new JMenuItem("Cut");
copyIt = new JMenuItem("Copy");
pasteIt = new JMenuItem("Paste");
mainMenuBar.add(fileMenu);
fileMenu.add(openIt);
fileMenu.add(closeIt);
fileMenu.add(saveIt);
mainMenuBar.add(editMenu);
editMenu.add(cutIt);
editMenu.add(copyIt);
editMenu.add(pasteIt);
//Add everything to the main frame
getContentPane().add(editPanel);//Add everything to the application
//Set up application to listen for all action events
cutBut.addActionListener(this);
copyBut.addActionListener(this);
pasteBut.addActionListener(this);
openBut.addActionListener(this);
saveBut.addActionListener(this);
printBut.addActionListener(this);
dgCB.addActionListener(this);
cbFontType.addActionListener(this);
cbFontSize.addActionListener(this);
openIt.addActionListener(this);
closeIt.addActionListener(this);
saveIt.addActionListener(this);
cutIt.addActionListener(this);
copyIt.addActionListener(this);
pasteIt.addActionListener(this);
}
//Handle button events
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (obj.equals(printBut))//Print button for picture canvas
{
PrinterJob printJob = PrinterJob.getPrinterJob();
//Set the printable object to be the textarea
printJob.setPrintable(editWindow);//Print the text area contents
if (printJob.printDialog())
{
try
{
printJob.print();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
if (obj.equals(cutBut) || obj.equals(cutIt)) //Cut button for text area
{
editWindow.cut();
}
if (obj.equals(copyBut) || obj.equals(copyIt)) //Copy button for text area
{
editWindow.copy();
}
if (obj.equals(pasteBut) || obj.equals(pasteIt)) //Paste button for text area
{
editWindow.paste();
}
if (obj.equals(dgCB) || obj.equals(cbFontType) || obj.equals(cbFontSize)) //Font selections
{
if( cbFontType.getSelectedItem().equals("Plain"))
dgfonty = Font.PLAIN;
else
if(cbFontType.getSelectedItem().equals("Bold"))
dgfonty = Font.BOLD;
else
if(cbFontType.getSelectedItem().equals("Italic"))
dgfonty = Font.ITALIC;
editWindowFont = new Font( (String)dgCB.getSelectedItem(), dgfonty, Integer.parseInt((String)cbFontSize.getSelectedItem()));
editWindow.setFont(editWindowFont);
editWindow.repaint();
}
if(obj.equals(openBut) || obj.equals(openIt)) //Paste button for text area
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String getFileName = "" + chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName();
openFile = new File(getFileName);
try
{
int myBuf;
String dataString = "";
FileInputStream inStream = new FileInputStream(openFile);
while( (myBuf = inStream.read()) != -1)
{
dataString += (char)myBuf;
}
editWindow.setText(dataString);
inStream.close();
}
catch(FileNotFoundException f)
{}
catch(IOException f)
{}
}
}
if(obj.equals(saveBut) || obj.equals(saveIt)) //Save button/menu item
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);//Open save dialog
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
String getFileName = "" + chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName();
saveFile = new File(getFileName);
try
{
int myBuf;
String dataString = "";
char[] outArray;
FileOutputStream outStream = new FileOutputStream(saveFile);
outArray = editWindow.getText().toCharArray();//Grab edit window contents
for( int z = 0; z < outArray.length; z++)
{
outStream.write(outArray[z]);
}
outStream.close();
}
catch(FileNotFoundException f)
{
System.out.println("No such file : " + f);
}
catch(IOException g)
{
System.out.println("I/O Exception : " + g);
}
}
}
/*Close button just clears screen because file
gets closed after read or write anyway */
if (obj.equals(closeIt))
{
editWindow.setText("");
}
}
}
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
public class dgTextArea extends JTextArea implements Printable
{
JTextArea editWindow;
public static void main(String[] args)
{
dgTextArea dgSTA = new dgTextArea();
//dgSTA.setSize(400,400);
//dgSTA.setVisible(true);
}
//Print method must be defined for printable interface
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi >= 1)
{
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(), pf.getImageableY());
paint(g2);
return Printable.PAGE_EXISTS;
}
}
(Review ID: 47976)
======================================================================
Name: krT82822 Date: 06/13/99
The following example creates a JTextArea, with
Hello World inside it. It uses the Arial truetype
font on Window Nt 4.0 srv pack 3.
When I try and change the font style to Italic
it doesn't change and it sticks
with being plain.
The font is supposed to be changed by pressing the
Button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test2 {
JFrame frame = new JFrame ();
JTextArea textArea = new JTextArea();
JButton button = new JButton ("Italic") ;
public void init () {
frame.getContentPane().setLayout ( new BorderLayout () );
frame.getContentPane().add ( textArea , BorderLayout.CENTER) ;
frame.getContentPane().add ( button , BorderLayout.NORTH) ;
textArea.setText ( "Hello World" );
textArea.setFont ( new Font ( "Arial" , Font.PLAIN, 30 )) ;
frame.setSize ( new Dimension ( 300,300) );
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent) {
italic();
}
});
frame.setVisible ( true ) ;
}
public void italic () {
textArea.setFont ( new Font ( "Arial" , Font.ITALIC , 30 ));
}
public static void main (String args [] ) {
Test2 test2 = new Test2();
test2.init();
}
}
(Review ID: 52353)
======================================================================
Name: krT82822 Date: 12/30/99
java version "1.2"
Classic VM (build JDK-1.2-V, native threads)
When setFont() on a JTextArea object is called and the only difference between
the old Font and the new Font is the type (bold, italic, etc...), the Font in