I want to save Name and value of jtable into two variable
public class NewClass extends JPanel implements TableModelListener {
private final String[] columnNames = { "Name", "Value","check"};
private JTable table;
private DefaultTableModel tableModel;
private final JButton buttonSave;
public NewClass(){
tableModel = new DefaultTableModel(columnNames, 0);
tableModel.addTableModelListener(this);
table = new JTable(tableModel);
javax.swing.table.TableColumn var_col;
var_col = table.getColumnModel().getColumn(2);
final JCheckBox check = new JCheckBox();
var_col.setCellEditor(new DefaultCellEditor(check));
var_col.setCellRenderer(new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int
column) {
check.setSelected(((Boolean)value).booleanValue()) ;
return check;
}
});
JScrollPane scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(BorderLayout.NORTH, new JLabel("Mon panier", JLabel.CENTER));
add(BorderLayout.CENTER, scrollPane);
//--------I want to save these Name and value in two variables -----------
Object[] data1 = {
new String("work"), new String("done"),new Boolean(false)};
tableModel.addRow(data1);
buttonSave = new JButton("Save");
buttonSave.setEnabled(false);
buttonSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonSave.setEnabled(false);
}
});
As discussed in How to Use Tables, your table's data should be managed by a TableModel such as AbstractTableModel or the concrete DefaultTableModel used in your example. In this example, DataModel extends AbstractTableModel and synthesizes a List<Value> of test data; yours would listen to whatever object monitors the serial port. The example also uses the class Value to encapsulate a selectable numeric value. The custom TableCellEditor updates each Value as it is changed, so the DataModel always contains the selection state of each element in the list. Your save button could then save the list elements in whatever format you prefer.
Related
I'm creating a JTable in Java & I'm asked to add to the table a JCheckBox, JButton and a JComboBox. The table that I created is displaying all the information, the button is working fine and the JCheckBox is also working, the problem I'm facing is that the JComboBox is not working. I really can't figure out why. I've tried to look the problem up but I can't figure it out. Can someone help me please?
The code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class sinX extends JFrame {
private JTable table;
private DefaultTableModel model;
private Object[][] data;
private String[] columnNames;
private JButton button;
JComboBox comboBox;
public sinX() {
comboBox = new JComboBox();
setTitle("Programming Languages");
data = new Object[][]{{"C","Dennis Ritchie",1972,false},{"C++","Bjarne Stroustrup",1983,true},
{"Python","Guido van Rossum",1991,false},{"Java","James Gosling",1995,true},{
"JavaScript","Brendan Eich",1995,true},{"C#","Anders Hejlsberg",2001,false},
{"Scala","Martin Odersky",2003,true}};
columnNames = new String[] {"Language","Author","Year","Check Box"};
//model = new DefaultTableModel(data, columnNames);
//table = new JTable(model);
//table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
final Class[] columnClass = new Class[] {
String.class, String.class, Integer.class, Boolean.class
};
//create table model with data
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
#Override
public boolean isCellEditable(int row, int column)
{
return false;
}
#Override
public Class<?> getColumnClass(int columnIndex)
{
return columnClass[columnIndex];
}
};
table = new JTable(model);
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
button = new JButton("Remove");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
// check for selected row first
if(table.getSelectedRow() != -1) {
// remove selected row from the model
model.removeRow(table.getSelectedRow());
JOptionPane.showMessageDialog(null, "Selected row deleted successfully");
}
}
});
TableColumn year = table.getColumnModel().getColumn(2);
comboBox.addItem("A");
comboBox.addItem("B");
comboBox.addItem("C");
comboBox.addItem("D");
year.setCellEditor(new DefaultCellEditor(comboBox));
add(new JScrollPane(table), BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new sinX();
}
}
the problem I'm facing is that the JComboBox is not working.
year.setCellEditor(new DefaultCellEditor(comboBox));
The above code indicates you are trying to use a combo box as an editor for the given column.
public boolean isCellEditable(int row, int column)
{
return false;
}
However, you have stated in your model that none of columns are editable.
You need to return true for any column where you want to edit the data.
the JCheckBox is also working
Not really. Yes you see the boolean value is rendered as a check box. However, you can't change its value by clicking on it, unless of course you return true for that column as well.
I am trying to make JTable cells non-editable but if i do so Iam unable to select a single cell value instead when i try to copy the entire row is getting selected
I want to copy only the selected cell value instead of entire row.Is there a way to do it?
public class EmployeeWin extends JFrame {
DefaultTableModel model = new DefaultTableModel() {
#Override
public boolean isCellEditable(int row, int column){
return false;
}
};
Container cont = this.getContentPane();
JTable tab = new JTable(model);
private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(model);
private final JTextField searchFilter = new JTextField();
public EmpDataWin(List<EmployeeDTO> pEmployeeDTO) {
initialize(pEmployeeDTO);
}
public void initialize(List<EmployeeDTO> pEmployeeDTOList) {
JPanel panelParent = new JPanel(new BorderLayout());
// Add Header
model.addColumn("Employee Name");
model.addColumn("Department");
model.addColumn("Details");
// Add data row to table
for (EmployeeDTO aEmployeeDTO : pEmployeeDTOList) {
model.addRow(new Object[] { aEmployeeDTO.getEmployee_Name(), aEmployeeDTO.getDepartment(),
aEmployeeDTO.getDetails()});
}
tab.setRowSorter(rowSorter);
tab.setAutoCreateRowSorter(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(UIConstants.SEARCH), BorderLayout.WEST);
JTextField searchFilter = SearchFilter.createRowFilter(tab);
panel.add(searchFilter, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane sp = new JScrollPane(tab,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panelParent.add(panel,BorderLayout.NORTH);
panelParent.add(sp,BorderLayout.CENTER);
panelParent.setBorder(BorderFactory.createEmptyBorder(10 , 10, 10, 10));
cont.add(panelParent);
this.pack();
}
public static void main(String[] args) {
EmployeeDAO dao = new EmployeeDAO();
List<EmployeeDTO> dto = dao.getemployeeData();
JFrame frame = new EmployeeDataWin(dto);
}
}
when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row
The default Action for the Ctrl+C key is to copy the entire row. If you only want the data of the currently selected cell then you need to replace the default Action with a custom Action.
The logic would be something like:
Action copyCell = new AbstractAction()
{
#Override
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
Object value = table.getValueAt(row, column);
// copy the data to the clipboard
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData = new StringSelection( value.toString() );
c.setContents(testData, testData);
}
};
table.getActionMap().put("copy", copyCell);
The above code will create the custom Action and replace it in the ActionMap of the JTable. See Key Bindings. The program provided in the link shows all the default Actions and the keyword for each Action.
I got a simple JTable which one of it's cells is an Edit button which needs to open a new JDialog.
After a several examples from the web I created a new class which implements TableCellRenderer and returned a JButton from it but the button is not doing what I need for some reason.
This is my code:
final MessageResourcesTableModel model = new MessageResourcesTableModel(columnNames);
JTable resultsTable = new JTable(model);
resultsTable.setShowGrid(true);
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTable.setPreferredScrollableViewportSize(resultsTable.getPreferredSize());
resultsTable.setFillsViewportHeight(true);
resultsTable.setFont(font13);
final TableCellRenderer buttonRenderer = new JTableButtonRenderer();
resultsTable.getColumn(COLUMN_EDIT).setCellRenderer(buttonRenderer);
And this is the renderer:
class JTableButtonRenderer implements TableCellRenderer {
#Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
final JButton editButton = new JButton("Edit");
editButton.setOpaque(true);
editButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
System.out.println("Hi from Action !!!");
}
});
return editButton;
}
}
Although there is a simple ActionListener implementation, there is nothing over the console
A renderer only displays the data in the cell. You can't add an ActionListener to the button because it is not a real component. A renderer only paints an image of the button in the table.
You need to create a custom editor if you want to be able to click on the button.
See Table Button Column for an editor implementation that does this for you.
In this code block I'm getting old values after clicking inside scroll pane, however new values also disappear at first on fetching but they slowly disappear and old values appear in that place.
I tried a few combinations but was't able to achieve the results as expected. Also during variable declaration I made all the variables as non-static except the frame which I suppose is not relevant and recommended and I'm also embedding the components on frame itself.
//THE TABLE
table = new JTable();
//THE COLUMN
String[] columns = new String[] {
"Serial No", "BundleId", "Bundle Count", "Record Count","Status","Date"
};
//THE ROW
Object[][] data = dc.Success(date,afterDate);
final Class[] columnClass = new Class[] {
Boolean.class,String.class, String.class, String.class,String.class,String.class };
//create table model with data
DefaultTableModel model = new DefaultTableModel(data, columns) {
#Override
public boolean isCellEditable(int row, int column)
{
return false;
}
#Override
public Class<?> getColumnClass(int columnIndex)
{
return columnClass[columnIndex];
}
public Object getCellEditorValue() {
return "";
}
};
// JTable table = new JTable(model);
table.setModel(model);
//add the table to the frame
//ADD SCROLLPANE
scroll.setBounds(70, 80, 600, 400);
scroll.setViewportView(table);
frame.getContentPane().add(scroll);
// this.add(new JScrollPane(table));
// table.setAutoCreateRowSorter(true);
table.setPreferredScrollableViewportSize(new Dimension(320, 160));
TableColumn tc = table.getColumnModel().getColumn(BOOLEAN_COL);
tc.setHeaderRenderer(new SelectAllHeader(table, BOOLEAN_COL));
this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
table.repaint();
If you want to notify your JTable about changes of your data, use something similar to tableModel.fireTableDataChanged().
There are other methods that can be used for updating specific ranges of data. tableModel.fireTableRowsUpdated​(firstRow, lastRow) might be of particular use to you. This is because you suggest you only update a single row when that row is clicked on. If that is not the case, stick to tableModel.fireTableDataChanged().
From your comments, I notice you are trying to fire a data change event and then changing your whole model anyway. This is not required and could actually be the source of your issues if your model is holding the old values.
Instead you should change the DataVector of the current model. To change the data on a single row you could use something like the following extract of code. There is a full example below the extract.
//Get your row's values from your database
//I'll use random values for this purpose
int col1Val = getRand(0, 9);
int col2Val = getRand(0, 9);
//Put the values in an object vector
Vector<Object> rowVals = new Vector<>();
rowVals.addElement(col1Val);
rowVals.addElement(col2Val);
//Set the object vector
model.getDataVector().setElementAt(rowVals, table.getSelectedRow());
//Notify the table the row has changed
model.fireTableRowsUpdated(table.getSelectedRow(), table.getSelectedRow());
Full example
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableExample extends JFrame {
private static final String windowName = "JTable Example";
private static final long serialVersionUID = 362702020844358278L;
private JPanel tablePanel;
private JScrollPane scrollPanel;
private JTable table;
private DefaultTableModel model;
private JTableExample() {
super(windowName);
SetUp();
}
private void SetUp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SetUpJTable();
SetUpJPanel();
add(tablePanel);
pack();
setVisible(true);
}
private void SetUpJPanel() {
tablePanel = new JPanel(new GridLayout());
tablePanel.add(scrollPanel);
}
private void SetUpJTable() {
String[] columns = new String[] {
"Something", "Something Else"
};
String[][] data = new String[][] {
{"1", "2"},
{"3", "3"}
};
model = new DefaultTableModel(data, columns) {
private static final long serialVersionUID = -3895234084030399437L;
#Override
public boolean isCellEditable(int row, int column)
{
return false;
}
};
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//Get your row's values from your database
//I'll use random values for this purpose
int col1Val = getRand(0, 9);
int col2Val = getRand(0, 9);
//Put the values in an object vector
Vector<Object> rowVals = new Vector<>();
rowVals.addElement(col1Val);
rowVals.addElement(col2Val);
//Set the object vector
model.getDataVector().setElementAt(rowVals, table.getSelectedRow());
//Notify the table the row has changed
model.fireTableRowsUpdated(table.getSelectedRow(), table.getSelectedRow());
}
});
scrollPanel = new JScrollPane(table);
scrollPanel.setPreferredSize(new Dimension(500, 500));
}
private static int getRand(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new JTableExample());
}
}
I have a code which displays Table in applets & consists of two columns:-
image icon
description
Here's my code:
import javax.swing.table.*;
public class TableIcon extends JFrame
{
public TableIcon()
{
ImageIcon aboutIcon = new ImageIcon("about16.gif");
ImageIcon addIcon = new ImageIcon("add16.gif");
ImageIcon copyIcon = new ImageIcon("copy16.gif");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon, "About"},
{addIcon, "Add"},
{copyIcon, "Copy"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
Now what i want to know is how can I implement selection listener or mouse listener event on my table , such that it should select a particular image from my table and display on text area or text field(my table contains path of image file)?
Can I add text field on table & table on frame? Please feel free to ask queries if required.
In my code I have a table where I set single selection mode; in my case, listener described in How to Write a List Selection Listener (with a for loop from getMinSelectionIndex to getMaxSelectionIndex) is not useful because releasing mouse button I'm sure I have just one row selected.
So I've solved as follows:
....
int iSelectedIndex =-1;
....
JTable jtable = new JTable(tableModel); // tableModel defined elsewhere
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel selectionModel = jtable.getSelectionModel();
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
handleSelectionEvent(e);
}
});
....
protected void handleSelectionEvent(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
// e.getSource() returns an object like this
// javax.swing.DefaultListSelectionModel 1052752867 ={11}
// where 11 is the index of selected element when mouse button is released
String strSource= e.getSource().toString();
int start = strSource.indexOf("{")+1,
stop = strSource.length()-1;
iSelectedIndex = Integer.parseInt(strSource.substring(start, stop));
}
I think this solution, that does not require a for loop between start and stop to check which element is selectes, is more suitable when table is in single selection mode
How about this?
protected void handleSelectionEvent(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource();
iSelectedIndex = target.getAnchorSelectionIndex();
}
Read the section from the Swing tutorial on How to Write a List Selection Listener.
You can't add a text field to the table, but you can add a textfield and a table to the same frame.