Re: Textfield selection - rows insertion
On Sep 24, 5:46 pm, Sriram <sxb4...@gmail.com> wrote:
On Sep 24, 2:25 pm, Sriram <sxb4...@gmail.com> wrote:
On Sep 24, 2:14 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
Sriram wrote:
Hi
Is it possible to enter a series of textvalues in a row of
textfields and upon clicking the down button a empty row is inserte=
d
below between the previous row(where the values were entered) and t=
he
next row of textfields?
Something like this
tf1 tf2 tf3 tf4 ( valuesin each textfield) Button
tf5 tf6 tf7 tf8 (values or no values here)
tf9 tf10 tf11 tf12
After button click , it comes like this
tf1 tf2 tf3 tf4
New row is added here tfnew1 tfnew2 tfnew3 tfnew4
tf5 tf6 tf7 tf8
tf9 tf10 tf11 tf12
Similarly can it be possible to insert a row above viceversa like t=
he
above case?.... on click of another button say Up button
I had no idea because sometimes selected focus on the textfields is
hard and insertion of one row below or above is tough... How can th=
is
be possible with just textfields but that is the way my boss wants
it....A JList would be easier with the selectedindex property
available....
Is there anyone out who could help me with a sample code perhaps
How did you get on with writing a custom layout manager as I suggeste=
d
when you raised this subject a while ago?
--
RGB- Hide quoted text -
- Show quoted text -
I was able to get this working with a custom JList and there are many
references. Even you suggested a Jtable would be nice to use and that
is what exactly I did... but my boss is adamant that I use the
textfields and instead I keep the field objects a list of all fied
objects textfields in each row like a list of Lists. I am unaware of
this list of Lists technique and have never worked in this before. he
says detrermine the row and like in that of excel functionality where
insertion of rows is possible u can insert like in textfield... that
is what I exaplined to him that a JTable cell renderer would help...
that is why I am here at this forum again and posing this question....
Sriram- Hide quoted text -
- Show quoted text -
Does anybody have idea in mind on how to work this with textfield
rows?
Sriram- Hide quoted text -
- Show quoted text -
Here is my sample code... I am unable to insert the column names in
the JTable column such as Code Description Value1 value2 Value3 and
have dynamic column names added to the JTable when a column is
added....Also I couldn't figure how to add One row above
(AddonerowAbove method) into the JTable when the Add Button is
pressed.Can anyone help me here? Can anyone help me how to delete the
current row of the JTable??
Thanks
Sriram
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class SpreadSheet{
class SpreadSheetModel extends AbstractTableModel
{
// A proposed data model: ArrayList of ArrayList
// Feel free to use anything you see fit...
ArrayList data = new ArrayList(65);
final Random random = new Random();
boolean inEnterMode = true;
public SpreadSheetModel()
{
for(int i=0; i<4; i++) {
data.add(new ArrayList(4));
for(int j=0; j<10; j++)
((ArrayList)data.get(i)).add("Value " + getColumnName(j+1) +
(i+1));
}
}
public int getRowCount()
{
return data.size();
}
public int getColumnCount()
{
return ((ArrayList)data.get(0)).size() + 1;
}
public Object getValueAt(int row, int col)
{
// We can return null here for the first column because
// it's going to rendered by our own renderer anyway...
// return data[row][col];
return col==0 ? null: ((ArrayList)data.get(row)).get(col-1);
}
public String getColumnName(int column)
{
return column==0 ? "" : super.getColumnName(column-1);
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col)
{
return inEnterMode && col!=0;
}
public void setValueAt(Object aValue, int row, int column)
{
System.err.println("Setting row value to: " + aValue);
ArrayList al = (ArrayList)data.get(row);
for(int i=0; i<al.size(); i++)
al.set(i, aValue);
fireTableRowsUpdated(row, row);
}
public void insertRow(int r)
{
int cols = ((ArrayList)data.get(0)).size();
ArrayList newRow = new ArrayList(cols);
data.add(r, newRow);
for(int i=0; i<cols; i++)
newRow.add("New " + i);
fireTableRowsInserted(r,r);
}
public void insertRowAbove(int r)
{
int cols = ((ArrayList)data.get(0)).size();
ArrayList newRow = new ArrayList(cols);
data.add(r, newRow);
for(int i=0; i<cols; i++)
newRow.add("New " + i);
fireTableRowsInserted(r,r);
}
public void insertColumn(int c)
{
int rows = data.size();
for(Iterator i=data.iterator(); i.hasNext();)
{
ArrayList row = (ArrayList)i.next();
row.add(c-1, "Col");
}
}
public void startEnterMode()
{
inEnterMode = true;
}
public void stopEnterMode()
{
inEnterMode = false;
}
}
final SpreadSheetModel dataModel = new SpreadSheetModel();
final JTable table = new JTable(dataModel);
class InsertAction extends AbstractAction
{
public InsertAction()
{
super("Insert");
}
public void actionPerformed(ActionEvent e)
{
if(table.getCellSelectionEnabled()) {
System.err.println("Nuts?");
return;
}
if(table.getRowSelectionAllowed()) {
int r = table.getSelectedRow();
dataModel.insertRow(r+1);
// work around problem where the old and new row are both
selected...
table.setRowSelectionInterval(r,r);
return;
}
if(table.getColumnSelectionAllowed()) {
int c = table.getSelectedColumn();
dataModel.insertColumn(c+1);
// Add a new column for the last column that
// otherwise would be shifted out of the table:
TableColumn tc = new TableColumn(table.getColumnCount());
table.addColumn(tc);
// Get the JTable updated:
table.sizeColumnsToFit(-1);
}
}
}
final Action insertAction = new InsertAction();
class EnterAction extends AbstractAction
{
public EnterAction()
{
super("Enter");
}
public void actionPerformed(ActionEvent e)
{
if(table.getCellSelectionEnabled()) {
dataModel.startEnterMode();
table.editCellAt(table.getSelectedRow(),
table.getSelectedColumn());
table.getEditorComponent().requestFocus();
dataModel.stopEnterMode();
}
else {
System.err.println("Nuts?");
}
}
}
final Action enterAction = new EnterAction();
// class DeleteAction extends AbstractAction
// {
// public DeleteAction()
// {
// super("Delete");
// }
// public void actionPerformed(ActionEvent e)
// {
// if(table.getRowSelectionAllowed()) {
// int r = table.getSelectedRow();
// dataModel.removeRow(0);
// return;
// }
// }
// final Action deleteAction=new DeleteAction();
// }
class AddAction extends AbstractAction
{
public AddAction()
{
super("Add");
}
public void actionPerformed(ActionEvent e)
{
if(table.getRowSelectionAllowed()) {
int r = table.getSelectedRow();
dataModel.insertRowAbove(r-1);
// work around problem where the old and new row are both
selected...
table.setRowSelectionInterval(r,r);
return;
}
}
final Action addAction=new AddAction();
}
final JButton clearButton = new JButton("Clear");
final JButton UpButton = new JButton("Add");
final JButton DeleteButton=new JButton("Delete");
public JToolBar createToolBar()
{
JToolBar jtb = new JToolBar();
jtb.add(insertAction);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.err.println("Clear");
table.clearSelection();
updateActionStates();
}});
jtb.add(enterAction);
jtb.add(clearButton);
jtb.add(UpButton);
jtb.add(DeleteButton);
return jtb;
}
private void updateActionStates()
{
boolean cellSel = table.getCellSelectionEnabled();
insertAction.setEnabled(!cellSel &
(table.getSelectedRowCount() +
table.getSelectedColumnCount() > 0));
enterAction.setEnabled(cellSel &
table.getSelectedRowCount() == 1 &
table.getSelectedColumnCount() == 1);
}
public SpreadSheet(final boolean haveScrollBars)
{
final JComponent tablePanel;
final JFrame frame = new JFrame( "Display Testing" );
table.setBorder( BorderFactory.createLoweredBevelBorder() );
table.getTableHeader().setReorderingAllowed( false );
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
updateActionStates();
if(haveScrollBars) {
tablePanel = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
// this one doesn't seem to work...
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
else {
// If you don't use a JScrollPane, you need to display the
header yourself:
JTableHeader tableHeader = table.getTableHeader();
tablePanel = new JPanel();
tablePanel.setLayout( new BorderLayout() );
tablePanel.add( tableHeader, BorderLayout.NORTH );
tablePanel.add( table, BorderLayout.CENTER );
}
tablePanel.setBorder( BorderFactory.createLoweredBevelBorder() );
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tablePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
tablePanel.setPreferredSize(new Dimension(640, 200));
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
System.err.println("Clicked at: " + table.rowAtPoint(p) +
table.columnAtPoint(p));
if(table.columnAtPoint(p) == 0) {
int i = table.rowAtPoint(p);
if(i < 0)
return;
table.clearSelection();
table.setCellSelectionEnabled(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addRowSelectionInterval(i,i);
//throw the focus away:
clearButton.requestFocus();
}
if(table.columnAtPoint(p) != 0 && !
table.getCellSelectionEnabled()) {
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.clearSelection();
table.setRowSelectionInterval(table.rowAtPoint(p),
table.rowAtPoint(p));
table.setColumnSelectionInterval(table.columnAtPoint(p),
table.columnAtPoint(p));
}
updateActionStates();
}
});
table.getTableHeader().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
int i = table.columnAtPoint(p);
if(i <= 0)
return;
System.err.println("Clicked[2] at: " + table.rowAtPoint(p) +
table.columnAtPoint(p));
table.clearSelection();
table.setCellSelectionEnabled(false);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addColumnSelectionInterval(i,i);
//throw the focus away:
clearButton.requestFocus();
updateActionStates();
}
});
table.getColumn("").setCellRenderer(new DefaultTableCellRenderer()
{
final JButton rowHeader = new JButton();
public Component getTableCellRendererComponent (
JTable t,
Object value,
boolean isSelected,
boolean cellHasFocus,
int r, int c )
{
if( r == -1 )
return super.getTableCellRendererComponent(
t, value, isSelected, cellHasFocus,r ,c );
else {
rowHeader.setText(String.valueOf(r+1));
return rowHeader;
}
}
});
frame.getContentPane().add( createToolBar(), BorderLayout.NORTH );
//frame.getContentPane().add( clearButton, BorderLayout.CENTER );
frame.getContentPane().add( tablePanel, BorderLayout.CENTER );
frame.pack();
frame.setVisible( true );
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
System.err.println("Now");
clearButton.requestFocus();
}
});
}
catch (java.lang.reflect.InvocationTargetException e) {}
catch (InterruptedException e) {}
}
public static void main(String args[])
{
new SpreadSheet(true);
}
}