Win a copy of Head First Android this week in the Android forum!

jason gass

Greenhorn
+ Follow
since Nov 11, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by jason gass

I am trying to get a simple grid with labels on the top and side, like a spreadsheet. How can I get the labels to be added properly to this format?




17 years ago
can you do a loop like this:

for (int i = 1; i<=10; i++)
textPanel.add (new JTextField(i + "") );

and name them in order, like text1,text2, text3?

So you can then refer to them like:

text1.addActionListener( textHandler );
17 years ago
I see that the link I posted does not work

/**
* File: PieBarGraph.java
* This program takes user input and creates a pie and/or bar chart
* Copyright (c) 2002-2003 Advanced Applications Total Applications Works.
* (AATAW) All Rights Reserved.
*
* AATAW grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import DrawPanel;

public class PieBarGraph extends JApplet {
private JButton pieButton, barButton, helpButton,
clearButton, aboutButton, exitButton;
private JPanel buttonPanel;
private JPanel textPanel;
private ButtonHandler buttonHandler ;
private JTextField text1, text2, text3, text4;
private TextFieldHandler textHandler ;
private JLabel label1;
private DrawPanel drawingArea;
private Container c ;
private int width = 500, height = 400;
private JFrame app ;
private String strVal;
public Graphics gVal;
boolean buttonPressed = false;

/** Initialization method */
public void init() {
drawingArea = new DrawPanel( width, height );

setupText() ;

setupButtons();

c = getContentPane();
c.add( textPanel, BorderLayout.NORTH );
c.add( buttonPanel, BorderLayout.SOUTH );
c.add( drawingArea, BorderLayout.CENTER );
}

/** Allocate and set up the text entry fields*/
public void setupText() {

label1 = new JLabel("Enter 4 numbers.");

textPanel = new JPanel();
text1 = new JTextField(5);
text2 = new JTextField(5);
text3 = new JTextField(5);
text4 = new JTextField(5);

textHandler = new TextFieldHandler();

textPanel.setLayout(
new GridLayout( 1, 5 ) );

textPanel.add(label1);
textPanel.add(text1);
textPanel.add(text2);
textPanel.add(text3);
textPanel.add(text4);
buttonPanel = new JPanel();
buttonPanel.setLayout(
new GridLayout( 1, 5 ) );

text1.addActionListener( textHandler );
text2.addActionListener( textHandler );
text3.addActionListener( textHandler );
text4.addActionListener( textHandler );
}

/** Allocate and set up the the JButtons */
public void setupButtons() {
buttonHandler = new ButtonHandler();

pieButton = new JButton( "Pie Chart" );
barButton = new JButton( "Bar Chart" );
helpButton = new JButton( "Help" );
aboutButton = new JButton( "About" );
helpButton.setBackground( Color.blue);
helpButton.setForeground( Color.white);
clearButton = new JButton( "Clear" );
clearButton.setBackground( Color.white);
exitButton = new JButton( "Exit" );
exitButton.setBackground( Color.red);
exitButton.setForeground( Color.white);
aboutButton.setBackground( Color.blue);
aboutButton.setForeground( Color.white);

buttonPanel.add( pieButton );
buttonPanel.add( barButton );
buttonPanel.add( helpButton );
buttonPanel.add( aboutButton );
buttonPanel.add( clearButton );
buttonPanel.add( exitButton );

pieButton.addActionListener( buttonHandler );
barButton.addActionListener( buttonHandler );
clearButton.addActionListener( buttonHandler );
helpButton.addActionListener( buttonHandler );
aboutButton.addActionListener( buttonHandler );
exitButton.addActionListener( buttonHandler );
}

/** Set the width of frame to 600 if w less than 0 */
public void setWidth( int w )
{ width = ( w >= 0 ? w : 600 ); }

/** Set the height to 600 if h less than 0 */
public void setHeight( int h )
{ height = ( h >= 0 ? h : 400 ); }

/** Make sure that the data entered is a digit */
public boolean checkDigit(String strVal) {

int strLength = 0;
boolean notDig = true;

strLength = strVal.length();

for (int ii = 0; ii < strLength; ii++) {
if (!Character.isDigit(strVal.charAt(ii)) ) {
notDig = false;
break;
}
}

return notDig;
}

