Created
April 4, 2017 03:26
-
-
Save easai/b35a45c64884194fbec09de173583a05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class FontChooser | |
{ | |
static public Font showDialog(Component component,Font font) | |
{ | |
FontChooserPane pane=new FontChooserPane(); | |
if(component!=null) | |
{ | |
Point pos=component.getLocation(); | |
if(component.getClass().getName().equals("ButtonApplet")) | |
{ | |
pos.x=((JApplet)component).getLocationOnScreen().x; | |
pos.y=((JApplet)component).getLocationOnScreen().y; | |
} | |
pane.setLocation(pos); | |
} | |
if(font!=null) | |
{ | |
pane.defaultFont(font); | |
} | |
pane.show(); | |
if(pane.isOK) | |
{ | |
String family=(String)pane.fontFamily.getSelectedItem(); | |
String str=pane.fontSize.getText(); | |
int size=Integer.parseInt(str); | |
int style=Font.PLAIN; | |
if(pane.bold.isSelected()) | |
{ | |
style=Font.BOLD; | |
if(pane.italic.isSelected()) | |
style|=Font.ITALIC; | |
} | |
else if(pane.italic.isSelected()) | |
style=Font.ITALIC; | |
font=new Font(family,style,size); | |
} | |
return font; | |
} | |
} | |
class FontChooserPane extends JDialog | |
{ | |
JToggleButton bold=new JToggleButton("Bold"); | |
// JToggleButton bold=new JToggleButton("B"); | |
JToggleButton italic=new JToggleButton("Italic"); | |
// JToggleButton italic=new JToggleButton("I"); | |
JComboBox fontFamily; | |
JTextField fontSize=new JTextField(3); | |
Font font=null; | |
ComboBoxRenderer renderer=new ComboBoxRenderer(); | |
JButton ok=new JButton("OK"); | |
JButton cancel=new JButton("Cancel"); | |
boolean isOK=false; | |
FontChooserPane() | |
{ | |
init(); | |
} | |
public void init() | |
{ | |
setTitle("Select Font"); | |
setModal(true); | |
fontSize.setText("24"); | |
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
String list[]=ge.getAvailableFontFamilyNames(); | |
fontFamily=new JComboBox(list); | |
renderer.setPreferredSize(new Dimension(300, 30)); | |
fontFamily.setMaximumRowCount(10); | |
fontFamily.setRenderer(renderer); | |
setLayout(new FlowLayout()); | |
Container pane=getContentPane(); | |
JPanel fontFamilyPanel=new JPanel(); | |
fontFamilyPanel.setLayout(new BorderLayout()); | |
fontFamilyPanel.add(new JLabel("Font Family: "),BorderLayout.NORTH); | |
fontFamilyPanel.add(fontFamily); | |
pane.add(fontFamilyPanel); | |
JPanel fontStylePanel=new JPanel(); | |
fontStylePanel.setLayout(new BorderLayout()); | |
fontStylePanel.add(new JLabel("Font Style: "),BorderLayout.NORTH); | |
JPanel fontStylePanelSelect=new JPanel(); | |
fontStylePanelSelect.add(bold); | |
fontStylePanelSelect.add(italic); | |
fontStylePanel.add(fontStylePanelSelect); | |
pane.add(fontStylePanel); | |
JPanel fontSizePanel=new JPanel(); | |
fontSizePanel.setLayout(new BorderLayout()); | |
fontSizePanel.add(new JLabel("Font Size: "),BorderLayout.NORTH); | |
fontSizePanel.add(fontSize); | |
pane.add(fontSizePanel); | |
pane.add(ok); | |
pane.add(cancel); | |
ok.setActionCommand("OK"); | |
ok.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
isOK=true; | |
hide(); | |
} | |
}); | |
cancel.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
hide(); | |
} | |
}); | |
pack(); | |
bold.addKeyListener(this); | |
italic.addKeyListener(this); | |
fontSize.addKeyListener(this); | |
fontFamily.addKeyListener(this); | |
} | |
public void keyPressed(KeyEvent e){} | |
public void keyReleased(KeyEvent e){} | |
public void keyTyped(KeyEvent e) | |
{ | |
if(e.getKeyChar()==KeyEvent.VK_ENTER) | |
{ | |
isOK=true; | |
hide(); | |
} | |
} | |
public void defaultFont(Font font) | |
{ | |
String fontName=font.getName(); | |
int i=0; | |
int nItems=fontFamily.getItemCount(); | |
while(!fontFamily.getItemAt(i).equals(fontName) && ++i<nItems); | |
if(i<nItems) | |
{ | |
fontFamily.setSelectedIndex(i); | |
} | |
int size=font.getSize(); | |
fontSize.setText(""+size); | |
Font boldFont=new Font(fontName,Font.BOLD,16); | |
bold.setFont(boldFont); | |
Font italicFont=new Font(fontName,Font.ITALIC,16); | |
italic.setFont(italicFont); | |
if(font.isItalic()) | |
italic.setSelected(true); | |
if(font.isBold()) | |
bold.setSelected(true); | |
pack(); | |
} | |
} | |
class ComboBoxRenderer extends JLabel | |
implements ListCellRenderer | |
{ | |
public ComboBoxRenderer() | |
{ | |
setOpaque(true); | |
setHorizontalAlignment(CENTER); | |
setVerticalAlignment(CENTER); | |
} | |
public Component getListCellRendererComponent( | |
JList list, | |
Object value, | |
int index, | |
boolean isSelected, | |
boolean cellHasFocus) | |
{ | |
String item=(String)value; | |
if (isSelected) | |
{ | |
setBackground(list.getSelectionBackground()); | |
setForeground(list.getSelectionForeground()); | |
} | |
else | |
{ | |
setBackground(list.getBackground()); | |
setForeground(list.getForeground()); | |
} | |
setText(item); | |
Font font=new Font(item,Font.PLAIN,24); | |
setFont(font); | |
return this; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment