dcsimg
<strong>How do I open a font.properties file in a required format using a Swing component</strong>
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Java_Techie
Posted On:   Friday, July 19, 2002 08:27 AM

I have a small Swing GUI with 5 JButtons "Open","Save" ,"Next","Previous" and 3 JTextfields "key1","key2" "value2" and one JTextArea "value1", With this GUI I have to do the following operations 1)Open a properties file (for Ex-font.properties) by click of the "Open" button using a JFileChooser 2)Store the properties file in Collection class (like List/Map/Tree/Properties etc) as Name-value Pair 3)I want to display Name-Value pair as "Name" in key1 and all the "Values" in value1 fields    More>>



I have a small Swing GUI with 5 JButtons "Open","Save" ,"Next","Previous" and 3 JTextfields "key1","key2" "value2" and one JTextArea "value1",


With this GUI I have to do the following operations


1)Open a properties file (for Ex-font.properties) by click of the "Open" button using a JFileChooser


2)Store the properties file in Collection class (like List/Map/Tree/Properties etc) as Name-value Pair


3)I want to display Name-Value pair as "Name" in key1 and all the "Values" in value1 fields


4)On click of a button say "Next" I have to get the key value in the key2 textfield and highlight the corresponding "Value" in the value1 textarea.


5)In the value2 text field I can create a new value and store it in a buffer


6) The "Next" button should work recursively line after line till the EOF is encountered and values of all the textfields should change correspondingly i.e in key1 and key2 and the highlighting the value1 text area


7)I should also be able to traverse back using another button say "Previous"


8)Then save the changes by clicking the "Save" button to a new property file by picking the changes from the buffer.


The GUI code is ready,I Will attach that with this message,
So how should I do all these operations?
Could anyone explain with a working java program,
This is very urgent,Could you mail the code to my email-kutts@lycos.com
please help


The Java GUI code I have written till now,copy this code to a Text editor it will get indented .



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


public class FontPropEditor extends JPanel implements ActionListener{
JButton open;
JButton save;
JButton exit;
JButton prev;
JButton next;
JTextField key1,key2;
JTextField val2;
JTextArea val1;
JScrollPane scroller;
JPanel topPanel,midPanel,bottomPanel,sidePanel,centerPanel;
String propName;


public FontPropEditor(){
super(true);
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());

key1=new JTextField(8);
key1.setText("Key 1");
key1.setForeground(Color.black);
key1.setEditable(true);

key2=new JTextField(8);
key2.setText("Key 2");
key2.setForeground(Color.black);
key2.setEditable(true);

val1=new JTextArea(2,10);
val1.setForeground(Color.black);
val1.setText("Value 1");
val1.setEditable(false);
scroller=new JScrollPane(val1);

val2=new JTextField(30);
val2.setText("Value 2");
val2.setForeground(Color.black);
val2.setEditable(true);

open=new JButton("Open");
open.setFont(new Font("Monotype crsiva",Font.BOLD,15));
open.setForeground(Color.black);

save=new JButton("Save");
save.setFont(new Font("Monotype crsiva",Font.BOLD,15));
save.setForeground(Color.black);

exit=new JButton("Exit");
exit.setFont(new Font("Monotype crsiva",Font.BOLD,15));
exit.setForeground(Color.black);

prev=new JButton(" < <");
prev.setFont(new Font("Monotype crsiva",Font.BOLD,15));
prev.setForeground(Color.black);

next=new JButton(">>");
next.setFont(new Font("Monotype crsiva",Font.BOLD,15));
next.setForeground(Color.black);

open.addActionListener(this);
save.addActionListener(this);
exit.addActionListener(this);
prev.addActionListener(this);
next.addActionListener(this);

topPanel=new JPanel();
midPanel=new JPanel();
bottomPanel=new JPanel();
centerPanel=new JPanel();
sidePanel=new JPanel();

add("North",topPanel);
add("Center",midPanel);
add("South",bottomPanel);
JPanel panel=new JPanel();

topPanel.add(open);
topPanel.add(save);
topPanel.add(exit);

midPanel.setLayout(new BorderLayout());
midPanel.add("West",sidePanel);
midPanel.add("Center",centerPanel);

sidePanel.setLayout(new BorderLayout());
sidePanel.add("North",key1);
sidePanel.add("South",key2);

centerPanel.setLayout(new BorderLayout());
centerPanel.add("Center",scroller);
centerPanel.add("South",val2);

bottomPanel.add(prev);
bottomPanel.add(next);
}


public void actionPerformed(ActionEvent ev){

String arg=(String)ev.getActionCommand();
if(arg.equals("Open")){
new OpenAction();
}
else if(arg.equals("Save")){
new SaveAction();
}
else if(arg.equals(" < <")){
new PrevAction();
}
else if(arg.equals(">>")){
new NextAction();
}
else if(arg.equals("Exit")){
System.exit(1);

}
}


public JFileChooser createFileChooser(){
// create a filechooser
JFileChooser fc = new JFileChooser();

// set the current directory to be the properties directory
File propFile = new File("C:/jdk1.3/jre/lib/font.properties");
if(propFile.exists()){
fc.setCurrentDirectory(propFile);
fc.setSelectedFile(propFile);

}

return fc;
}


class OpenAction{
OpenAction(){
JFileChooser fc = createFileChooser();
// show the filechooser
int result = fc.showOpenDialog(FontPropEditor.this);
File file = fc.getSelectedFile();
if(result == JFileChooser.OPEN_DIALOG){
propName=fc.getSelectedFile().getPath();
loadPropFile(propName);
}
}
public void loadPropFile(String name){

}
}


class SaveAction{
SaveAction(){
JFileChooser fc = createFileChooser();
// show the filechooser
int result = fc.showSaveDialog(FontPropEditor.this);
File file = fc.getSelectedFile();
if(result == JFileChooser.SAVE_DIALOG){
propName=fc.getSelectedFile().getPath();
savePropFile(propName);
}
}
public void savePropFile(String name){
}
}
class PrevAction{
PrevAction(){
}
}
class NextAction{
NextAction(){
}
}


public static void main(String args[]){
try {
String vers = System.getProperty("java.version");
if (vers.compareTo("1.1.2") < 0) {
System.out.println("!!!WARNING: Swing must be run with a " +
"1.1.2 or higher version VM!!!");
}
JFrame frame = new JFrame();
frame.setTitle("Font Property Editor");
frame.setBackground(Color.lightGray);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add("Center", new FontPropEditor());
frame.addWindowListener(new AppCloser());
frame.pack();
frame.setSize(500, 500);
frame.show();
} catch (Throwable t) {
System.out.println("uncaught exception: " + t);
t.printStackTrace();
}
}


protected static final class AppCloser extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

}

   <<Less
About | Sitemap | Contact