/** Check if data in the text fields has changed */
public void checkFieldsChange( ActionEvent e ) {
if (!text1.getText().equals(drawingArea.strField1) ||
!text2.getText().equals(drawingArea.strField2) ||
!text3.getText().equals(drawingArea.strField3) ||
!text4.getText().equals(drawingArea.strField4) ) {
getTextValues();
}

return;
}

/** Retrieve data from the text fields */
public void getTextValues() {
drawingArea.strField1 = text1.getText() ;
drawingArea.strField2 = text2.getText() ;
drawingArea.strField3 = text3.getText() ;
drawingArea.strField4 = text4.getText() ;

drawingArea.intField1 =
Integer.parseInt(text1.getText() );
drawingArea.intField2 =
Integer.parseInt(text2.getText() );
drawingArea.intField3 =
Integer.parseInt(text3.getText() );
drawingArea.intField4 =
Integer.parseInt(text4.getText() );

return;
}

/** Ensure that the data is not null */
public boolean checkFields(
ActionEvent e, JButton choice, int i ) {
boolean checkedOK = false;

if (text1.getText().equals("") ||
text2.getText().equals("") ||
text3.getText().equals("") ||
text4.getText().equals("") ) {

drawingArea.setCurrentChoice( 3 );
drawingArea.errOne = true;
}
else if( !checkDigit(text1.getText()) ||
!checkDigit(text2.getText()) ||
!checkDigit(text3.getText()) ||
!checkDigit(text4.getText()) ) {

drawingArea.setCurrentChoice( 3 );
drawingArea.errTwo = true;
}
else {
getTextValues();
checkedOK = true;
if (!buttonPressed)
drawingArea.OKOne = true;
}

return checkedOK;
}

/** */
public void sysExit( int a ) {
System.exit( a );
}

/** Main entry point */
public static void main( String args[] )
{
int width = 790, height = 550;
Graphics gVal2;

// create window in which applet will execute
JFrame appMain =
new JFrame( "Pie and Bar Chart" );

appMain.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);

/** create one applet instance */
PieBarGraph appBar = new PieBarGraph();
appBar.setWidth( width );
appBar.setHeight( height );


/** call applet's init and start methods */
appBar.init();
appBar.start();

/** attach applet to center of window */
appMain.getContentPane().add( appBar );

/** set the window's size */
appMain.setSize( appBar.width, appBar.height );

/**
* showing the window causes all GUI components
* attached to the window to be painted
*/


appMain.show();
}

/** ButtonHandler is the event handler */
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
buttonPressed = true;

if ( e.getSource() == pieButton ) {
if ( checkFields( e, pieButton, 0 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 0 );
}
else if ( e.getSource() == barButton ) {
if ( checkFields( e, barButton, 1 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 1 );
}
else if ( e.getSource() == helpButton ) {
if ( checkFields( e, helpButton, 3 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 3 );
}
else if ( e.getSource() == aboutButton ) {
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "c:\\Program Files\\Internet Explorer\\IExplore.exe" ,
"http://sumtotalz.com/TotalAppsWorks/index.html " };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
//MyPrint ("Process exit code is: " +
//child.exitValue());
}
catch(IOException e2) {
//MyPrint( "IOException starting process!");
}
catch(InterruptedException e3) {
//MyPrint( "Interrupted waiting for process!");
}
drawingArea.setCurrentChoice( 4 );
}
else if ( e.getSource() == clearButton ) {
if ( checkFields( e, clearButton, 4 ) )
checkFieldsChange( e );
drawingArea.setCurrentChoice( 4 );
}
else if ( e.getSource() == exitButton ) {
sysExit( 0 );
}
}
}

/** TextFieldHandler is the text field event handler */
private class TextFieldHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
int i = 0;

buttonPressed = false;

drawingArea.setCurrentChoice( 3 );
checkFields( e, helpButton, i );
}
}

}





**************************************************************************

