JTable search and deleting rows - java

I am learning how to use JTable and I have issues with both my search and delete function.
From my codes, if I checked row 1 and row 3 and press the delete button, it will remove perfectly.
If I enter J in my search textfield and press the search button, John and Jane will be displayed
and now if I check the 2 rows and press the delete button then press the search button again,
John and Jane will be removed including Kate and Ann.
Please run the codes below if you guys still don't understand what I am saying.
follow the steps that I mentioned and you will see what's wrong.
help me. Thanks
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TableExample extends JFrame {
private static final long serialVersionUID = 1L;
protected static final boolean DEBUG = false;
private JTable table;
TableModel model;
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Delete");
JTextField searchTextField = new JTextField(15);
JButton searchBtn = new JButton("Search");
JPanel flowLayoutPanel = new JPanel(new FlowLayout());
TableRowSorter<TableModel> sorter;
public TableExample() {
String[] columnNames = {"Employer", "Company", "Salary", "Boolean"};
Object[][] data = {
{"Kate", "20",new Integer(5000), new Boolean(false)},
{"John", "35", new Integer(3000), new Boolean(false)},
{"Ann", "20", new Integer(4000), new Boolean(false)},
{"Jane", "12", new Integer(4000), new Boolean(false)},
{"May", "42", new Integer(4500), new Boolean(false)}
};
model = new DefaultTableModel(data, columnNames) {
#Override
public Class getColumnClass(int column) {
switch(column) {
case 0:
case 1: return String.class;
case 2: return Integer.class;
case 3: return Boolean.class;
default: return Object.class;
}
}
};
table = new JTable(model) {
public boolean isCellEditable(int row, int col) {
return true;
}
};
sorter = new TableRowSorter<TableModel>(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setRowSorter(sorter);
table.setGridColor(Color.black);
JScrollPane scrollPane = new JScrollPane(table);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
((DefaultTableModel) model).removeRow(row);
row--;
}
}
}
});
searchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = searchTextField.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
flowLayoutPanel.add(searchTextField);
flowLayoutPanel.add(searchBtn);
panel.add(flowLayoutPanel,BorderLayout.NORTH);
panel.add(scrollPane , BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
getContentPane().add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TableExample frame = new TableExample();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}

Since the row number changes when you filter the table. Use original row number to delete a correct row.
Use table.convertRowIndexToModel(row) to get the actual row number.
Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
sample code:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
((DefaultTableModel) model).removeRow(table.convertRowIndexToModel(row));
row--;
}
}
}
});
Read more...

Related

Make cell editable with JButton

