Antony Patric

Greenhorn
+ Follow
since Apr 24, 2008
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 Antony Patric

Hi all,

I have a JTable with a few columns and I need to set specific widths for each of the columns.I did that using setMaxWidth,setMinWidth and setPreferredWidth.

Actually the specific portion id invoked when a button is clicked.The first time the code works fine and the columns are displayed in the correct widths.But when I click the button again the widths are changed and all the columns take equal spacing and occupy the space of the scrollpane.

If anybody knows the problem please help me...

I am posting the portion of code below..

public void displayhistory() {
TableColumn column=null;
colshist=new Vector();
rowshist=new Vector();
String colnames[]={"Prescription ID","Medicine ID","Prescription Date","Prescribed By","Dose","Frequency","Duration","Medication Time","Notes"};
int colwidths[]={105,75,100,150,80,85,90,85,200};
TableColumnModel colmodel=null;
for(int i=0;i<colnames.length;i++) {
colshist.addElement(colnames[i]);
}

if(flag2==0)
{
tabhistmodel= new DefaultTableModel();
flag2++;
}

tabhistmodel.setDataVector(rowshist,colshist);
populaterowshist();
tablehistory=new JTable(tabhistmodel);
int height=tablehistory.getRowHeight();
tablehistory.setRowHeight(height+5);


colmodel=tablehistory.getColumnModel();
for(int i=0;i<colmodel.getColumnCount();i++) {

System.out.println("inside column width");
column=colmodel.getColumn(i);

column.setMaxWidth(colwidths[i]);
column.setMinWidth(colwidths[i]);
column.setPreferredWidth(colwidths[i]);
System.out.println(column.getMaxWidth()+" maxwidth");
System.out.println(column.getMinWidth()+" minwidth");

}


tablehistory.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

tablehistory.getTableHeader().setReorderingAllowed(false);
tablehistory.getTableHeader().setResizingAllowed(false);

jspanehist = new JScrollPane(tablehistory);
}



Regards,
Antony
13 years ago
Hi all,

I have a defaulttable model and I need to place a checkbox in one of the columns.I used this code below:

table.getColumnModel().getColumn(8).setCellEditor(new DefaultCellEditor(new JCheckBox()));

But I got the value as String in the columns cells and not as checkboxes.
Could someone help me?I need to have the real checkboxes instead of the String in the cells.

Regards,
Antony
13 years ago
Hi all,

I am stuck with a problem here.Could someone please help me out.

I have a table and in one of the columns i have given a combobox with the autofill property.
The problem is that when I type in the letters the autofill works just fine but when i select the option then it selects not only the current cell of the table but also the cells below it.But if I do the same on a lower cell the cells below are affected but the cells above are in tact.

Here is a part of the code:


tf = (JTextField)jcb.getEditor().getEditorComponent();
tf.addCaretListener(this);


tf.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{if (e.getKeyCode() == e.VK_ENTER)
{
Object value = comboUi.getList().getSelectedValue();
tf.transferFocus();
jcb.hidePopup();
System.out.println("enter pressed, selected value, when enter pressed: "+value);
fireEditingStopped();
}
}
});

jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// fireEditingStopped();
}
});

}
/* public JList getList()
{
return listBox;
}
*/

public void focusGained(FocusEvent fe)
{
System.out.println("focusgained");
}
public void focusLost(FocusEvent fe) {
System.out.println("focuslost");
}
public void addCellEditorListener(CellEditorListener listener) {
System.out.println("addCellEdtrlistner");

listenerList.add(CellEditorListener.class, listener);
}

public void removeCellEditorListener(CellEditorListener listener) {
System.out.println("removeCellEdtrlistner");
listenerList.remove(CellEditorListener.class, listener);

}

protected void fireEditingStopped() {
System.out.println("fireEditingStopped called ");

CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
tf.setText(newValue.toString());

}


protected void fireEditingCanceled() {

System.out.println("fireEditingCanceled called ");

CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
// tf.setText(newValue.toString());
}