/** **********************************************
* File: DrawPanel.java
* This file extends the JPanel class.
*
* Copyright (c) 2002-2003 Advanced Applications Total Applications Works.
* (AATAW) All Rights Reserved.
*
* AATAW grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. AATAW AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL AATAW OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.print.*;
import java.util.*;
import java.lang.Math;

/**
* subclass of JPanel to allow drawing in a separate area
*/
class DrawPanel extends JPanel {
private int currentChoice = -1; // don't draw first time
private int width = 100, height = 100, prevChoice = -1 ;;
private boolean initPass = true;
public boolean errOne = false;
public boolean errTwo = false, OKOne = false;
private Graphics gVal;
private JFrame myFrame ;
private PrinterJob job ;
public int intField1, intField2, intField3, intField4;
public String strField1, strField2, strField3, strField4;

public DrawPanel( int w, int h ) {
width = ( w >= 0 ? w : 100 );
height = ( h >= 0 ? h : 100 );
}

public void drawFont( Graphics g) {

/** set current font to Serif (Times), bold, 12pt
* and draw a string */
g.setFont( new Font( "Serif", Font.BOLD, 16 ) );
g.drawString( "Please follow the instructions below.", 20, 50 );

/** set current font to Monospaced (Courier),
* italic, 24pt and draw a string */
g.setFont( new Font( "Monospaced", Font.ITALIC, 16 ) );
g.drawString( "Enter a number in each entry field above and then press a chart button.", 20, 70 );

/** set current font to SansSerif (Helvetica),
* plain, 14pt and draw a string */
g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) );
g.drawString( "To repeat these instructions, press the help button below.", 20, 90 );

/** set current font to Serif (times), bold/italic,
* 18pt and draw a string */
g.setColor( Color.red );
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g.drawString( "These instructions will disappear " +
"when you request a chart. ", 20, 110 );
}

/** drawBar uses the user input to draw the bar chart */
public void drawBar( Graphics g) {
if ( ( intField1 >= 0 ) && ( intField2 >= 0 ) &&
( intField3 >= 0 ) && ( intField4 >= 0 ) ) {
/** create 2D by casting g to Graphics2D */
Graphics2D g2d = ( Graphics2D ) g;

/** Draw the x-coordinate line */
g.setColor(Color.black);
g.drawLine ( 5, 406, 450, 406);

/** draw 2D Bars in red */
g2d.setColor( Color.red );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 50, 400 - intField1, 5,
intField1 ) );
g.drawString("Red ", 45,
400 - intField1 - 40);
g.drawString("" + intField1, 45,
400 - intField1 - 20);

g2d.setColor( Color.green );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 100, 400 - intField2,
5, intField2) );
g.drawString( "Green ", 90,
400 - intField2 - 40);
g.drawString( "" + intField2, 90,
400 - intField2 - 20);

g2d.setColor( Color.yellow );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 150, 400 - intField3,
5, intField3 ) );
g.drawString("Yellow ", 140,
400 - intField3 - 40);
g.drawString("" + intField3, 140,
400 - intField3 - 20);

g2d.setColor( Color.blue );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 200, 400 - intField4,
5, intField4 ) );
g.drawString("Blue " , 190,
400 - intField4 - 40);
g.drawString("" + intField4 , 190,
400 - intField4 - 20);
}
}

/** drawPie uses the user input to draw the pie chart */
public void drawPie( Graphics g) {
double floatVal[] = {0.0, 0.0, 0.0, 0.0};
double totVal = 0.0;
Font font ;

/** create 2D by casting g to Graphics2D */
Graphics2D g2d = ( Graphics2D ) g;

totVal = (double) intField1 + + intField2 + intField3 + intField4 ;
floatVal[0] = (double) (intField1 / totVal);
floatVal[1] = (double) (intField2 / totVal);
floatVal[2] = (double) (intField3 / totVal);
floatVal[3] = (double) (intField4 / totVal);

Arrays.sort (floatVal);

/** draw 2D pie-shaped arc in white */
g2d.setPaint( Color.white );
g2d.setStroke( new BasicStroke( 6.0f ) );
g2d.fillArc( 50, 80, 300, 300, 0, (int) ( floatVal[ 3 ] * 360 ) );
g2d.fillRect( 440, 80, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + (java.lang.Math.round( floatVal[ 3 ] * 100.0 )) + " %",
490, 95);


g2d.setPaint( Color.green );
g2d.fillArc( 50, 80, 300, 300, (int) ( floatVal[ 3 ] * 360 ),
( int )( floatVal[ 2 ] * 360 ) );
g2d.fillRect( 440, 120, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 2 ] * 100)) + " %",
490, 135);