I'm stuck for two days on a piece of code, the cells of my table are non-editable default and I want to editable when I click on the edit button, but I can not run this code.
Thank you in advance for your help.
table = new JTable(model) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sorter = new TableRowSorter < TableModel > (model);
table.setPreferredScrollableViewportSize(new Dimension(560, 200));
JScrollPane scrollpane = new JScrollPane((table));
table.setRowSorter(sorter);
add(ScrollPane);
scrollPane.setBounds(10, 180, 560, 200);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
modifier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == change) {
boolean isCellEditable(int row, int column) {
return true;
}
}
}
});
Put enabled cell (row, col) to a set and make your table use that set to check if cell is enabled.
package javaapplication33;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.JTextField;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
import java.awt.EventQueue;
import javax.swing.Box;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.table.AbstractTableModel;
public class JavaApplication33
{
public static void main(String[] args)
{
Runnable r = new Runnable()
{
#Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.setPreferredSize(new Dimension(300,300));
final Set<Point> erc = new HashSet<>(); //this set contains (row,cell) pairs of editable cells
//put some data to the table
final JTable table = new JTable(new String[][]
{{"a", "aa"},
{"b", "bb"},
{"c", "cc"}},
new String[]{"col1", "col2"}) //name columns
{
Collection<Point> editableRowsCells = erc;
Collection<Point> getEditableRowsCells()
{
return editableRowsCells;
}
public boolean isCellEditable(int row, int column)
{
return getEditableRowsCells()
.contains(new Point(row, column));
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollpane = new JScrollPane(table);
p.add(scrollpane, BorderLayout.CENTER);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
final JTextField xf = new JTextField();
final JTextField yf = new JTextField();
Action enableAction = new AbstractAction("Enable")
{
#Override
public void actionPerformed(ActionEvent e)
{
int row = Integer.valueOf(xf.getText());
int col = Integer.valueOf(yf.getText());
erc.add(new Point(row,col));
((AbstractTableModel) table.getModel())
.fireTableCellUpdated(row, col);
}
};
JButton enableButton = new JButton();
enableButton.setAction(enableAction);
Box b = new Box(BoxLayout.X_AXIS);
b.add(new JLabel("Row"));
b.add(xf);
b.add(new JLabel("Col"));
b.add(yf);
b.add(enableButton);
p.add(b, BorderLayout.NORTH);
f.add(p);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
This is the output:
Update: to make all cells editable
package javaapplication33;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.Action;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;
import java.awt.Color;
import javax.swing.table.AbstractTableModel;
public class JavaApplication33
{
public static class Toggle
{
Boolean val;
public Toggle(Boolean val)
{
this.val = val;
}
public void setVal(Boolean val)
{
this.val = val;
}
public Boolean getVal()
{
return val;
}
public void toggle()
{
setVal(!getVal());
}
}
public static void main(String[] args)
{
Runnable r = new Runnable()
{
#Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.setPreferredSize(new Dimension(300, 300));
final Toggle editable = new Toggle(false);
final JTable table = new JTable(
new String[][]{
{"a", "aa"},
{"b", "bb"},
{"c", "cc"}},
new String[] {"col1", "col2"}) //name columns
{
public boolean isCellEditable(int row, int column)
{
return editable.getVal(); //request current value
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollpane = new JScrollPane(table);
p.add(scrollpane, BorderLayout.CENTER);
table.setRowHeight(28);
table.setBackground(Color.DARK_GRAY);
table.setForeground(Color.WHITE);
Action enableAction = new AbstractAction("Toggle editable")
{
#Override
public void actionPerformed(ActionEvent e)
{
editable.toggle(); //toggle current value
((AbstractTableModel) table.getModel()).fireTableStructureChanged();
}
};
JButton enableButton = new JButton();
enableButton.setAction(enableAction);
p.add(enableButton, BorderLayout.NORTH);
f.add(p);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
Output:
Here my code :
table = new JTable(model){
public boolean isCellEditable(int row, int column) {
return false;
}
};
modifier.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ((JButton)e.getSource()== modifier)
{
public boolean isCellEditable(int row, int column) {
return true;
}
}
}
});

Sorting Table is wrong when the sort button be pressed more than once?

I have a problem here. I create program to add data to the table and sort it when i press the button. when i press the sort button once, it isn't wrong. but when i press again it is wrong. so why? please help me.
this is the code.
Nama Class
public class Nama {
private String nama;
private int index;
public Nama(String n,int i){
nama=n;index=i;
}
void setData(String n,int i){
nama=n;index=i;
}
String nama(){
return nama;
}
int ind(){
return index;
}
public String toString(){
return(String.format("%s %d", nama,index));
}
}
MergeSort Class
import java.util.*;
public class MergeSortS{
public void merge_sort(int low,int high,Nama [] a){
int mid;
if(low<high) {
mid=(low+high)/2;
merge_sort(low,mid,a);
merge_sort(mid+1,high, a);
merge(low,mid,high,a);
}
}
public void merge(int low,int mid,int high,Nama [] a){
int h,i,j,k;
Nama b[]=new Nama[50];
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high)){
if(a[h].nama().compareToIgnoreCase(a[j].nama())<0){
b[i]=a[h];
h++;
}
else{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid){
for(k=j;k<=high;k++){
b[i]=a[k];
i++;
}
}
else{
for(k=h;k<=mid;k++){
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
public MergeSortS() {
}
}
Panel Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
*
* #author Kareem
*/
public class Panel extends JPanel implements ActionListener{
JTable table;
JTextField tf1,tf2,tf3,tf4;
JButton b1,b2,b3,b4,b5,b6,b7;
Vector rows,columns,temp;
DefaultTableModel tabModel;
String[]data = new String[3];
String [] column = {"Nama", "Nim", "IP"};
Nama[] n,nim,ip;
MergeSortS mS;
int c=0,index=0;
public Panel(){
this.setBounds(0,0,1280, 800);
this.setLayout(null);
inaTf();
inaTab();
inaBut();
n= new Nama[50];
nim= new Nama[50];
ip= new Nama[50];
mS= new MergeSortS();
this.setVisible(true);
}
public void inaTab(){
rows=new Vector();
columns= new Vector();
temp= new Vector();
addColumns(column);
tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);
table = new JTable(tabModel);
table.setPreferredScrollableViewportSize(new
Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setBounds(100,100,200,200);
JScrollPane scroll = new JScrollPane(table);
scroll.setBounds(50,50,400,400);
add(scroll);
}
public void inaBut(){
b1=new JButton("add Row");
b1.setBounds(50,600,90,25);
add(b1);
b1.addActionListener(this);
b2=new JButton("Delete Row");
b2.setBounds(170,600,90,25);
add(b2);
b2.addActionListener(this);
b3=new JButton("SortName");
b3.setBounds(290,600,120,25);
add(b3);
b3.addActionListener(this);
b5=new JButton("SortNim");
b5.setBounds(290,650,120,25);
add(b5);
b5.addActionListener(this);
b6=new JButton("SortIP");
b6.setBounds(290,700,120,25);
add(b6);
b6.addActionListener(this);
b4=new JButton("RESET");
b4.setBounds(170,650,90,25);
add(b4);
b4.addActionListener(this);
}
public void inaTf(){
tf1=new JTextField();
tf1.setBounds(640,50,90,25);
add(tf1);
JLabel l1= new JLabel("Nama \t: ");
l1.setBounds(530,50,90,25);
add(l1);
tf2=new JTextField();
tf2.setBounds(640,80,90,25);
add(tf2);
JLabel l2= new JLabel("Nim : ");
l2.setBounds(530,80,90,25);
add(l2);
tf3=new JTextField();
tf3.setBounds(640,110,90,25);
add(tf3);
JLabel l3= new JLabel("IPK : ");
l3.setBounds(530,110,90,25);
add(l3);
tf4=new JTextField();
tf4.setBounds(640,140,90,25);
add(tf4);
JLabel l4= new JLabel("Hapus Baris ke ");
l4.setBounds(530,140,120,25);
add(l4);
}
public void addRow()
{
Vector r;
r = createBlankElement();
rows.addElement(r);
table.addNotify();
}
public void addRow(String [] data)
{
Vector r=new Vector();
r = isi(data);
rows.addElement(r);
table.addNotify();
}
public Vector createBlankElement()
{
Vector t = new Vector();
t.addElement((String) " ");
t.addElement((String) " ");
t.addElement((String) " ");
return t;
}
public Vector isi(String[] data) {
Vector t = new Vector();
for(int j=0;j<3;j++){
t.addElement((String) data[j]);
}
return t;
}
public void addColumns(String[] colName) {
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}
void deleteRow(int index) {
if(index!=-1) {
rows.removeElementAt(index);
table.addNotify();
}
}
#Override
public void actionPerformed(ActionEvent e) {
try{
if(e.getSource()==b1){
data[0]=tf1.getText()+" "+index;
n[index]=new Nama(data[0],index);
data[1]=tf2.getText();
nim[index]=new Nama(data[1],index);
data[2]=tf3.getText()+rows.size();
ip[index]=new Nama (data[2],index);
c=c+1;
index=index+1;
addRow(data);
}
if(e.getSource()==b2){
int i;
i=Integer.parseInt(tf4.getText());
deleteRow(i);
// for(;i<rows.size();i++){
// n[i]=n[i+1];
// }
}
if(e.getSource()==b3){
mS.merge_sort(0, rows.size()-1, n);
temp.setSize(rows.size());
for(int i=0;i<index;i++){
temp.set(i, rows.get(n[i].ind()));
}
for(int i=0;i<index;i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b4){
rows.setSize(0);
temp.setSize(0);
for(int i=0;i<index;i++)n[i]=null;
index=0;
}
if(e.getSource()==b5){
mS.merge_sort(0, rows.size()-1, nim);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.set(i, rows.get(nim[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
if(e.getSource()==b6){
mS.merge_sort(0, rows.size()-1, ip);
temp.setSize(rows.size());
for(int i=0;i<rows.size();i++){
temp.add(i, rows.get(ip[i].ind()));
}
for(int i=0;i<rows.size();i++){
rows.set(i, temp.get(i));
}
}
repaint();
}
catch (Throwable t){
JOptionPane.showMessageDialog(null, "AAAAAA");
}
}
}
Frame Class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
*
* #author Kareem
*/
public class Frame extends JFrame {
public Frame(){
super("Penghitung Gaji");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(1280, 800);
this.getContentPane().add(new Panel());
this.setVisible(true);
}
public static void main(String[] args) {
Frame frame = new Frame();
}
}
Thanks. And Sorry for my bad english.
No where do you actually tell the TableModel that it's contents have changed...
Now, the thing that weirds me out, is you are keeping a Vector of the rows outside of the model...
mS.merge_sort(0, rows.size()-1, n);
temp.setSize(rows.size());
for(int i=0;i<index;i++){
temp.set(i, rows.get(n[i].ind()));
}
for(int i=0;i<index;i++){
rows.set(i, temp.get(i)); // This is doing nothing...
}
Once the data has been handed to the table model, you should not be interacting with it, unless you are willing to notify the table model of the changes.
Personally, I would simply use the inbuilt sorting capabilities of the JTable
ps- I would also, strongly, encourage you to learn how to use layout managers, they will save your sanity in the long run
Dashing through the snow
In a one-horse open sleigh
O'er the fields we go
Laughing all the way
Bells on bobtail ring'
Making spirits bright
What fun it is to ride and sing
A sleighing song tonight!
Jingle bells, jingle bells,
Jingle all the way.
Oh! what fun it is to ride
In a one-horse open sleigh.
Jingle bells, jingle bells, ....
song singing from code, please to apologize me if isn't this one your favorite song
can't comment or suggesting, excluding used LayoutManager in my code, I walked the path of least resistance, my endless lazyness
.
.
.
from code
.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter.SortKey;
import javax.swing.ScrollPaneConstants;
import javax.swing.SortOrder;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class MyFrame {
private JFrame frame = new JFrame();
private JTable table;
private JPanel buttonPanel = new JPanel();
private JPanel buttonPanelSouth = new JPanel();
private JPanel textFieldPanel = new JPanel();
private JPanel northPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JLabel l1, l2, l3, l4;
private JTextField tf1, tf2, tf3, tf4;
private JButton b1, b2, b3, b4, b5, b6, b7;
private String[] columnNames = {"Nama", "Nim", "IP", "Hapus Baris ke"};
private Object[][] data = {
{"igor", "B01_125-358", "1.124.01.125", true},
{"lenka", "B21_002-242", "21.124.01.002", true},
{"peter", "B99_001-358", "99.124.01.001", false},
{"zuza", "B12_100-242", "12.124.01.100", true},
{"jozo", "BUS_011-358", "99.124.01.011", false},
{"nora", "B09_154-358", "9.124.01.154", false},
{"xantipa", "B01_001-358", "1.124.01.001", false},};
private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
switch (column) {
case 3:
return true;
default:
return false;
}
}
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public MyFrame() {
table = new JTable(model);
table.setAutoCreateRowSorter(true);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
table.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
DefaultTableCellRenderer stringRenderer =
(DefaultTableCellRenderer) table.getDefaultRenderer(String.class);
stringRenderer.setHorizontalAlignment(SwingConstants.CENTER);
JScrollPane pane = new JScrollPane(table,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
centerPanel.setLayout(new BorderLayout(10, 10));
centerPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
centerPanel.add(pane);
centerPanel.add(pane);
//
b1 = new JButton("add Row");
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.addRow(new Object[]{(tf1.getText()).trim(),
(tf2.getText()).trim(), (tf3.getText()).trim(), true});
}
});
b2 = new JButton("Delete Row");
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int rowToDelete = 0;
int rowToModel = 0;
if (table.getSelectedRow() > -1) {
rowToDelete = table.getSelectedRow();
rowToModel = table.convertRowIndexToModel(rowToDelete);
model.removeRow(rowToModel);
}
}
});
b3 = new JButton("RESET");
b3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
table.getRowSorter().setSortKeys(null);
}
});
b4 = new JButton("SortName");
b4.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
table.getRowSorter().toggleSortOrder(0);
}
});
b5 = new JButton("SortNim");
b5.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
List<SortKey> sortKeys = new ArrayList<SortKey>();
SortKey sortKey = new SortKey(1, SortOrder.ASCENDING);
sortKeys.add(sortKey);
rowSorter.setSortKeys(sortKeys);
rowSorter.sort();
}
});
b6 = new JButton("SortIP");
b6.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
List<SortKey> sortKeys = new ArrayList<SortKey>();
SortKey sortKey = new SortKey(2, SortOrder.DESCENDING);
sortKeys.add(sortKey);
SortKey sortKey1 = new SortKey(1, SortOrder.ASCENDING);
sortKeys.add(sortKey1);
SortKey sortKey2 = new SortKey(0, SortOrder.UNSORTED);
sortKeys.add(sortKey2);
rowSorter.setSortKeys(sortKeys);
rowSorter.sort();
}
});
b7 = new JButton("SortIP");
buttonPanel.setLayout(new GridLayout(1, 0, 50, 0));
buttonPanel.setBorder(new EmptyBorder(2, 10, 2, 10));
buttonPanel.add(b1);
buttonPanel.add(b2);
//
buttonPanelSouth.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanelSouth.add(b3);
buttonPanelSouth.add(b7);
b7.setVisible(false);
buttonPanelSouth.add(b4);
buttonPanelSouth.add(b5);
buttonPanelSouth.add(b6);
centerPanel.add(buttonPanelSouth, BorderLayout.SOUTH);
//
l1 = new JLabel("Nama : ", JLabel.RIGHT);
tf1 = new JTextField();
l2 = new JLabel("Nim : ", JLabel.RIGHT);
tf2 = new JTextField();
l3 = new JLabel("IPK : ", JLabel.RIGHT);
tf3 = new JTextField();
l4 = new JLabel("Hapus Baris ke :", JLabel.RIGHT);
tf4 = new JTextField();
textFieldPanel.setLayout(new GridLayout(4, 2, 10, 10));
textFieldPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
textFieldPanel.add(l1);
textFieldPanel.add(tf1);
textFieldPanel.add(l2);
textFieldPanel.add(tf2);
textFieldPanel.add(l3);
textFieldPanel.add(tf3);
textFieldPanel.add(l4);
textFieldPanel.add(tf4);
//
northPanel.setLayout(new BorderLayout());
northPanel.add(textFieldPanel);
northPanel.add(buttonPanel, BorderLayout.SOUTH);
//
frame.add(northPanel, BorderLayout.NORTH);
frame.add(centerPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] arg) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame myFrame = new MyFrame();
}
});
}
}

