seema prakash

Ranch Hand
+ Follow
since Nov 17, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
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 seema prakash

If I have an application's UI written in Java Swing, would it require lot of
changes to convert it to an eclipse plug in ? Does the book cover all those ?
I would really like to read the book and try out the examples and decide on switching over to Netbeans from Eclipse.
You think switching over from Eclipse to Netbeans is easy ?
Does the book have examples that a beginner would be able to follow and can try out?
Yes, I agree that the complex code has to be refactored and the method names and variable names should be descriptive enough.

What about the comments we add after fixing a bug ? Is it not a good practice to do that ? If we have nested loops, then it is easier to have some comment such as "//end of if". What about such comments?
Writing Comments: I definitely believe writing descriptive comments makes it easier for the developer to understand at a glance what is going on in the code easily especially when the code is complex. Do you agree ?
[ September 23, 2008: Message edited by: Ilja Preuss ]
Thanks a lot for the help. Yes, I had used the member variable as you said. But, instead of eliminating it, I assigned the variable the value table.getSelectedRow() instead of just using the variable. I corrected it. It works fine as desired.
15 years ago
Hi,
Thanks for the reply. But, I still have the same problem.
It still shows the wrong selected row value.
15 years ago
Sorry the code is not formatted correctly.

Here is the formatted one.

My sample code for the table and the main is below:





The code for the editor and renderer are below:



Thanks.
15 years ago
Hi,

I have a jtable with different editors for the columns.
The first column(col 0) had a combobox editor and renderer.
The second col(col 1) has a combobox editor and the default editor(jtextfield) depending upon the selection in the jcombobox of the first column.

First I add a row and in the first combo box I choose "Non Work" item. Then I add another row and choose "Exclude" item from the first combo box. The display is fine. But, now When I go to the first row(row 0)
and first col(col 0) to choose a different item, the selected row has to be 0 but instead it shows 1. I have a print statement that prints the selected row value on the console. That is the reason col 1 displays wrong value. I'm not sure why this happens. Please help.

My sample code for the table and the main is below:

{code}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class BusinessRuleTable extends JTable {

String[] colNames = new String[] { "RuleType", "Date", "Action/Type", "Description" };

private DefaultTableModel _model = null;
private CWComboBoxEditor editor1 = null;
private CWComboBoxEditor editor2 = null;
private CWComboBoxEditor editor3 = null;
private CWComboBoxEditor editor4 = null;
private BusinessRuleDateColEditor dateEditor = null;
private CWComboBoxRenderer renderer = null;
private JComboBox comboBox1 = null;
private JComboBox comboBox2 = null;
private JComboBox comboBox3 = null;
private JComboBox comboBox4 = null;

public BusinessRuleTable(DefaultTableModel model){
_model = model;

_model.setColumnIdentifiers(colNames);
this.setModel(_model);

//Use a combo box as the editor
editor1 = new CWComboBoxEditor(getComboBox1(loadRuleTypeValues()));
JComboBox cbx1 = (JComboBox) editor1.getComponent();
cbx1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(e.getSource() instanceof JComboBox){
JComboBox o = (JComboBox) e.getSource();
String str = (String) o.getSelectedItem();
int selectedRow = BusinessRuleTable.this.getSelectedRow();

System.out.println("selectedRow = "+selectedRow);

if(BusinessRuleTable.this.getSelectedRow() >-1){
if(str.equals("Exclude")){
BusinessRuleTable.this.setValueAt("Gregorian", BusinessRuleTable.this.getSelectedRow(), 2);
BusinessRuleTable.this.setValueAt("", BusinessRuleTable.this.getSelectedRow(), 1);
}
else if(str.equals("Non Work")){
BusinessRuleTable.this.setValueAt("RollForward", BusinessRuleTable.this.getSelectedRow(), 2);
BusinessRuleTable.this.setValueAt("Sunday", BusinessRuleTable.this.getSelectedRow(), 1);
}else if(str.equals("Holiday")){
BusinessRuleTable.this.setValueAt("RollForward", BusinessRuleTable.this.getSelectedRow(), 2);
BusinessRuleTable.this.setValueAt("", BusinessRuleTable.this.getSelectedRow(), 1);
}
}
}

}});
editor2 = new CWComboBoxEditor(getComboBox2(loadActionTypeForExclude()));
editor3 = new CWComboBoxEditor(getComboBox3(loadActionTypeForHolidayAndNonWork()));
editor4 = new CWComboBoxEditor(getComboBox4(loadDateForHoliday()));

