?
Solved

"Formula Bar" in Java Spreadsheet Application

Posted on 2006-05-13
9
Medium Priority
?
575 Views
Last Modified: 2010-08-05
I would like to create a spreadsheet application, it is going well but I got stucked with "Formula Bar", How can I create a "Formula Bar" like in Excel with "check sign" and "cross" sign near them?

Thanks
0
Comment
Question by:johnmarco
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16673888
By "check" and "cross" sign, do you mean the tick and cross buttons to the left of the formula bar in Excel?

If so, you just create JButton's and assign those graphics to them via an ImageIcon instance, for example:


    ImageIcon cross = new ImageIcon( "cross.gif" ) ;
    ImageIcon check = new ImageIcon( "check.gif" ) ;
   
    JButton cross_button = new JButton( cross ) ;
    JButton check_button = new JButton( check ) ;


You can also apply the rollover effects, etc, as demonstrated here:
http://javaalmanac.com/egs/javax.swing/button_OtherIcon.html


Does that help?
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16673891
(Alternatively, you could create a custom component - but the above would probably be an easier and more efficient technique).
0
 

Author Comment

by:johnmarco
ID: 16674934
But where is the formula bar? Do you have a sample output to show?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16677141
You don't already have a formular bar?

Well, for the formula input field itself, you'd just use a JTextField. But to add this (and any other buttons for the formula bar) to some sort of moveable toolbar, you'd use the JToolBar -- see here for an example:

http://javaalmanac.com/egs/javax.swing/toolbar_ToolBar.html?l=rel
0
 

Author Comment

by:johnmarco
ID: 16677315
I did read the article you mentioned but could not figure it out, can you help to add a tool bar to the code?
I also want one tick/check sign and one X/cancel sign, thank you

Here is the code:



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

class  tableExample implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;
JButton cmdOpen,cmdSave,cmdNew,cmdAdd,cmdDelete,cmdSetValue,cmdGetValue;
JPanel mainPanel,buttonPanel;


   public static void main(String[] args)
    {
tableExample t=new tableExample();
    }

tableExample()
{
rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"A",
"B",
"C",
"D",
"E",
"F",
"G"
};
addColumns(columnNames);

tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);

table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(false);

table.getModel().addTableModelListener(this);

lblMessage=new JLabel("");


buttonPanel=new JPanel();
cmdOpen=new JButton("Open File");
cmdSave=new JButton("Save File");
cmdNew=new JButton("New File");
cmdAdd=new JButton("Add Row");
cmdDelete=new JButton("Delete") ;
cmdSetValue=new JButton("Set Value");
cmdGetValue=new JButton("Get Value");

buttonPanel.add(cmdOpen);
buttonPanel.add(cmdSave);
buttonPanel.add(cmdNew);
buttonPanel.add(cmdAdd);
buttonPanel.add(cmdDelete);
buttonPanel.add(cmdSetValue);
buttonPanel.add(cmdGetValue);

cmdOpen.addActionListener(this);
cmdSave.addActionListener(this);
cmdNew.addActionListener(this);
cmdAdd.addActionListener(this);
cmdDelete.addActionListener(this);
cmdSetValue.addActionListener(this);
cmdGetValue.addActionListener(this);

mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setSize(800,600);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",scrollPane);
mainPanel.add("South",buttonPanel);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));

}
 public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}

public void addRow() //Add Row
{
Vector r=new Vector();
r=createBlankElement();
rows.addElement(r);
table.addNotify();

}

// Start of Open File Class
public void openFile() //Open File
{
      JFileChooser fc = new JFileChooser();
      int returnVal = fc.showOpenDialog(cmdOpen);
}

public void saveFile() //Save File
{
      JFileChooser fc = new JFileChooser();
      int returnVal = fc.showSaveDialog(cmdSave);      
}

public void newFile() //New File
{
      JFileChooser fc = new JFileChooser();
      int returnVal = fc.showOpenDialog(cmdNew);      
}

 public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}

 void deleteRow(int index)
   {
     if(index!=-1)//At least one Row in Table
      {
        rows.removeElementAt(index);
        table.addNotify();
       }

   }//Delete Row

 public void tableChanged(javax.swing.event.TableModelEvent source)     {
                 String msg="";
                 TableModel tabMod = (TableModel)source.getSource();
          switch (source.getType())
                   {
                       case TableModelEvent.UPDATE:
                       msg="Table Value Updated for  cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
              JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
                break;

                   }

    }//Table Changed Method

public void selectCell(int row,int col)
    {
         if(row!=-1 && col !=-1)            
          {
          table.setRowSelectionInterval(row,row);
          table.setColumnSelectionInterval(col,col);
          }
    }


public void actionPerformed(ActionEvent source)
    {
            if (source.getSource()==(JButton) cmdOpen)
            {
                  openFile();
            }
            if (source.getSource()==(JButton) cmdSave)
            {
                  saveFile();
            }
            if (source.getSource()==(JButton) cmdNew)
            {
                  newFile();
            }
         if (source.getSource()==(JButton) cmdAdd)
         {
             addRow();
         }
         if (source.getSource()==(JButton) cmdDelete)
         {
             deleteRow(table.getSelectedRow());
         }
         if (source.getSource()==(JButton) cmdSetValue)
         {
          String CName=JOptionPane.showInputDialog(null,"Enter Value to be set at Cell 0,2 ","Simple Table Example",JOptionPane.INFORMATION_MESSAGE);
              if(!CName.trim().equals("") && table.getRowCount()>0)
              {
                  selectCell(0,2);
                  table.setValueAt(CName,0,2);
              }
         }
         if (source.getSource()==(JButton) cmdGetValue)
         {
            if(table.getRowCount()>0)
             {
           String msg="Value At cell 0,0 is "+table.getValueAt(0,0).toString();
           JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
             }
     }
   
    }//ActionList

}