How to set column width in JTable/JXTable?

My application consists of several JTables resp. JXTables. The goal is to store column width and reload them on start up.
But the following code does nothing change on the column widths of the tables:
tblTasks.getColumn(1).setWidth(36);
and also this one does nothing:
tblTasks.getColumn(1).setPreferredWidth(36);
Any ideas ?
But the following code does nothing change on the column widths of the
tables:
tblTasks.getColumn(1).setWidth(36);
and also this one does nothing:
tblTasks.getColumn(1).setPreferredWidth(36);
Any ideas ?
.
proper constructor is JTable.getColumnModel().getColumn(0).setPreferredWidth(36);
for example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TableRowHeight {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("p*s*s*s*s*t*t");
private String[] columnNames = {"one", "two", "Playing with", "four", "five",};
private String[][] data = {
{"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeeee",},
{"bbbbbb", "cccccc", "dddddd", "eeeeeee", "aaaaaa",},
{"cccccc", "dddddd", "eeeeeee", "aaaaaa", "bbbbbb",},
{"dddddd", "eeeeeee", "aaaaaa", "bbbbbb", "cccccc",},
{"eeeeeee", "aaaaaa", "bbbbbb", "cccccc", "dddddd",}};
private JTable table = new JTable(new DefaultTableModel(data, columnNames));
private TableColumnModel tcm = table.getColumnModel();
private Stack<TableColumn> colDeleted = new Stack<TableColumn>();
private JButton restoreButton = new JButton("Restore Column Size");
private JButton hideButton = new JButton("Set Column Size to Zero");
private JButton deleteButton = new JButton("Delete Column");
private JButton addButton = new JButton("Restore Column");
public TableRowHeight() {
table.setRowMargin(4);
table.setRowHeight(30);
table.setFont(new Font("SansSerif", Font.BOLD + Font.PLAIN, 20));
JScrollPane scrollPane = new JScrollPane(table);
for (int i = 0; i < (tcm.getColumnCount()); i++) {
tcm.getColumn(i).setPreferredWidth(150);
}
table.setPreferredScrollableViewportSize(table.getPreferredSize());
restoreButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(100);
}
});
hideButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(000);
}
});
deleteButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (table.getColumnCount() > 0) {
TableColumn colToDelete = table.getColumnModel().getColumn(table.getColumnCount() - 1);
table.removeColumn(colToDelete);
table.validate();
colDeleted.push(colToDelete);
addButton.setEnabled(true);
} else {
deleteButton.setEnabled(false);
}
}
});
addButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (colDeleted.size() > 0) {
table.addColumn(colDeleted.pop());
table.validate();
deleteButton.setEnabled(true);
} else {
addButton.setEnabled(false);
}
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(hideButton);
btnPanel.add(restoreButton);
btnPanel.add(deleteButton);
btnPanel.add(addButton);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(btnPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TableRowHeight frame = new TableRowHeight();
}
});
}
}
table.getColumn("Column Name").setMaxWidth(36);
What I did is setting the minimum width to a value for each column. For the preferred width of the last column I choose Integer.MAX_VALUE which will push all the other columns to their minimum size.
This is a snippet from my project:
ruleTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
ruleTable.getColumnModel().getColumn(0).setMinWidth(25);
ruleTable.getColumnModel().getColumn(1).setMinWidth(125);
ruleTable.getColumnModel().getColumn(2).setMinWidth(175);
ruleTable.getColumnModel().getColumn(2).setMaxWidth(Integer.MAX_VALUE);
Hope it will help you and maybe others... For you it may be a little late ;-P
table.getColumn("Column Name").setMinWidth(36);