renderer = new CWComboBoxRenderer();

}

private JComboBox getComboBox1(List items){
if(comboBox1 == null){
comboBox1 = new JComboBox();
}
comboBox1.removeAllItems();
comboBox1.setEditable(false);

Iterator iter = items.iterator();
while (iter.hasNext()) {
comboBox1.addItem(iter.next());
}
return comboBox1;
}

private JComboBox getComboBox2(List items){
if(comboBox2 == null){
comboBox2 = new JComboBox();
}
comboBox2.removeAllItems();
comboBox2.setEditable(false);
Iterator iter = items.iterator();
while (iter.hasNext()) {
comboBox2.addItem(iter.next());
}
return comboBox2;
}

private JComboBox getComboBox3(List items){
if(comboBox3 == null){
comboBox3 = new JComboBox();
}
comboBox3.removeAllItems();
comboBox3.setEditable(false);

Iterator iter = items.iterator();
while (iter.hasNext()) {
comboBox3.addItem(iter.next());
}
return comboBox3;
}

private JComboBox getComboBox4(List items){
if(comboBox4 == null){
comboBox4 = new JComboBox();
}
comboBox4.removeAllItems();
comboBox4.setEditable(false);

Iterator iter = items.iterator();
while (iter.hasNext()) {
comboBox4.addItem(iter.next());
}
return comboBox4;
}

List loadRuleTypeValues(){
List ruleTypeValues = new ArrayList();
ruleTypeValues.add("Exclude");
ruleTypeValues.add("Non Work");
ruleTypeValues.add("Holiday");
return ruleTypeValues;
}

List loadActionTypeForExclude(){
List lst = new ArrayList();
lst.add("Gregorian");
lst.add("Julian");
return lst;
}

List loadActionTypeForHolidayAndNonWork(){
List lst = new ArrayList();
lst.add("RollForward");
lst.add("RollBackward");
return lst;
}

List loadDateForHoliday(){
List lst = new ArrayList();
lst.add("Monday");
lst.add("Tuesday");
lst.add("Wednesday");
lst.add("Thursday");
lst.add("Friday");
lst.add("Saturday");
lst.add("Sunday");
return lst;
}

public TableCellEditor getCellEditor(int row, int col) {
if(col == 0 ){
return editor1;
} else if (col == 2) {
Object value = getModel().getValueAt(row, 0);

if(value.equals("Exclude")){
//set editor2 for col2
return editor2;
}else if(value.equals("Holiday") || value.equals("Non Work")){
//set editor3 for col2
return editor3;
}else {
return editor2;
}
} else if (col == 1) {
Object value = getModel().getValueAt(row, 0);
if(value.equals("Non Work")){
return editor4;
}

}

return super.getCellEditor(row, col);
}

public TableCellRenderer getCellRenderer(int row, int column) {

if (column == 0 || column == 2) {
return renderer;
}

if (column == 1){
Object value = getModel().getValueAt(row, 0);
if(value.equals("Non Work")){
return renderer;
}
}
return super.getCellRenderer(row, column);
}

}



{code}

{code}

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

public class BusinessRuleTest1 extends JFrame {

private BusinessRuleTable tblSelection;
private DefaultTableModel tblSelectionModel;
/**
* Constructor
*/
public BusinessRuleTest1(){
tblSelectionModel = new DefaultTableModel();

tblSelection = new BusinessRuleTable( tblSelectionModel );
tblSelection.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
int col = tblSelection.getSelectedColumn();
int row = tblSelection.getSelectedRow();
System.out.println("col = " + col + "row = "+row);
}
});

