johnmarco
asked on
"Formula Bar" in Java Spreadsheet Application
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
Thanks
(Alternatively, you could create a custom component - but the above would probably be an easier and more efficient technique).
ASKER
But where is the formula bar? Do you have a sample output to show?
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
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
ASKER
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,cmd Add,cmdDel ete,cmdSet Value,cmdG etValue;
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(row s,columns) ;
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//Scrol lPane
table.setRowSelectionAllow ed(false);
table.getModel().addTableM odelListen er(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(cmdSetValu e);
buttonPanel.add(cmdGetValu e);
cmdOpen.addActionListener( this);
cmdSave.addActionListener( this);
cmdNew.addActionListener(t his);
cmdAdd.addActionListener(t his);
cmdDelete.addActionListene r(this);
cmdSetValue.addActionListe ner(this);
cmdGetValue.addActionListe ner(this);
mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setSize(800,600);
frame.setExtendedState(JFr ame.MAXIMI ZED_BOTH);
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",scr ollPane);
mainPanel.add("South",butt onPanel);
mainPanel.setBackground(Co lor.white) ;
buttonPanel.setBackground( Color.whit e);
table.getParent().setBackg round(Colo r.black);
frame.getContentPane().add (mainPanel );
frame.setVisible(true);
mainPanel.setBorder(Border Factory.cr eateLineBo rder(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.e vent.Table ModelEvent source) {
String msg="";
TableModel tabMod = (TableModel)source.getSour ce();
switch (source.getType())
{
case TableModelEvent.UPDATE:
msg="Table Value Updated for cell "+table.getSelectedRow()+" ,"+table.g etSelected Column()+" \nWhich is "+table.getValueAt(table.g etSelected Row(),tabl e.getSelec tedColumn( )).toStrin g();
JOptionPane.showMessageDia log(null,m sg,"Table Example",JOptionPane.INFOR MATION_MES SAGE);
break;
}
}//Table Changed Method
public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{
table.setRowSelectionInter val(row,ro w);
table.setColumnSelectionIn terval(col ,col);
}
}
public void actionPerformed(ActionEven t source)
{
if (source.getSource()==(JBut ton) cmdOpen)
{
openFile();
}
if (source.getSource()==(JBut ton) cmdSave)
{
saveFile();
}
if (source.getSource()==(JBut ton) cmdNew)
{
newFile();
}
if (source.getSource()==(JBut ton) cmdAdd)
{
addRow();
}
if (source.getSource()==(JBut ton) cmdDelete)
{
deleteRow(table.getSelecte dRow());
}
if (source.getSource()==(JBut ton) cmdSetValue)
{
String CName=JOptionPane.showInpu tDialog(nu ll,"Enter Value to be set at Cell 0,2 ","Simple Table Example",JOptionPane.INFOR MATION_MES SAGE);
if(!CName.trim().equals("" ) && table.getRowCount()>0)
{
selectCell(0,2);
table.setValueAt(CName,0,2 );
}
}
if (source.getSource()==(JBut ton) cmdGetValue)
{
if(table.getRowCount()>0)
{
String msg="Value At cell 0,0 is "+table.getValueAt(0,0).to String();
JOptionPane.showMessageDia log(null,m sg,"Table Example",JOptionPane.INFOR MATION_MES SAGE);
}
}
}//ActionList
}
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,cmd
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(row
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//Scrol
table.setRowSelectionAllow
table.getModel().addTableM
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(cmdSetValu
buttonPanel.add(cmdGetValu
cmdOpen.addActionListener(
cmdSave.addActionListener(
cmdNew.addActionListener(t
cmdAdd.addActionListener(t
cmdDelete.addActionListene
cmdSetValue.addActionListe
cmdGetValue.addActionListe
mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setSize(800,600);
frame.setExtendedState(JFr
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",scr
mainPanel.add("South",butt
mainPanel.setBackground(Co
buttonPanel.setBackground(
table.getParent().setBackg
frame.getContentPane().add
frame.setVisible(true);
mainPanel.setBorder(Border
}
public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String
}
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.e
String msg="";
TableModel tabMod = (TableModel)source.getSour
switch (source.getType())
{
case TableModelEvent.UPDATE:
msg="Table Value Updated for cell "+table.getSelectedRow()+"
JOptionPane.showMessageDia
break;
}
}//Table Changed Method
public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{
table.setRowSelectionInter
table.setColumnSelectionIn
}
}
public void actionPerformed(ActionEven
{
if (source.getSource()==(JBut
{
openFile();
}
if (source.getSource()==(JBut
{
saveFile();
}
if (source.getSource()==(JBut
{
newFile();
}
if (source.getSource()==(JBut
{
addRow();
}
if (source.getSource()==(JBut
{
deleteRow(table.getSelecte
}
if (source.getSource()==(JBut
{
String CName=JOptionPane.showInpu
if(!CName.trim().equals(""
{
selectCell(0,2);
table.setValueAt(CName,0,2
}
}
if (source.getSource()==(JBut
{
if(table.getRowCount()>0)
{
String msg="Value At cell 0,0 is "+table.getValueAt(0,0).to
JOptionPane.showMessageDia
}
}
}//ActionList
}
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,cmd Add,cmdDel ete,cmdSet Value,cmdG etValue;
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(row s,columns) ;
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//Scrol lPane
table.setRowSelectionAllow ed(false);
table.getModel().addTableM odelListen er(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(cmdSetValu e);
buttonPanel.add(cmdGetValu e);
cmdOpen.addActionListener( this);
cmdSave.addActionListener( this);
cmdNew.addActionListener(t his);
cmdAdd.addActionListener(t his);
cmdDelete.addActionListene r(this);
cmdSetValue.addActionListe ner(this);
cmdGetValue.addActionListe ner(this);
mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setDefaultCloseOpera tion( JFrame.EXIT_ON_CLOSE ) ;
frame.setSize(800,600);
frame.setExtendedState(JFr ame.MAXIMI ZED_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",tool bar) ;
mainPanel.add("Center",scr ollPane);
mainPanel.add("South",butt onPanel);
mainPanel.setBackground(Co lor.white) ;
buttonPanel.setBackground( Color.whit e);
table.getParent().setBackg round(Colo r.black);
frame.getContentPane().add (mainPanel );
frame.setVisible(true);
mainPanel.setBorder(Border Factory.cr eateLineBo rder(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.e vent.Table ModelEvent source) {
String msg="";
TableModel tabMod = (TableModel)source.getSour ce();
switch (source.getType())
{
case TableModelEvent.UPDATE:
msg="Table Value Updated for cell "+table.getSelectedRow()+" ,"+table.g etSelected Column()+" \nWhich is "+table.getValueAt(table.g etSelected Row(),tabl e.getSelec tedColumn( )).toStrin g();
JOptionPane.showMessageDia log(null,m sg,"Table Example",JOptionPane.INFOR MATION_MES SAGE);
break;
}
}//Table Changed Method
public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{
table.setRowSelectionInter val(row,ro w);
table.setColumnSelectionIn terval(col ,col);
}
}
public void actionPerformed(ActionEven t source)
{
if (source.getSource()==(JBut ton) cmdOpen)
{
openFile();
}
if (source.getSource()==(JBut ton) cmdSave)
{
saveFile();
}
if (source.getSource()==(JBut ton) cmdNew)
{
newFile();
}
if (source.getSource()==(JBut ton) cmdAdd)
{
addRow();
}
if (source.getSource()==(JBut ton) cmdDelete)
{
deleteRow(table.getSelecte dRow());
}
if (source.getSource()==(JBut ton) cmdSetValue)
{
String CName=JOptionPane.showInpu tDialog(nu ll,"Enter Value to be set at Cell 0,2 ","Simple Table Example",JOptionPane.INFOR MATION_MES SAGE);
if(!CName.trim().equals("" ) && table.getRowCount()>0)
{
selectCell(0,2);
table.setValueAt(CName,0,2 );
}
}
if (source.getSource()==(JBut ton) cmdGetValue)
{
if(table.getRowCount()>0)
{
String msg="Value At cell 0,0 is "+table.getValueAt(0,0).to String();
JOptionPane.showMessageDia log(null,m sg,"Table Example",JOptionPane.INFOR MATION_MES SAGE);
}
}
}//ActionList
}
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,cmd
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(row
table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//Scrol
table.setRowSelectionAllow
table.getModel().addTableM
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(cmdSetValu
buttonPanel.add(cmdGetValu
cmdOpen.addActionListener(
cmdSave.addActionListener(
cmdNew.addActionListener(t
cmdAdd.addActionListener(t
cmdDelete.addActionListene
cmdSetValue.addActionListe
cmdGetValue.addActionListe
mainPanel=new JPanel();
frame=new JFrame("Table Example");
frame.setDefaultCloseOpera
frame.setSize(800,600);
frame.setExtendedState(JFr
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",tool
mainPanel.add("Center",scr
mainPanel.add("South",butt
mainPanel.setBackground(Co
buttonPanel.setBackground(
table.getParent().setBackg
frame.getContentPane().add
frame.setVisible(true);
mainPanel.setBorder(Border
}
public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String
}
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.e
String msg="";
TableModel tabMod = (TableModel)source.getSour
switch (source.getType())
{
case TableModelEvent.UPDATE:
msg="Table Value Updated for cell "+table.getSelectedRow()+"
JOptionPane.showMessageDia
break;
}
}//Table Changed Method
public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{
table.setRowSelectionInter
table.setColumnSelectionIn
}
}
public void actionPerformed(ActionEven
{
if (source.getSource()==(JBut
{
openFile();
}
if (source.getSource()==(JBut
{
saveFile();
}
if (source.getSource()==(JBut
{
newFile();
}
if (source.getSource()==(JBut
{
addRow();
}
if (source.getSource()==(JBut
{
deleteRow(table.getSelecte
}
if (source.getSource()==(JBut
{
String CName=JOptionPane.showInpu
if(!CName.trim().equals(""
{
selectCell(0,2);
table.setValueAt(CName,0,2
}
}
if (source.getSource()==(JBut
{
if(table.getRowCount()>0)
{
String msg="Value At cell 0,0 is "+table.getValueAt(0,0).to
JOptionPane.showMessageDia
}
}
}//ActionList
}
ASKER
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
I hope that is clear,
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No worries, I give you 500 point.
Thanks,
More specific problem is @ https://www.experts-exchange.com/questions/21849814/Pop-up-menu-when-you-right-click-the-cell.html
Thanks,
More specific problem is @ https://www.experts-exchange.com/questions/21849814/Pop-up-menu-when-you-right-click-the-cell.html
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?