Deleting row from JTable after valueChanged event is triggered

I'm using ListSelectionListener to update my JTextField (countryTxt) from selected row.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class App {
JFrame frame = new JFrame();
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][] {},
new String[] { "Country", "City", "Street" });
JButton button = new JButton("Remove");
JTextField countryTxt = new JTextField();
int row;
public App() {
table.setModel(model);
data();
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
row = table.getSelectedRow();
countryTxt.setText((String) model
.getValueAt(row, 0));
}
}
});
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
model.removeRow(row);
}
});
frame.add(countryTxt,BorderLayout.NORTH);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void data() {
model.addRow(new String[] { "USA", "New York", "First street" });
model.addRow(new String[] { "Russia", "Moscow", "Second street" });
model.addRow(new String[] { "Japan", "Osaka", "Osaka street" });
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
}
But when I select a row and click a button it trows me and ArrayIndexOutOfBounds exception. When I don't select a row in my table and click a button everything works fine. Obviously I can delete a row when valueChanged event is not triggered. So my question is: How to delete a row after valueChanged event is triggered. Thanks in advance.
Have a look at the javadoc of the getLeadSelectionIndex() method
Return the second index argument from the most recent call to setSelectionInterval(), addSelectionInterval() or removeSelectionInterval()
This is not what you expect. You better use the JTable#getSelectedRow() which of course still requires you to check whether it is different from -1 .
A few observations:
Selecting a row via keyboard or mouse updates the countryTxt field correctly.
You can use Control>-Tab to tab out of the table and back to your panel.
Don't use setBounds(); do use pack().
I tested your example without MigLayout, but I don't think that's relevant to your findings.
I had to track down a similar problem involving list removal a while ago. The main issue here is that the button listener's call to model.removeRow(row) was sending a valueChanged event to the model's selection listener, which would then attempt to update the text field using a nonexistent selection (i.e. a list index of -1). I've made these fixes to your code, and the relevant sections are marked with comments. This code allows items to be selected/deleted without throwing an exception.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class App {
JFrame frame = new JFrame();
DefaultTableModel model = new DefaultTableModel(new Object[][] {},
new String[] { "Country", "City", "Street" });
JTable table = new JTable(model);
JButton button = new JButton("Remove");
JTextField countryTxt = new JTextField();
public App() {
data();
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
// get the current selected row
int i = table.getSelectedRow();
// if there is a selected row, update the text field
if(i >= 0) {
countryTxt.setText((String) model
.getValueAt(i, 0));
}
}
}
});
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// get the current selected row
int i = table.getSelectedRow();
// if there's no selection, but there are some rows,
// we'll just delete the first row
if(i < 0 && model.getRowCount() > 0) {
i = 0;
}
// if we have a valid row to delete, do the deletion
if(i >= 0) {
countryTxt.setText("");
model.removeRow(i);
table.revalidate();
}
}
});
frame.add(countryTxt,BorderLayout.NORTH);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void data() {
model.addRow(new String[] { "USA", "New York", "First street" });
model.addRow(new String[] { "Russia", "Moscow", "Second street" });
model.addRow(new String[] { "Japan", "Osaka", "Osaka street" });
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
}

