File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes adding and manipulating elements of a CCombo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding and manipulating elements of a CCombo" Watch "adding and manipulating elements of a CCombo" New topic
Author

adding and manipulating elements of a CCombo

maroun Adolf
Greenhorn

Joined: Jun 23, 2011
Posts: 9
Hi all,
I am using SWT designer.

I wanted to make an item , that allow me to add elements to it.
And each time I wanted to add an element I wanted that a dialog appears asking me to enter the element; of course after clicking on it.
myItem = new CCombo(shlBusinessProcess, SWT.BORDER);
myItem.addMouseListener(new MouseAdapter() {
public void mouseDoubleClick(MouseEvent e) {
String name = JOptionPane.showInputDialog( "Enter the element's name" );
myitem.add(name);
}
});//this code works

In addition; I want that each time I click on a certain element of myItem, a check box appears, a button (named "OK") appears, and a button ("submit") appears.
Suppose for the first element("moni") of MyItem: the user checks the check box( became true), when he clicks OK, the check box and the OK button disappear.
Suppose for the second element("graz") of MyItem: the user did not check the check box(remains false), when he clicks OK, the check box and the OK button disappear.
my objective is that I want to print, when the submit button is clicked (after entering all elements), each element of MyItem and the boolean property false or true of this element (which I get from the checkbox of this element)

example output: moni true
graz false

Thank you guys, I know that I asked a lot but I am still new and I need some push

Adolf

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Have yuo read the API for CCombo? There are a couple of methods for adding items. The method you've used isn't for adding a new item -- it's for modifying an already existing item, and there isn't an existing item at the index you pass to the method.


luck, db
There are no new questions, but there may be new answers.
maroun Adolf
Greenhorn

Joined: Jun 23, 2011
Posts: 9
Hi burk I corrected my first error still can't figure out the the code for the second one
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Please don't edit and change code in a post that's already been replied to, as that makes the reply meaningless. Also, I had edited your post to include code tags and you have removed them. Please UseCodeTags when posting code here.
maroun Adolf
Greenhorn

Joined: Jun 23, 2011
Posts: 9
You replied while I was editing; thank you for the reply anyway; and regarding the tags I will use them from now on
thank you
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Hi Maroun,

Have you thought about creating Jtable with checkbox and name, where you control the list from ADD and DELETE button on top of Table.

It is just my thought


SCJP 1.4, SCWCD 1.4
maroun Adolf
Greenhorn

Joined: Jun 23, 2011
Posts: 9
Hi Arun,

thank you for the help.
I tried to create a dynamic JTable, but I got frustrated trying;

So I switched to an alternative:
each time a user wants to add data, he double click on "myitem", a new shell appears with a txtelement, a checkbox, and an OK button; the user enters his name, checks the box if he wants, than clicks OK.

It works but I have to print the name and true or false value each time I press OK; which is not my objective

Can you please help me find the way to implement a dynamic JTable; I tried looking in tutorials but each time I try a code my program crashs

Thank you for your help,
Maroun


Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Try the below code.....you have to customize it as per your request.....

import javax.swing.table.DefaultTableModel;

public class SampleTable extends javax.swing.JFrame {

/** Creates new form SampleTable */
public SampleTable() {
initComponents();
}

static int count = 0;

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("ADD");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Delete");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null}
},
new String [] {
"ItemChecked", "ItemName", "Select"
}
) {
Class[] types = new Class [] {
java.lang.Boolean.class, java.lang.String.class, java.lang.Boolean.class
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);

jButton3.setText("Save");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(59, 59, 59)
.addComponent(jButton2)
.addGap(103, 103, 103))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(jButton3)
.addContainerGap(174, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(33, 33, 33)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(jButton3)
.addContainerGap(104, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(42, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(138, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
if(count > 0){
model.removeRow(count);
count --;
}

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.addRow(new Object [][]
{null, null, null});
count++;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SampleTable().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}

maroun Adolf
Greenhorn

Joined: Jun 23, 2011
Posts: 9
Hi,

Thank you , I modified the code a little bit and it works,


Thank you for the great help

regards,
Maroun
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Arun, next time please UseCodeTags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding and manipulating elements of a CCombo