public void cancelCellEditing() {
System.out.println("cancelCellEditing called ");
fireEditingCanceled();
}

public boolean stopCellEditing() {
System.out.println("stopCellEditing called ");
fireEditingStopped();
return true;
}


public boolean isCellEditable(EventObject event) {
System.out.println("iscellEditable");

return true;
}

public boolean shouldSelectCell(EventObject event) {
System.out.println("shouldSelectcell");
return false;
}

public Object getCellEditorValue() {
System.out.println("getCellEditorValue called returning vlaue: "+newValue);
//tf.setText(newValue.toString());
// o1.table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(tf));
// o1.table.getModel().setValueAt(tf,2,1);

return newValue;

// return super.getSelectedItem();
}

/*public void setcellEditorValue(Object obj) {
System.out.println("setcelleditor");
tf.setText(obj.toString());
}
*/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {

return jcb;
}

public void caretUpdate(CaretEvent e) {
System.out.println("caretUpdate");
System.out.println(++caretctr);
// newValue=" ";
if (!jcb.isPopupVisible() && tf.isShowing() && tf.hasFocus())
jcb.showPopup();
JTextField tf = (JTextField)e.getSource();
String text = tf.getText().toLowerCase();

int index = -1;
for (int i = 0; i < jcb.getItemCount(); i++) {
String item = ((String)jcb.getItemAt(i)).toLowerCase();
if (item.startsWith(text))
{
index = i;
break;
}
}
if (index != -1 )
{
comboUi.getList().setSelectedIndex(index);
System.out.println(comboUi.getList().getSelectedIndex());
System.out.println(comboUi.getList().getSelectedValue());
}
else
comboUi.getList().clearSelection();

newValue = comboUi.getList().getSelectedValue();
System.out.println("new value set to: "+newValue);

}
}


this is the code for the renderer..

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,int column) {
System.out.println("row and col "+row+" "+column);
System.out.println("getTableCellRenderercmpnt hasfocus"+hasFocus);
System.out.println("getTableCellRenderercmpnt isSelected"+isSelected);
if (hasFocus)
{
System.out.println("has focus-is selected "+isSelected);
table.editCellAt(row,column);
JComboBox combo = (JComboBox)table.getColumnModel().getColumn(1).getCellEditor();
ComboBoxEditor editor = (ComboBoxEditor)combo.getEditor();
tf = (JTextField)editor.getEditorComponent();
tf.requestFocusInWindow();
System.out.println("selectall");
tf.selectAll();

}
if (isSelected) {

jcb.setForeground(table.getSelectionForeground());
jcb.setBackground(table.getSelectionBackground());
System.out.println("row "+row+" col "+column);
} else {
jcb.setForeground(table.getForeground());
jcb.setBackground(table.getBackground());
}
//table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(tf));
//table.getModel().setValueAt(tf.getText(),row,column);

jcb.setSelectedItem(value);

return jcb;
}

}

Please forgive me for giving all those print statements...
Any solution would be helpful...
Thanks,
Antony
13 years ago
Hi,
Could someone tell how to give multiple span column header.The header must be the same for 2 columns (or more) as required.
14 years ago
Hi,
Could someone tell me how to give row header spanning multiple columns(i.e. 2 or more columns).
14 years ago
Hi,
how do we give the java script to prompt the text to be entered.
What I mean is that,I have a set of names and when I enter the first letter the names starting with that letter should be viewed and the second letter and so on..
If anybody knows how to do it please help...

Regards,
Antony
Hi,
Could anyone help me...
I visited a site www.w3schools.com ,and in that when we select a particular topic the link remains to be bold until we select another topic or link.

we can give different looks for the links by using CSS and using the attributes hover,visited,etc but i couldn't find anything with which this is possible.
When I took the source code of the page I could see that the link was mentioned to be bold explicitly.But somebody told me that they used javascript to edit the html code and give the bold view.

I have given the sample source code from the page.

Thanks,
Antony

[Eric- edit - added code tags]
[ June 12, 2008: Message edited by: Eric Pascarello ]