Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!

Kannan Somaiah

Ranch Hand
+ Follow
since Jun 06, 2006
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 Kannan Somaiah

How to delete this message from javaranch?
Is there any way for it?
9 years ago
Right now I am getting either true or false for all the checkboxes irrespective of whether i have selected/deselected all/few.

Thanks in advance,
Kannan Somaiah
14 years ago
Sorry.. typo mistake.. please read the subject as "Finding how many checkboxes are selected in a table?"

Regards,
Kannan Somaiah
14 years ago
Hi All,

I have an requirement wherein i need to count the number of JCheckboxes selected in a JTable column. I am using a separate renderer and editor for doing this.

Following is the code that I am using now. But I am not able to accomplish this. Please help me out.

Thanks,
Kannan




package com.emirates.res.easymars.ui.tools;

import static com.emirates.res.easymars.util.CCGUIUtilities.MAIN_PANEL_BGCOLOR;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.DefaultCellEditor;

import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.html.HTMLEditorKit;

import com.emirates.res.easymars.core.t2se.CCGUIMessageBuilder;
import com.emirates.res.easymars.core.ui.CCGUIComboBox;
import com.emirates.res.easymars.core.ui.CCGUIPanel;
import com.emirates.res.easymars.core.ui.CCGUITable;
import com.emirates.res.easymars.core.ui.CCGUITextField;
import com.emirates.res.easymars.core.ui.CallCentreDialog;
import com.emirates.res.easymars.core.ui.CommandTask;
import com.emirates.res.easymars.core.ui.RequestWorker;
import com.emirates.res.easymars.core.ui.TaskListener;
import com.emirates.res.easymars.core.ui.exception.CCGUIException;
import com.emirates.res.easymars.core.ui.exception.ReLoginException;
import com.emirates.res.easymars.ui.ticketing.CCGUITicketUtilities;
import com.emirates.res.easymars.util.CCGUIUtilities;