g2d.setPaint( Color.blue );
g2d.fillArc( 50, 80, 300, 300,
(int)(floatVal[ 3 ] * 360) + (int)( floatVal[ 2 ] * 360 ) ,
(int)(floatVal[ 1 ] * 360 ) );
g2d.fillRect( 440, 160, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 1 ] * 100)) + " %",
490, 175);

g2d.setPaint( Color.black );
g2d.fillArc( 50, 80, 300, 300,
(int)(floatVal[ 3 ] * 360) + (int)( floatVal[ 2 ] * 360 ) +
(int)( floatVal[ 1 ] * 360) , (int) (floatVal[ 0 ] * 360 ) + 2 ) ;
g2d.fillRect( 440, 190, 40, 20 ) ;
g.setFont(
new Font( "", Font.BOLD + Font.ITALIC, 18 ) );
g2d.setPaint( Color.black );
g.drawString("- " + ( java.lang.Math.round( floatVal[ 0 ] * 100)) + " %",
490, 205);

g2d.setStroke( new BasicStroke( 1.0f ) );
g.drawArc( 50, 80, 300, 300, 0, 360 );

return;
}

/** */
public void paintComponent( Graphics g ) {
super.paintComponent( g );
/** if this the initial pass, display the help instructions. */
if ( initPass ) {
initPass = false;
setGraphicsVal(g);
currentChoice = 3;
}

/** Display th e help instructions */
if ( currentChoice == 3 ) {
drawFont(g);
}
/** Valid numbers were not entered */
else if (errOne) {
errOne = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.red);
g.drawString(
"Please enter a valid number in each of the fields above.", 30, 50 );
}
/** Valid numbers were not entered */
else if (errTwo) {
errTwo = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.red);
g.drawString(
"Please enter a valid number in each of the fields above.", 30, 50 );
g.drawString(
"Alphbet letters and special characters are not numbers/digits.", 30, 90 );
}
/** Valid numbers were entered but a button was not
* pressed */
else if (OKOne) {
OKOne = false;
g.setFont(new Font("Serif", Font.BOLD, 24) );
g.setColor(Color.blue);
g.drawString(
"Please press the button for the chart you wish.", 30, 50 );
}
/** Process the data using the pressed button */
else {
switch( currentChoice ) {
case 0:
drawPie(g);
prevChoice = 0 ;
break;

case 1:
drawBar(g);
prevChoice = 1 ;
break;

case 2:
;
break;

case 3:
drawFont(g);
prevChoice = 3 ;
break;

case 4:
setCurrentChoice(-1);
prevChoice = -1 ;
break;

case 5:
;
break;
}
}
}

/** */
public void setCurrentChoice( int c ) {
currentChoice = c;
repaint();
}

/** */
public void setGraphicsVal( Graphics g ) {
gVal = g;
}

/** */
public void setFrameAddress( JFrame myFrame_1 ) {
myFrame = myFrame_1 ;
System.out.println("setFrameAddress(): myFrame_1 is " + myFrame_1 +
"\n myFrame is " + myFrame);
}
}
17 years ago
Hey Everyone!
I am trying to create a chart drawer, I found this tutorial on planet-source-code but it compiles with an error:import DrawPanel;
'.' expected

I do not understand what it can not import the file, DrawPanel compiles with no problem.

Could some look at the code and see what the error is? I've contacted the creator and have not heard back.

Thanks,

Jason

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3798&txtForceRefresh=111920041654974170
17 years ago
I have done VB programming, if you have a question feel free to contact me. I won't go against the forum rules by discussing it here.
Jason

