I saw the BallonTip at java.net and I tried to integrate it into my application to be displayed when a user clicks a table cell. When a table cell is clicked, the BalloonTip is showing up as intended, but when you scroll it out of the current viewport, you can click another cell without the BalloonTip showing up. When you scroll the table, the BallonTip is shown again.
Here is an example:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.TablecellBalloonTip;
import net.java.balloontip.styles.EdgedBalloonStyle;
public class TableTest2 extends JFrame {
static final int LENGTH = 40;
TablecellBalloonTip tip;
JTable mainTable;
JPanel main;
JLayeredPane layeredPane;
JScrollPane mainScroll;
TableTest2() {
mainTable = new JTable(LENGTH, LENGTH);
CustomListSelectionListener clsl = new CustomListSelectionListener(mainTable);
mainTable.getColumnModel().getSelectionModel().addListSelectionListener(clsl);
mainTable.getSelectionModel().addListSelectionListener(clsl);
mainTable.setTableHeader(null);
mainTable.setColumnSelectionAllowed(true);
mainScroll = new JScrollPane(mainTable);
add(mainScroll);
tip = new TablecellBalloonTip(mainTable, new JLabel("Hello World!"), -1, -1, new EdgedBalloonStyle(Color.WHITE,
Color.BLUE), BalloonTip.Orientation.LEFT_ABOVE, BalloonTip.AttachLocation.ALIGNED, 5, 5, false);
setPreferredSize(new Dimension(500, 400));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
new TableTest2();
}
protected class CustomListSelectionListener implements ListSelectionListener {
private int row, column, lead, anchor;
private JTable table;
public CustomListSelectionListener(JTable table) {
this.table = table;
}
#Override
public void valueChanged(ListSelectionEvent evt) {
if (evt.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {
// row selection changed
row = table.getSelectedRow();
column = table.getSelectedColumn();
tip.setCellPosition(row, column);
tip.refreshLocation();
} else if (evt.getSource() == table.getColumnModel().getSelectionModel()
&& table.getColumnSelectionAllowed()) {
// column selection changed
lead = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
anchor = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
if (lead <= anchor) {
column = anchor;
} else {
column = lead;
}
row = table.getSelectedRow();
tip.setCellPosition(row, column);
tip.refreshLocation();
}
}
}
}
How can I force the BalloonTip to be shown after I click a cell in the table? I think there is a listener, which is listening for the scrolling event and manages the painting of the BallonTip, but I do not have a clue which one it is.
best regards
htz
According to this mailing list, there was a bug in the BallonTip version 1.2.1. Now, with version 1.2.3, this is fixed.
Related
I'm trying To insert a JTable into a JScrollPane and I see a small gap between the borders of the table and the scroll Pane.And on the left side the table looks to be aligned to extreme left.Can someone please help me fix this.?
Removing setAutoResizeMode(JTable.AUTO_RESIZE_OFF) is fixing that.But I Need to turn that off.
this.dataTable = new SortableTable(this);
this.dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.dataTable.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
scrollPane = new JScrollPane(dataTable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(900,250));
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
You simply need to set the resize mode in a listener, when the component is resized. Here is an example:
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.TableColumn;
public class TableTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TableTest());
}
#Override
public void run() {
JTable table = new JTable(10, 3);
final JScrollPane scroller = new JScrollPane(table);
// update the resize mode when scroller is resized and window is shown
scroller.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
Window win = SwingUtilities.windowForComponent(scroller);
if (win != null && win.isVisible()) {
updateColumns(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
}
});
JFrame frm = new JFrame("Table");
frm.add(scroller);
frm.setSize(600, 400);
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
// Transfer column widths from autoresize mode
private void updateColumns(JTable table) {
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn col = table.getColumnModel().getColumn(i);
col.setPreferredWidth(col.getWidth());
}
}
}
I have been lurking around here for a couple years or so, never needed to ask a question before because I have always found my answer in someone else's question. Thank you!
I guess the lurking has come to an end. I have seen similar questions but not exactly this situation on here:
I have a 2 column JTable with a JComboBox in the first column, and an integer in the second column.
JComboBox has ItemListener set up so that when the selection in the JComboBox is changed the value in the Integer column is set to the comboBox selected index. Right click on the table renders JPopupMenu with addRow() as MouseEvent.
It works fine as long as I add all the rows I want when setting up the DefaultTableModel. But, if I start the model with only one row and use the MouseEvent (or any other method of adding rows other than adding them in parameters of DefaultTableModel) to add rows, when we start changing the selections in the combo boxes it will change the integer values in other rows.
For example: If i run the program and immediately add two more rows via MouseEvent, I then select Zero from combo in row 0, One from combo in row 1, and Two from combo in row 2. All fine so far...
Then I go back to row 0 and as soon as I activate the combobox (I haven't selected an item yet...) it changes the integer in row 2 to 0.
Can anyone tell me how to stop the integers from changing in this manner?
here is a trial code:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class JComboBoxInJTable {
public static void main(String[] args) {
new JComboBoxInJTable();
}
public JComboBoxInJTable() {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"ComboBox", "Index"}, 1);
JTable table = new JTable(model);
//popup menu to add row
JPopupMenu popup = new JPopupMenu();
JMenuItem newRow;
newRow = new JMenuItem("New Row");
newRow.setToolTipText("Add new row.");
newRow.addActionListener((ActionEvent nr) -> {
model.addRow(new Object[]{"", ""});
});
popup.add(newRow);
//set up right-click to open popup menu
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent rc) {
if (SwingUtilities.isRightMouseButton(rc)) {
if (table.getSelectedRow() >= 0) {
popup.show(table, rc.getX(), rc.getY());
}
}
}
});
JComboBox combo = new JComboBox(new Object[]{"Zero", "One", "Two", "Three"});
combo.addItemListener((ItemEvent e) -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
//sets value of cell to left of combobox to comboboxe's selected index
table.setValueAt(combo.getSelectedIndex(), table.getSelectedRow(), 1);
} else {
//do nothing...
}
});
DefaultCellEditor comboEditor = new DefaultCellEditor(combo);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setCellEditor(comboEditor);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
As usual I am sure this is something simple that I am missing. Thanks Again for the help in the past and in the future!
Don't use MouseListeners to show popup menus, different OSs have different triggers for the popups, and not all are triggered by mousePressed. Instead use JComponent#setComponentPopupMenu and let the API deal with it
Don't modify the state of the model from an editor. This can place the model into an invalidate state and cause other side effects, like you have now. Once seems to be happening is when a new row is selected, the editor is been updated with the rows cell value, but the row selection hasn't been set, so the table still thinks the previous row is still selected. Instead, use the models setValueAt method to make decisions about what to do once the first columns value is changed.
For example...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class JComboBoxInJTable {
public static void main(String[] args) {
new JComboBoxInJTable();
}
private List<String> comboData;
public JComboBoxInJTable() {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"ComboBox", "Index"}, 1) {
#Override
public void setValueAt(Object aValue, int row, int column) {
super.setValueAt(aValue, row, column);
if (column == 0) {
String value = aValue == null ? null : aValue.toString();
if (aValue == null) {
super.setValueAt(null, row, 1);
} else {
super.setValueAt(comboData.indexOf(aValue), row, 1);
}
}
}
};
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
table.setGridColor(Color.GRAY);
//popup menu to add row
JPopupMenu popup = new JPopupMenu();
JMenuItem newRow;
newRow = new JMenuItem("New Row");
newRow.setToolTipText("Add new row.");
newRow.addActionListener((ActionEvent nr) -> {
model.addRow(new Object[]{"", ""});
});
popup.add(newRow);
table.setComponentPopupMenu(popup);
comboData = new ArrayList<>(Arrays.asList(new String[]{"Zero", "One", "Two", "Three"}));
JComboBox combo = new JComboBox(comboData.toArray(new String[comboData.size()]));
// combo.addItemListener((ItemEvent e) -> {
// if (e.getStateChange() == ItemEvent.SELECTED) {
// //sets value of cell to left of combobox to comboboxe's selected index
// System.out.println("Selected row = " + table.getSelectedRow());
// table.setValueAt(combo.getSelectedIndex(), table.getSelectedRow(), 1);
// } else {
// //do nothing...
// }
// });
DefaultCellEditor comboEditor = new DefaultCellEditor(combo);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setCellEditor(comboEditor);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
i'm workin on programe and i need to get the selected radio buttom from a Jtable
I found an exemple that i'm workin on
there is to class
the 1st :
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
class RadioButtonRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
return (Component) value;
}
}
class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JRadioButton button;
public RadioButtonEditor(JCheckBox checkBox) {
super(checkBox);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value == null)
return null;
button = (JRadioButton) value;
button.addItemListener(this);
return (Component) value;
}
public Object getCellEditorValue() {
button.removeItemListener(this);
return button;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
and the 2nd class is :
package TP2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
public class tablesDesEtudiants extends JFrame {
public tablesDesEtudiants(Object[][] objt) {
super("List des etudiants");
// UIDefaults ui = UIManager.getLookAndFeel().getDefaults();
// UIManager.put("RadioButton.focus", ui.getColor("control"));
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(objt , new Object[] {"Nom","Prénom","Année","Succs","Type","Select"});
JTable table = new JTable(dm) {
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
repaint();
}
};
final ButtonGroup group1 = new ButtonGroup();
for(int i =0 ; i<objt.length;i++){
if(objt[i][1]!=null)
group1.add((JRadioButton) dm.getValueAt(i, 5));
}
// System.out.println(objt.length);
table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Select").setCellEditor(new RadioButtonEditor(new JCheckBox()));
JScrollPane scroll = new JScrollPane(table);
JButton bView = new JButton("View");
bView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonModel choix = group1.getSelection();
if (choix != null) {
System.out.println( choix.getActionCommand());
}
else System.out.println("nullll");
}
});
JPanel pp =new JPanel();
MigLayout layout = new MigLayout(
"", // Layout Constraints
"[][]20[]", // Column constraints
"[]20[]"); // Row constraint
pp.setLayout(layout);
pp.add(scroll,"cell 1 2");
pp.add(bView,"cell 2 3");
getContentPane().add(pp);
setSize(600, 400);
setVisible(true);
}
/* public static void main(String[] args) {
JRadioButtonTableExample frame = new JRadioButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}*/
}
in the 2nd class i'm trying to add all the radio button on ButtonGroup
like this :
final ButtonGroup group1 = new ButtonGroup();
for(int i =0 ; i<objt.length;i++){
if(objt[i][1]!=null)
group1.add((JRadioButton) dm.getValueAt(i, 5));
}
and my problem is that i can't get selected RadioButton :
ButtonModel choix = group1.getSelection();
if (choix != null) {
System.out.println( choix.getActionCommand());
}
else System.out.println("nullll");
}
i alwayse get nullll
any help plz !!
Read my answer to this question. You shouldn't store components in a table. Store booleans. You should then configure a renderer to display the boolean as a radio button, and an editor to change the value of the checked cell as well as the value of the previously checked one. And understand that the same instance of renderer is used to render/edit all the cells of the same class.
I think that not easy job, TableCellEditor for JRadioButtons in the ButtonGroup
use JCheckBox if is possible
use JComboBox as TableCellEditor instead of JRadioButtons in the ButtonGroup if is possible, for TableCellRenderer you can use JRadioButtons in the ButtonGroup
Hi I want to put my button at the South position! How can I do that? Here is my code:
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
public class TableDemo extends JPanel {
private static Icon leftButtonIcon;
private boolean DEBUG = false;
// added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();
public TableDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JButton button=new JButton("Buy it");
button.setSize(30,60);
button.add(button);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//create a button
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rosterList.size();
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return rosterList.get(row)[col];
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Something like this?
See the comments in the code.
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
public class TableDemo extends JPanel {
private static Icon leftButtonIcon;
private boolean DEBUG = false;
// added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();
public TableDemo() {
super(new BorderLayout(3,3));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JButton button=new JButton("Buy it");
// Rarely has the intended effect.
// also best not to presume we can guess the size
// a component needs to be.
//button.setSize(30,60);
// cannot add a button to itself!
//button.add(button);
JPanel buttonCenter = new JPanel( new FlowLayout(FlowLayout.CENTER) );
// allow the button to be centered in buttonCenter,
// rather than stretch across the width of the SOUTH
// of the TableDemo
buttonCenter.add(button);
add(buttonCenter, BorderLayout.SOUTH);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane, BorderLayout.CENTER);
//create a button
// add a nice border
setBorder(new EmptyBorder(5,5,5,5));
}
class MyTableModel extends AbstractTableModel {
// apologies about the column names
private String[] columnNames = { "??d????", "??s?t?ta", "??µ?", "?e????af?", "???e???", "??tsa"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rosterList.size();
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return rosterList.get(row)[col];
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
// JPanels are opaque by default!
//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) {
// Should be done on the EDT.
// Left as an exercise for the reader.
TableDemo.createAndShowGUI();
}
}
You add the button to itself, and you should use BorderLayout, if you want to place the components in Center / North / South / etc. manners:
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
"Hi i want to put my button at the South posotion! how can i?"
If you want to place something in a BorderLayout location, it would make sense to have the container use a ... BorderLayout, n'est-ce pas?
But seriously, most of your recent questions in this forum suggest you're coming here before reading the tutorials. You've already been given the links several times, so please do yourself a favor and learn Swing right -- study the layout tutorials and other tutorials and you will save yourself a lot of grief and guessing.
try this:
JFrame frame = new JFrame();
frame.add(yourbutton,BORDERLAYOUT.SOUTH)
and for give position and size to button,do this
yourbutton.setBounds(10,15,120,200) --> (x,y,height,width) , x , y set possition
Does anyone have a good example of how to Add/Remove rows from a JTable using a custom table model? The issue I seem to be having is how to have the table keep updating when I add or remove items.
The real simple idea here is to have an add and remove button above my table which allows the user on the fly to change the table.
Here is example for adding row:
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
public class RowAdder extends JFrame {
final SimpleModel tableData = new SimpleModel();
JTable table = new JTable(tableData);
public static void main(String[] args) {
RowAdder ra = new RowAdder();
ra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ra.setSize(400, 300);
ra.setVisible(true);
}
public RowAdder() {
final JTextField textField = new JTextField();
setLayout(new BorderLayout());
add(new JScrollPane(table), BorderLayout.CENTER);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tableData.addText(textField.getText());
textField.setText("");
}
});
add(textField, BorderLayout.SOUTH);
}
}
class SimpleModel extends AbstractTableModel {
Vector textData = new Vector();
public void addText(String text) {
textData.addElement(text);
fireTableDataChanged();
}
public int getRowCount() {
return textData.size();
}
public int getColumnCount() {
return 3;
}
public Object getValueAt(int row, int column) {
return textData.elementAt(row);
}
}
above ref from : http://www.java2s.com/Tutorial/Java/0240__Swing/AddrowstoaTable.htm
Checkout this tutorial about JTable:
http://download.oracle.com/javase/tutorial/uiswing/components/table.html
Specifically for table model check:
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data
I think this tutorial should answer all your question.
You have to notify the JTable object on changes of the underlying table model. The table is not observing the model but waiting for events.
After every change (or set of changes), create a TableModelEvent and call the tables tableChanged method.