public class MultipleTicketsDisplayPanel extends CCGUIPanel implements
CommandTask, TaskListener {
private static GridBagLayout gbl1 = new GridBagLayout();

private static CCGUIComboBox subjectCombo;

private CCGUITextField toTxt;

private static Dimension TABLE_SIZE;

private static CallCentreDialog dialog = null;

private RequestWorker worker;

private CCGUITable multipleTicketsTable = null;

private DefaultTableModel multipleTicketsModel = null;

private CCGUIComboBox languageCombo;

public static String[] tickets = null;

// Changes start
private static JEditorPane editorPane = new JEditorPane();

private ArrayList<String> eTktNumber = null;

static ArrayList<JEditorPane> arrHTMlEditorPane = new ArrayList<JEditorPane>();

private static JEditorPane htmlEditorPane = new JEditorPane();

// Changes end
private static String newEReceipt;

private String rLoc = null;

private static final String CONTENTTYPE_TEXT_PLAIN = "text/plain";

public static boolean IS_NEW_ERECEIPT_SELECTED = false;

public static boolean IS_MULTIPLE_ERECEIPT_SELECTED = false;

static {
TABLE_SIZE = new Dimension(625, 120);
}

public MultipleTicketsDisplayPanel() {
createComponentLayout();
}

public void createComponentLayout() {
setBackground(CCGUIUtilities.MAIN_PANEL_BGCOLOR);
GridBagLayout gbl = (GridBagLayout) super.getCCGUILayout();
GridBagConstraints gbc = super.getGridBagConstraints();
this.setLayout(gbl);
gbc.weightx = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
}

JPanel createMultipleTicketsPanel(Object[] multipleTickets) {

Object[][] dataValues = new Object[multipleTickets.length][3];
Object columnNames[] = { "Select", "Ticket Number" };
Properties prop = new Properties();

JPanel panel = new JPanel();
String ticket = null;
for (int i = 0; i < multipleTickets.length; i++) {
ticket = (String) multipleTickets[i];
dataValues[i][0] = Boolean.TRUE;
dataValues[i][1] = ticket.substring(23, ticket.length());
}
multipleTicketsModel = new DefaultTableModel(dataValues, columnNames);
multipleTicketsTable = new CCGUITable(multipleTicketsModel);
multipleTicketsTable.getColumnModel().getColumn(0).setCellRenderer(
new MyCellRenderer());
JCheckBox checkBox = new JCheckBox();
multipleTicketsTable.getColumnModel().getColumn(0).setCellEditor(new MyCellEditor());
//multipleTicketsTable.getColumn(0).setCellRenderer(
//new MyCheckRenderer());
//multipleTicketsTable.getColumn(0).setCellEditor(
//new MyCheckEditor());
JScrollPane scrollPane = new JScrollPane(multipleTicketsTable);
scrollPane.setPreferredSize(TABLE_SIZE);
scrollPane.setMinimumSize(TABLE_SIZE);
multipleTicketsTable.setSize(new Dimension(50, 100));
multipleTicketsTable.getColumn("Select").setMaxWidth(50);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(gbl);
panel.setBackground(MAIN_PANEL_BGCOLOR);
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = CCGUIUtilities.ZERO_INSETS;
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(scrollPane, gbc);
panel.add(scrollPane);
JPanel panel1 = new JPanel();
panel1.setLayout(gbl1);
panel1.setBackground(MAIN_PANEL_BGCOLOR);
gbc.anchor = GridBagConstraints.SOUTH;
gbc.gridx = 0;
gbc.gridy = 0;
JButton okButton = new JButton();
okButton.setBorderPainted(true);
okButton.setText("Ok");
gbl1.setConstraints(okButton, gbc);
EmailListener1 listener = new EmailListener1(multipleTicketsTable);
okButton.addActionListener(listener);
panel1.add(okButton);
gbc.gridx = 1;
gbc.gridy = 0;
JButton cancelButton = new JButton();
cancelButton.setBorderPainted(true);
cancelButton.setText("Cancel");
gbl1.setConstraints(cancelButton, gbc);
panel1.add(cancelButton);
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(panel1, gbc);
panel.add(panel1);
return panel;
}

public void executeCommandTask() {

}

public void onTaskFailure(Exception e) {
CCGUIUtilities.showErrorMessage(e.getMessage(), "Tools : Email");
}

/*
* Added by Panneer Selvam. @Date 19-06-2006
*/

public void onTaskCompleted(Object result) {
setCursor(CCGUIUtilities.NORMAL_CURSOR);
resetTaskDetail();
String actionType = worker.getActionType();
try {
if (!worker.isFailure()) {

if (actionType.equals("new_ereceipt")) {

// eTktNumber.get(0));
newEReceipt = BookingFormatUtil.textToNewTicketFormat(
result, eTktNumber.get(0));
eTktNumber.remove(0);
if (eTktNumber.size() > 0) {

startETicketWorker((String) eTktNumber.get(0));
}

if (eTktNumber.size() == 0) {
EMailPanel.loadTicketDetailsHtml(newEReceipt);

String s = new String();

if (multipleTicketsTable.getRowCount() == tickets.length) {
s = tickets[0].trim() + "-"
+ tickets[tickets.length - 1].trim();
} else {
for (int i = 0; i < tickets.length; i++) {
if (i == tickets.length) {
s = s + tickets[i].trim();
} else {
s = s + tickets[i].trim() + ", ";
}
}
}


EMailPanel.subjectCombo.addItem(s);
// EMailPanel.subjectCombo.setSelectedIndex(EMailPanel.subjectCombo.getItemCount()
// - 1);
// EMailPanel.subjectCombo.setItems(new Object[]{s});
}
EMailPanel.dialog.setVisible(false);
}
}

} catch (CCGUIException ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(), "Tools : Email");
} catch (ReLoginException e) {
CCGUIUtilities.showReLoginMessage();
}
}

public static void loadEmailHtmlContent(String filePath) {
try {
htmlEditorPane.setEditorKit(new HTMLEditorKit());
htmlEditorPane.setPage(filePath);
setCurorAtStart();

} catch (Exception ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(), "Tools - Email");
ex.printStackTrace();
}
}