getContentPane().add( new JScrollPane( tblSelection ), BorderLayout.CENTER );

JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int rows = tblSelectionModel.getRowCount();
Object rowData[] = { "", "", "" };
tblSelectionModel.insertRow(rows , rowData);
}
});
getContentPane().add(btnAdd, BorderLayout.SOUTH);
}

static void createAndShowGUI() {
BusinessRuleTest1 frame = new BusinessRuleTest1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

}


{code}

The code for the editor and renderer are below:
{code}

import java.awt.Component;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
/**
* Returns a JComboBox as the editor in sync with the CWComboBoxRenderer
*
*/
public class CWComboBoxEditor extends DefaultCellEditor implements TableCellEditor{
/**
* Constructor for the CWComboBoxEditor object
*
*@param cbr Description of the Parameter
*/

public CWComboBoxEditor(JComboBox cbx) {
super(cbx);
}


/**
* Constructor for the CWComboBoxEditor object
*
*@param vector Description of the Parameter
*/
public CWComboBoxEditor(Vector vector) {
super(new JComboBox(vector));
}


/**
* Constructor for the CWComboBoxEditor object
*
*@param objects Description of the Parameter
*/
public CWComboBoxEditor(Object[] objects) {
super(new JComboBox(objects));
}


/**
* Gets the tableCellEditorComponent attribute of the CWComboBoxEditor
* object
*
*@param table Description of the Parameter
*@param value Description of the Parameter
*@param isSelected Description of the Parameter
*@param row Description of the Parameter
*@param col Description of the Parameter
*@return The tableCellEditorComponent value
*/
public Component getTableCellEditorComponent
(JTable table, Object value,
boolean isSelected,
int row, int col) {

return super.getTableCellEditorComponent(table, value, isSelected, row, col);
}


}

{code}
{code}

import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;

public class CWComboBoxRenderer extends JComboBox implements TableCellRenderer {


Border border = new EmptyBorder(1,2,1,2);
DefaultTableCellRenderer defaultRenderer = new DefaultTableCellRenderer();

/**
* Constructor for the CWComboBoxRenderer object
*/
public CWComboBoxRenderer() {
super();
}

/**
* Gets the tableCellRendererComponent attribute of the CWComboBoxRenderer
* object
*
*@param table Description of the Parameter
*@param value Description of the Parameter
*@param isSelected Description of the Parameter
*@param hasFocus Description of the Parameter
*@param row Description of the Parameter
*@param column Description of the Parameter
*@return The tableCellRendererComponent value
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {

//
// Render non-editable cells using the default table cell renderer (i.e. JLabel)
//
if (!table.isCellEditable(row, column)) {
return defaultRenderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}

//
// Render editable cells using a JComboBox
//
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
if (hasFocus)
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
else
setBorder(border);

this.removeAllItems();
this.addItem(value);

return this;

}

/**
* return Look and Feel button width.
*/
public static int getComboButtonWidth() {
return 20;

}

}

{code}

Thanks.
15 years ago
Hi,

I have a sample table with 3 columns with the first and the third columns rendered as checkboxes.

I want the check box in the third column to be displayed only if the checkbox in the first column

is checked. Can anyone please guide me how to go about.

The sample code is below:

15 years ago
Thanks a lot for the help. It works fine.
16 years ago
Hi,

I just created a sample program after reading the tutorials.
The second column has JTextField as an editor. It converts all the text into uppercase and renders it the same way.

But, I want to have the cell in the second column to change based on a condition. For this I have the ChangeUpperCaseEditor class. When I use this as editor it does not render correctly. What may be the problem? Please help.

The sample code is:
16 years ago
int minRowCount = Math.min(rsRowCount,rs1RowCount);
16 years ago
Hi,
I have a PDf-file which i can open when I click on a button. But, is it possible
to open a specific bookmark(Page) on this button click.

Here is the code to open a pdf file:


Please help.
Thanks
16 years ago