How to get newly updated data in a cell in a DefaultTableModel [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I create a table from DefaultTableModel, and then I use table.getModel().setValueAt(value,row,col) to add the 1st value in a cell. However, when I click the mouse on that cell and change the data inside to the 2nd value. and then I get that value back by getValueAt(row,col), it always returns that first value even though on the table the 2nd value is displayed in the cell. What am I wrong?
Hope you understand my messy idea.
Thanks a lot!
//////////////
Thank you for your answer, I have copied the code from the link you gave me and run it. I have a test like this: I modify value at the Cell(0,3) and then try getting that value out (by click the okButton) But the value is the old, NOT the new one?
Here is the code :
package DialogExample;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class SimpleTableDemo extends JPanel implements TableModelListener {
private boolean DEBUG = false;
JTable table;
JButton okButton;
public JButton getOkButton() {
okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
#Override //// modify value at the Cell(0,3) and then get that value out.
/// the value is the old, NOT the new one?????
public void actionPerformed(ActionEvent arg0) {
int i = (Integer) table.getModel().getValueAt(0, 3);
System.out.print(i);
}
});
return okButton;
}
public SimpleTableDemo() {
super(new GridLayout(1, 0));
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
// Add the scroll pane to this panel.
add(scrollPane);
add(getOkButton());
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
// Do something with the data...
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
now could be works,
have to define TableModel with proper ColumnClass
code
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class SimpleTableDemo extends JPanel implements TableModelListener {
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
private JTable table;
private JButton okButton;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), (false)},
{"John", "Doe", "Rowing", new Integer(3), (true)},
{"Sue", "Black", "Knitting", new Integer(2), (false)},
{"Jane", "White", "Speed reading", new Integer(20), (true)},
{"Joe", "Brown", "Pool", new Integer(10), (false)}};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
private JButton getOkButton() {
okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
int i = (Integer) table.getModel().getValueAt(0, 3);
System.out.print(i);
}
});
return okButton;
}
public SimpleTableDemo() {
super(new GridLayout(1, 0));
model.addTableModelListener(this);
table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
add(getOkButton());
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
Object datas = model.getValueAt(row, column);
System.out.println("Column ---> " + columnName + ", new value is " + datas);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Not exactly sure what you are trying to do without an SSCCE, but please read the tutorial about JTables in general and, more specific, model changes first.

Categories