public static String loadTicketDetailsHtml(String htmlContents) {
final int fontSize4Display = 11;

if (htmlContents != null) {
newEReceipt = htmlContents;
try {
clearHtmlEditorPane();
htmlEditorPane.setBackground(java.awt.Color.WHITE);
htmlEditorPane.setText(resizeHtml(htmlContents,
fontSize4Display));
setCurorAtStart();
} catch (Exception ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(),
"Tools - Email");
ex.printStackTrace();
}
}
return htmlContents;
}

private static void setCurorAtStart() {
htmlEditorPane.requestFocusInWindow();
htmlEditorPane.setCaretPosition(0);
}

private static void clearHtmlEditorPane() {
htmlEditorPane.setContentType("text");
htmlEditorPane.setContentType(CONTENTTYPE_TEXT_PLAIN);
htmlEditorPane.setText("");
htmlEditorPane.setBackground(CCGUIUtilities.TEXT_FIELD_BGCOLOR);
htmlEditorPane.setContentType("text/html");
}

private static String resizeHtml(String html, int fontSize) {

int tmp = (2 * fontSize) + 3;
html = html
.replaceAll("font-size:11pt", "font-size:" + fontSize + "pt");
html = html.replaceAll("font-size:25px", "font-size:" + tmp + "px");
html = html.replaceAll("font-size:8pt", "font-size:" + fontSize + "pt");
html = html.replaceAll("font-size:30px", "font-size:" + tmp + "px");
html = html
.replaceAll(
"<tr id='TEMPROW'><td colspan='6' height='1' bgcolor='black'></td></tr>",
"");

return html;
}

