This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes converting program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting program" Watch "converting program" New topic
Author

converting program

jason gass
Greenhorn

Joined: Nov 11, 2004
Posts: 13
I have been trying to get a basic spreadsheet created. I have an old program based on an array that has it so you can be in a cell and type =01+02 and it will properly add it. I am now trying to get this to work properly in my new layout. If you have any suggestion please post or email me.

thanks,
jason


[..........OLD SPREADSHEET...............]

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Spreadsheet extends Frame implements ActionListener{

TextField tf[][];

public Spreadsheet() {
super("Spreadsheet");

MenuBar mybar = new MenuBar();
setMenuBar(mybar);
Menu f_menu = new Menu("File");
f_menu.add(new MenuItem("Open"));
f_menu.add(new MenuItem("Save"));
f_menu.add(new MenuItem("Exit"));
mybar.add(f_menu);


//new container
Container newContainer = new Container();

//creates a grid inside the container
newContainer.setLayout(new GridLayout(3, 4, 0, 0));
// array
tf = new TextField[3][4];
//creates the coordinates for the rows and columns
for (int r=0; r <= 2; r++) {
for (int c=0; c <= 3; c++) {
tf[r][c]=new TextField(10);

//adds the array of textboxes on the container
newContainer.add(tf[r][c]);
tf[r][c].addActionListener(this);
}
}


//creates boaderlayout and adds grid to the center
setLayout(new BorderLayout());
add(newContainer, BorderLayout.CENTER);
Button saveB, openB;
saveB = new Button("Save");
saveB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
//ADDS SAVE TO EAST LAYOUT
add(saveB, BorderLayout.EAST);

//ADDS OPEN TO WEST
openB = new Button("Open");
add(openB, BorderLayout.WEST);
openB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
pack();
setVisible(true);
}

public static void main(String args[]) {
Spreadsheet spr = new Spreadsheet();
}

//


//MATH PART OF SPREADSHEET


//
public void actionPerformed(ActionEvent e) {
TextField atf = (TextField)e.getSource();
String atftext = atf.getText();
if (atftext.charAt(0)=='=') {
int rf = (int)atftext.charAt(1)-(int)'0';
int cf = (int)atftext.charAt(2)-(int)'0';
char symbol = atftext.charAt(3);
int rs = (int)atftext.charAt(4)-(int)'0';
int cs = (int)atftext.charAt(5)-(int)'0';
int valone = Integer.parseInt(tf[rf][cf].getText());
int valtwo = Integer.parseInt(tf[rs][cs].getText());
switch (symbol) {
case '+': atf.setText(String.valueOf(valone+valtwo));
break;
case '-': atf.setText(String.valueOf(valone-valtwo));
break;
case '*': atf.setText(String.valueOf(valone*valtwo));
break;
case '/': atf.setText(String.valueOf(valone/valtwo));
break;
}
}
}


//SAVES the data to a file
public void saveFile() {
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter("Spreadsheet.data"));
for (int writerow=0;writerow<=2;writerow++)
for (int writecol=0;writecol<=3;writecol++)
writer.write(tf[writerow][writecol].getText());
writer.close();
}
catch (IOException e) {
System.out.println(e);
}
}

//OPENS THE SPREADSHEET.DATA FILE
public void openFile() {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("Spreadsheet.data"));
for (int readrow=0;readrow<=2;readrow++)
for (int readcol=0;readcol<=3;readcol++)
tf[readrow][readcol].setText(String.valueOf(reader.read()-'0'));
}
catch (IOException e) {
System.out.println(e);
}
}


}



*************************************************************
NEW FORMAT
*************************************************************


import java.awt.*;
import java.awt.event.*;


import java.lang.Character ;
import java.lang.*;

public class GBLTest extends Frame {

public GBLTest() {
super("GridBagLayout Test");

MenuBar mybar = new MenuBar();
setMenuBar(mybar);
Menu f_menu = new Menu("File");
f_menu.add(new MenuItem("Open"));
f_menu.add(new MenuItem("Save"));
f_menu.add(new MenuItem("Exit"));
mybar.add(f_menu);


//


//Creates the Grid
int rows = 10, cols = 10;
Panel panel = new Panel ( );
panel.setLayout ( new GridBagLayout () );

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2); // spacing around components
gbc.weightx = 1.0; // allow horizontal dispersion
gbc.weighty = 1.0; // " vertical "

String s = "";






for(int row = 0; row <= rows; row++) {
for(int col = 0; col <= cols; col++) {
if(col == cols) // last column
gbc.gridwidth = gbc.REMAINDER;
if(row == 0) // top row
s = col > 0 ? String.valueOf(col) : "";
if(row > 0 && col == 0) // left column
s = String.valueOf(row);
if(row == 0 || col == 0) // labels
panel.add(new Label(s, Label.CENTER), gbc);
else { // text fields
if(gbc.fill == gbc.NONE)
gbc.fill = gbc.BOTH;
s = String.valueOf((row - 1) * cols + col);
panel.add(new TextField(s), gbc);


}
}
gbc.gridwidth = 1; // reset for
gbc.fill = gbc.NONE; // next round
}
add ("Center", panel) ;

Panel northPanel = new Panel ( );
northPanel.add ( new Label("Address") );
northPanel.add ( new TextField("",5));
northPanel.add ( new Label("Value") );
northPanel.add ( new TextField("",20));

add ("North", northPanel) ;




addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
setBackground(SystemColor.control);
pack();
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
new GBLTest();
}
}
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Please use UBB code tags so the indentation is preserved in your program.


Mike Gershman
SCJP 1.4, SCWCD in process
 
Don't get me started about those stupid light bulbs.
 
subject: converting program