0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16677325
An example


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

class  tableExample implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;
JButton cmdOpen,cmdSave,cmdNew,cmdAdd,cmdDelete,cmdSetValue,cmdGetValue;
JPanel mainPanel,buttonPanel;


   public static void main(String[] args)
    {
tableExample t=new tableExample();
    }

tableExample()
{
rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"A",
"B",
"C",
"D",
"E",
"F",
"G"
};
addColumns(columnNames);

tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);

table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(false);

table.getModel().addTableModelListener(this);

lblMessage=new JLabel("");


buttonPanel=new JPanel();
cmdOpen=new JButton("Open File");
cmdSave=new JButton("Save File");
cmdNew=new JButton("New File");
cmdAdd=new JButton("Add Row");
cmdDelete=new JButton("Delete") ;
cmdSetValue=new JButton("Set Value");
cmdGetValue=new JButton("Get Value");

buttonPanel.add(cmdOpen);
buttonPanel.add(cmdSave);
buttonPanel.add(cmdNew);
buttonPanel.add(cmdAdd);
buttonPanel.add(cmdDelete);
buttonPanel.add(cmdSetValue);
buttonPanel.add(cmdGetValue);

cmdOpen.addActionListener(this);
cmdSave.addActionListener(this);
cmdNew.addActionListener(this);
cmdAdd.addActionListener(this);
cmdDelete.addActionListener(this);
cmdSetValue.addActionListener(this);
cmdGetValue.addActionListener(this);

mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
frame.setSize(800,600);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel.setLayout(new BorderLayout());

JToolBar toolbar = new JToolBar() ;
JButton cross = new JButton( "X" ) ;
JButton check = new JButton( "/" ) ;
toolbar.add( cross ) ;
toolbar.add( check ) ;
toolbar.add( new JLabel( "   fx: " ) ) ;
JTextField formulaBar = new JTextField() ;
toolbar.add( formulaBar ) ;

mainPanel.add("North",toolbar) ;

mainPanel.add("Center",scrollPane);
mainPanel.add("South",buttonPanel);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));

}
 public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}

public void addRow() //Add Row
{
Vector r=new Vector();
r=createBlankElement();
rows.addElement(r);
table.addNotify();

}

// Start of Open File Class
public void openFile() //Open File
{
     JFileChooser fc = new JFileChooser();
     int returnVal = fc.showOpenDialog(cmdOpen);
}

public void saveFile() //Save File
{
     JFileChooser fc = new JFileChooser();
     int returnVal = fc.showSaveDialog(cmdSave);    
}

public void newFile() //New File
{
     JFileChooser fc = new JFileChooser();
     int returnVal = fc.showOpenDialog(cmdNew);    
}

 public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}

 void deleteRow(int index)
   {
     if(index!=-1)//At least one Row in Table
      {
        rows.removeElementAt(index);
        table.addNotify();
       }

   }//Delete Row

 public void tableChanged(javax.swing.event.TableModelEvent source)     {
                 String msg="";
                 TableModel tabMod = (TableModel)source.getSource();
          switch (source.getType())
                   {
                       case TableModelEvent.UPDATE:
                       msg="Table Value Updated for  cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
              JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
                break;

                   }

    }//Table Changed Method

public void selectCell(int row,int col)
    {
         if(row!=-1 && col !=-1)            
          {
          table.setRowSelectionInterval(row,row);
          table.setColumnSelectionInterval(col,col);
          }
    }


public void actionPerformed(ActionEvent source)
    {
          if (source.getSource()==(JButton) cmdOpen)
          {
               openFile();
          }
          if (source.getSource()==(JButton) cmdSave)
          {
               saveFile();
          }
          if (source.getSource()==(JButton) cmdNew)
          {
               newFile();
          }
         if (source.getSource()==(JButton) cmdAdd)
         {
             addRow();
         }
         if (source.getSource()==(JButton) cmdDelete)
         {
             deleteRow(table.getSelectedRow());
         }
         if (source.getSource()==(JButton) cmdSetValue)
         {
          String CName=JOptionPane.showInputDialog(null,"Enter Value to be set at Cell 0,2 ","Simple Table Example",JOptionPane.INFORMATION_MESSAGE);
              if(!CName.trim().equals("") && table.getRowCount()>0)
              {
                  selectCell(0,2);
                  table.setValueAt(CName,0,2);
              }
         }
         if (source.getSource()==(JButton) cmdGetValue)
         {
            if(table.getRowCount()>0)
             {
           String msg="Value At cell 0,0 is "+table.getValueAt(0,0).toString();
           JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
             }
     }
   
    }//ActionList

}
0
 

Author Comment

by:johnmarco
ID: 16677332
One more thing before you get all the points, could you please help to link the cell and this Tool bar, If you type something in the cell, it is shown immediately in the Toolbar(like Excel does)If you press tick/check sign, it stores the data in the cell, if you press X/cance, it empties the cell.

I hope that is clear,
Thanks
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 16677342
I can't really do your homework for you; give me a more specific problem that you're having, and I can then help further.

See here firstly:

http://javaalmanac.com/egs/javax.swing.table/AnchorEdit.html
0
 

Author Comment

by:johnmarco
ID: 16677349
No worries, I give you 500 point.
Thanks,

More specific problem is @ http://www.experts-exchange.com/Programming/Q_21849814.html

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question