jrg03s@fsu.edu
17 years ago
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();
}
}
17 years ago
Can a Jtable do arithmetic functions?

Jason
17 years ago
Hey Thanks,
Thats what I was trying for. Which forum do I go to asking about programming the background on this? I want to be able to have a text box in which i can put C1=A1+B1, but im unsure on to reference the cells.

Thanks again, Im back off to try to get this to work.

Jason
17 years ago
Hey Everyone,
Im a newbie in Java. I am trying to create a spreadsheet. Could somone help me? I am trying to get my labels to work properly on the top and side of a grid but I can not get the spacing right

/*
*
* GridBagLayout layout manager
*/

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

public class GridBagLayoutTest extends Frame
implements WindowListener {


public GridBagLayoutTest() {

super("GridBagLayout Test");

MenuBar mybar = new MenuBar();
setMenuBar(mybar);

// and some menus

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);



Panel buttonPanel = new Panel ( );
buttonPanel.setLayout (new GridLayout (10, 10, 1, 1) );
for (int i = 1; i<=100; i++)
buttonPanel.add (new TextField(i + "") );



Panel centerPanel =new Panel();
centerPanel.add(buttonPanel);
add ("Center", centerPanel) ;

Panel bottomPanel = new Panel ( );
bottomPanel.add ( new Button("Dial") );
add ("South", bottomPanel) ;


Panel northPanel = new Panel ( );
northPanel.setLayout (new GridLayout (0, 10, 1, 1) );
for (int i = 1; i<=10; i++)
northPanel.add(new Label(i+""));
add ("North", northPanel) ;



Panel eastPanel = new Panel ( );
eastPanel.setLayout (new GridLayout (10, 1, 1, 1) );

add ("East", eastPanel) ;


Panel westPanel = new Panel ( );
westPanel.setLayout (new GridLayout (10, 1, 1, 1) );
for (int i = 1; i<=10; i++)
westPanel.add(new Label(i+""));
add ("West", westPanel) ;

addWindowListener(this);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();


pack();
setBackground(SystemColor.control);
setVisible(true);
}

public static void main(String args[]) {
new GridBagLayoutTest();
}

//WindowListener Interface
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}

}
17 years ago
Im not bound to either textfields or the AWT. My professor really hasnt talked much how to do this so I am looking for the easiest way right now as I am learning.

So I am looking at this in two ways:

1) use the AWT, I can have it create the cells
setLayout(new GridLayout(0, 10, 1, 1));
//make the cells

for (int b=1; b <=100; b++) {
add(new TextField(" "));

But I do not know where to go from here. I, can not get the labels to line up right at the top or bottom. When I create a new label it pushes the text boxes onto the next row.


2) Use an array to create textboxes and then display them, but I am unsure how to get them to line up properly in a grid format.

Finally, I need to be able to refrence the cells so I can do simple calculations like add and subtract.

Any suggestions?

Jason
17 years ago
Can I create a java table that will do the basic task i need? or will it not work?

thanks
17 years ago
That is pretty close to what I need to get. I just got back from the bookstore looking at sample code and picked up a couple of books. I think I have an idea to get the layout done properly, but can someone direct me how to save the textfields to an array? I am using a loop to create them in a panel.

Thanks,

Jason

Note: I tried the source code link on that last page and it just comes up as a page with a bunch of mixed up writing? any suggestions?
17 years ago
Hi Everyone,
I have a huge problem. I am new at java. I am taking a class in school and for the first half of the semester, the professor taught interface principle, the second half (less that 8 weeks) he is trying to teach java code. I need to create a spreadsheet. Simple grid with textboxs, labels and able to save. I am lost complety. I have a week and half and we havent been taught any events.

Can someone give me some direction to get the interface developed? I have been going back and forth between gridlayout and gridbaglayout. Imunsure what to use. I figured to use a loop to create textfields and store it to an array(not quite sure how this works) this is so the cells can be refrenced to, but then i need to be able to create labels next to the cells

Please help in anyway. I am really interested in Java but trying to learn it in 5 weeks is not my style.

Jason
17 years ago