In my table. I want to put some buttons into each row that I can press.
But I do not know how to do it
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
java.sql.ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
For doing this you have to first look at,
Editors and Renderers
How to render a cell in JTable as Button example.
Not only the above stated example, you have many sources which shows how to add a button in a JTable cell.
Related
This is NetBeand GUI code. I need help with that specifically.
How do I get this code to append whole row in witch the asked element is to TextArea:
String trener1 = jTextField9.getText();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int mCol = model.getColumnCount();
int mRow = model.getRowCount();
for(int i = 0; i < mCol; i++){
for(int j = 0; j < mRow; j++){
if(jTable1.getModel().getValueAt(i, j).equals(trener1)){
jTextArea1.append(model.getValueAt(i).toString()+ "\n");
}
}
}
Code should do: I have a list of gym members. Informations about them are in table, their name, age, instructor. When I type the name of the instructor in one TextField, I want all names of members that have that instructor to get appended to TextArea.
Same with their age.
That's it. Solved.
String trener1 = jComboBox5.getSelectedItem().toString();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int Row = model.getRowCount();
for(int i = 0; i < Row; i++){
if(jTable1.getModel().getValueAt(i, 8).equals(trener1)){
jTextArea1.append(model.getValueAt(i, 1).toString());
}
}
Anyone have an idea how to insert data to existing excel file from jtable am using java to develop this program and my scenario is like this, jtable data insert to excel but the excel already have a design and formula. I don't know how to start it so I ask question. thank you for your tip.
Excel uses a complicated formatting for its native .xls files but it also supports other formats. I am using the Tab-Separated Values (TSV), which is commonly used for transferring information from a database program to a spreadsheet.
You can use below code to extract data from Jtable and export it to excel.
public void toExcel(JTable table, File file) {
try {
TableModel model = table.getModel();
FileWriter excel = new FileWriter(file);
for (int i = 0; i < model.getColumnCount(); i++) {
excel.write(model.getColumnName(i) + "\t");
}
excel.write("\n");
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
excel.write(model.getValueAt(i, j).toString() + "\t");
}
excel.write("\n");
}
excel.close();
} catch (IOException e) {
System.out.println(e);
}
}
First you need to extract data from JTable. You this method:
public Object[][] getTableData (JTable table) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0 ; i < nRow ; i++)
for (int j = 0 ; j < nCol ; j++)
tableData[i][j] = dtm.getValueAt(i,j);
return tableData;
}
Now having data you can append them into excel file using apache poi. Here are tutorials http://poi.apache.org/spreadsheet/quick-guide.html
I have designed a Jtable using Netbeans GUI in Swing.Now as per my requirement i have to display database table values into jtable.
Here is my code ..
Statement statement = (Statement) con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = (ResultSetMetaData) resultSet.getMetaData();
DefaultTableModel dtm = new DefaultTableModel();
int columns = metaData.getColumnCount();
Vector Column_Name = new Vector();
Vector data_rows = new Vector();
for (int i = 1; i < columns; i++) {
Column_Name.addElement(metaData.getColumnName(i));
}
dtm.setColumnIdentifiers(Column_Name);
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
}
dtm.addRow(data_rows);
jTable1.setModel(dtm);
resultSet.close();
Why is this code displaying only one row of my database into jtable when there so many rows in the table.
Please help me to solve this.
Each time you overwrite you data, because you have added the row just one time, which is currently after the while loop
So try to move the adding of row data in the loop like:
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
dtm.addRow(data_rows);
}
In my application I got 2 tables, table A contains optional columns for table B. It should be possible to drag "columns" (tableItem of table A) and drop them into table B. Table B should use the dragged tableItems as new columns. This works fine. Table B appends them.
Now table B should add the columns in the right order. org.eclipse.swt.dnd.DropTargetEvent knows its position (DropTargetEvent.x / y). So I have to figure out the column / columnIndex at the drop-position, so I could add the "new columns" next to column.atPoint(x,y). org.eclipse.swt.widgets.Table itself just got a method called getColumn(int index). Is there any way to figure this out?
Here is some code that will print out the column for a mouse click event. You can modify it to use the location of your drop instead of the mouse click:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Stackoverflow");
shell.setLayout(new RowLayout(SWT.VERTICAL));
Table table = new Table(shell, SWT.BORDER);
table.setHeaderVisible(true);
for(int col = 0; col < 3; col++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Col: " + col);
}
for(int row = 0; row < 20; row++)
{
TableItem item = new TableItem(table, SWT.NONE);
for(int col = 0; col < table.getColumnCount(); col++)
{
item.setText(col, row + " " + col);
}
}
for(int col = 0; col < table.getColumnCount(); col++)
{
table.getColumn(col).pack();
}
table.addListener(SWT.MouseDown, new Listener()
{
#Override
public void handleEvent(Event e)
{
Table table = (Table) e.widget;
System.out.println("Column: " + getColumn(table, e.x));
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static int getColumn(Table table, int x)
{
int overallWidth = 0;
for(int i = 0; i < table.getColumnCount(); i++)
{
overallWidth += table.getColumn(i).getWidth();
if(x < overallWidth)
{
return i;
}
}
return -1;
}
I would like to get the value from the Jtable, and I tried it using the getvalueat however whenever I try to get the value from the JTable it only get the value from the first column of the selected row, I need to get all the value from the Jtable which I selected. Can you please help me with this one
here is my code:
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
Object data = (Object)table.getValueAt(row, col);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is my action event where the value of the selected table is shown in the JOptionPane unfortunately it only display one value(which is the one you already selected) not the whole row.
This code is for my Jbutton for call the action event(I already excluded my code from the JTable since it fetch the Jtable value from my database)
ActionListener tableAction = new GetTableValue();
buttonEdit = new JButton("EDIT");
buttonEdit.addActionListener(tableAction);
the code is plain and simple, I also search Mr. G(google) about a good tutorial on fetching row, unfortunately there isn't a good tutorial for fetching Jtable value(per row).
If you want all the values from selected row then try this code.
int row = jTable1.getSelectedRow();
int column = jTable1.getColumnCount();
for(int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(row, i));
}
You get the all values for selected row, no matter how much columns are there in jtable
If you want all the values from jtable then try:
int row = jTable1.getRowCount();
int column = jTable1.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
System.out.println(jTable1.getValueAt(j, i));
}
}
Yes you can use Object[] to store the values. For example:
Object[] val = new Object[column];
for (int k = 0; k < val.length - 1; k++) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
val[k] = jTable1.getValueAt(j, i);
System.out.println(val[k]);
}
}
}
getValueAt will return you the value of the cell (at row/col). Unless you're table model supports it, there is no convenient way (beyond what you are doing) to get the whole row in a single request.
Also, remember, if the table is sorted or filtered, the model indices will not match the view, you need to convert them first, using convertRowIndexToModel and convertColumnIndexToModel
UPDATE
The only way around it is if the table model you're using has a getRow (or equivalent) method. Without know how you are storing the data in the table model it's next to near impossible to give an accurate answer, but a general idea would be...
public class MyAwesomeTableModel extends AbstractTableModel {
// All the usual stuff...
public MyRowData getRowAt(int index) { ... }
}
Now, MyRowData is what ever implementation of the table data you've created. It could be (preferably) a single Object or in the case of the DefaultTableModel an array of objects.
class GetTableValue implements ActionListener{
public void actionPerformed(ActionEvent e){
AbstractButton button = (AbstractButton)e.getSource();
if(e.getActionCommand().equals(button.getActionCommand)){
int row = table.convertRowIndexToModel(table.getSelectedRow());
MyAwesomeTableModel model = (MyAwesomeTableModel)table.getModel();
MyRowData data = model.getRowAt(row);
JOptionPane.showMessageDialog(null, data);
}
}
}
This is all dependent on how you've implemented your TableModel and how you've implemented your row data, but that's the general jist
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int selectedRow;
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener()
{
#Override
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
int numCols = jTable1.getColumnCount();
model = (DefaultTableModel) jTable1.getModel();
System.out.print(" \n row " + selectedRow + ":");
for (int j = 0; j < numCols; j++)
{
System.out.print(" " + model.getValueAt(selectedRow, j));
}
}
});
}
Using this you can get value of whole row where u click on particular row.
you can try the below code to get selected row value:
int selectedRow = jTableName.getSelectedRow();
selectedRow = jTableName.convertRowIndexToModel(selectedRow);
String val1 = (String)jTableName.getModel().getValueAt(selectedRow, 0);
String val2 = (String)jTableName.getModel().getValueAt(selectedRow, 1);