public class EmailListener1 implements ActionListener {

CCGUITable table = null;

public EmailListener1(CCGUITable multipleTicketsTable) {
this.table = multipleTicketsTable;
}

public void actionPerformed(ActionEvent arg0) {
performAction();

CallCentreDialog dialog = MultipleTicketsDisplayPanel.dialog;

if (dialog != null) {
dialog.dispose();
}
}

public void performAction() {

eTktNumber = new ArrayList<String>();

//Changes start

System.out.println("********" + table.getCellEditor(0, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(1, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(2, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(3, 0).getCellEditorValue());

//Changes end

for (int i = 0; i < table.getRowCount(); i++) {
eTktNumber.add(i, (String) table.getValueAt(i, 1));
}

IS_MULTIPLE_ERECEIPT_SELECTED = true;
tickets = new String[eTktNumber.size()];
eTktNumber.toArray(tickets);

//changes start

File file = new File("D:/EASYMARS/");
File[] arrFile = file.listFiles();
int iLength = arrFile.length;
String name = null;
for(int i = 0 ; i < iLength ; i++)
{
name = arrFile[i].getName();
if(name.length() > 4 && name.substring(name.length() - 4, name.length()).equals(".pdf"))
{
arrFile[i].delete();
}
}
//changes end

startETicketWorker(eTktNumber.get(0));
}
}

private void startETicketWorker(String eTktNumber) {

initMultiEReceiptWorker(eTktNumber);
IS_MULTIPLE_ERECEIPT_SELECTED = true;
if (worker != null)
worker.start();
}

private void initMultiEReceiptWorker(String eTktNumber) {
setCursor(CCGUIUtilities.WAIT_CURSOR);
setTaskDetail("Send e-Receipt in progress", true);
String request = CCGUITicketUtilities.NEW_MULTIPLE_ERECEIPT_REQ_MSG
.format(new Object[] { initET(eTktNumber) });
Object message = CCGUIMessageBuilder.createEReceiptFormatMsg(request,
"EReceipt");

worker = new RequestWorker(this, 1, "new_ereceipt");
worker.setMessage(message);
}

private String initET(String eTktNumber) {
int cnt = 1;
StringBuffer buffer = new StringBuffer("");
buffer.append(CCGUITicketUtilities.ETKT_REQ_MSG
.format(new Object[] { eTktNumber.trim() }));
return buffer.toString();
}

private class MyCellRenderer implements TableCellRenderer
{
protected Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
private JCheckBox checkBox;
public MyCellRenderer()
{
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.white);
}
public Component getTableCellRendererComponent(JTable
table, Object value, boolean isSelected, boolean
hasFocus, int row, int column)
{
checkBox.setSelected( Boolean.valueOf( value.toString() ).booleanValue() );
return checkBox;
}
}

private class MyCellEditor extends AbstractCellEditor implements TableCellEditor
{
JCheckBox checkBox = new JCheckBox();
public MyCellEditor()
{
//checkBox = new JCheckBox();
//checkBox.setHorizontalAlignment(SwingConstants.CENTER);
//checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
checkBox = new JCheckBox((String) value);
System.out.println("value is getTableCellEditorComponent method is: " + value);
checkBox.setSelected( Boolean.valueOf( value.toString() ).booleanValue() );


return checkBox;
}
public Object getCellEditorValue()
{
return Boolean.valueOf(checkBox.isSelected());
}
}
}
14 years ago
Hi,

Can anyone help me out in this?

Regards,
Kannan Somaiah
14 years ago
Hi,

I am working on Java swings. I have a case wherein I have a JButton present inside a JTable.

I have also written a ButtonRenderer for the same.

Problem I am facing now is that I am not able to click the button .. In simple words focus is not coming to the button when I click on... but somehow if I get the focus on the button(with the use of tab key), I am able to click the button.

Hope I made the issue clear.

Can anyone help me in this?

My renderer code is below

Thanks in advance,
Kannan Somaiah

package com.emirates.res.easymars.ui.fares;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.emirates.res.easymars.core.ui.CCGUIButton;

public class CCGUIButtonRenderer implements TableCellRenderer {

String[] interfaces;
CCGUIButton button;
public CCGUIButtonRenderer(String[]interfaces) {
//setOpaque(true);
//super("Hello");
this.interfaces = interfaces;
}

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {

if(column == 3 && row == 0) {
button = new CCGUIButton(interfaces[0], interfaces[0]);
button.setText(interfaces[0]);
}if(column == 3 && row == 1) {
button = new CCGUIButton(interfaces[1], interfaces[1]);
button.setText(interfaces[1]);
}if(column == 3 && row == 2) {
button = new CCGUIButton(interfaces[2], interfaces[2]);
button.setText(interfaces[2]);
}if(column == 3 && row == 3) {
button = new CCGUIButton(interfaces[3], interfaces[3]);
button.setText(interfaces[3]);
}
return button;
}
}
14 years ago
Hi,

I started with gridBagLayout as I have to add many more components to the same panel.

So can I solve this problem using GridBagLayout itself?

Please advice.

Regards,
Kannan Somaiah
14 years ago
I tried the code snippet you had sent. But I couldn't crack it.

Regards,
Kannan Somaiah
14 years ago
Hi,

Thanks for your erply. I am adding components to a JPanel. I don't have frame here. Is there any similar method which can be applied to JPanel and get the same things done on a JPanel?

Thanks,
Kannan Somaiah
14 years ago
I want to add my components in the North side.

Thanks,
Kannan Somaiah
14 years ago
Hi ALl,

How to add components from the top of the JPanel using GridBagLayout?

I hope by default it starts from the centre.

Here is my code.


JPanel panel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(gbl);
priceButton = new JButton();
panel.add(priceButton);

I tried diff options but it didnt work.

Thanks in advance.

Regards,
Kannan Somaiah
14 years ago
Hi,

Thanks a lot. I got what you said.

I will get back to you in case if I have any doubt.

Regards,
Kannan Somaiah
14 years ago
Hi,

I used the following code and I didnt get exception in the console...

String url = "https://10.14.3.31:80/cpg/Order.jsp";
URL server = new URL(url);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();


It means.. it is conencted... right?

I am not able to test the negative case... What will I get in the negative case wherein the URL is down.

Thanks.. I have changed my name now.

Regards,
Kannan Somaiah
14 years ago
Hi,

I tried the following code

String url = "http://sun.com/osc";
URL server = new URL(url);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();

I gave an URL which is working.. but I got an java.net.ConnectException

Regards,
Kannan Somaiah
14 years ago
Hi,

Thanks for your reply.

I tried URL(String url) ... but it didn't work..

Now I am looking into what you have said..

Regards,
Kannan Somaiah
14 years ago