Dynamically Add/Remove Rows from JTable - java

I have in my application two ComboBoxes for rows and columns for each JTable. Shown below:
I want to add and remove rows and columns in each table dynamically. I know I would do something like table.add() but what about remove? For example if user selects 3 from Row combobox, it should add another row to it but what if user selects 2 back? It should remove that row inserted. So how do I do that?
I know it can be a stupid question but I am a newbie to Swing so people can't really expect much from me :(

It seems that you want to control the dimension of the table using combo boxes. JTable renderers its underlying data model. Read more about tables in How to Use Tables. Adding and removing rows is actually manipulation of the data model. For example DefaultTableModel has many methods useful for your task: addRow(), removeRow(), getRowCount() etc.
It all depends on the data and task required. Check out this simple example that uses a custom table model that wraps Apache's RealMatrix. You can choose whatever data structure you need. DefaultTableModel is also good an may be sufficient. The example has two combo boxes that adjust table size.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
public class TestTableDims extends JPanel{
private MyTableModel model;
private JTable table;
private JComboBox rowsCombo;
private JComboBox columnsCombo;
public TestTableDims() {
setLayout(new BorderLayout(5, 5));
model = new MyTableModel(2, 2);
JPanel buttonsPanel = new JPanel();
Integer[] test = {1, 2, 3, 4, 5};
rowsCombo = new JComboBox(test);
rowsCombo.setSelectedItem(2);
rowsCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.setRows(((Integer)rowsCombo.getSelectedItem()));
}
});
buttonsPanel.add(new JLabel("rows"));
buttonsPanel.add(rowsCombo);
columnsCombo = new JComboBox(test);
columnsCombo.setSelectedItem(2);
columnsCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.setColumns(((Integer)columnsCombo.getSelectedItem()));
}
});
buttonsPanel.add(new JLabel("columns"));
buttonsPanel.add(columnsCombo);
add(buttonsPanel, BorderLayout.NORTH);
JTable table = new JTable();
table.setModel(model);
add(new JScrollPane(table));
}
class MyTableModel extends AbstractTableModel {
private RealMatrix matrix;
public MyTableModel(int rows, int columns) {
matrix = new Array2DRowRealMatrix(rows, columns);
}
public void setRows(int rows) {
matrix = new Array2DRowRealMatrix(rows, matrix.getColumnDimension());
fireTableStructureChanged();
}
public void setColumns(int columns) {
matrix = new Array2DRowRealMatrix(matrix.getRowDimension(), columns);
fireTableStructureChanged();
}
#Override
public int getColumnCount() {
return matrix.getColumnDimension();
}
#Override
public int getRowCount() {
return matrix.getRowDimension();
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return Double.class;
}
#Override
public void setValueAt(Object value, int row, int column) {
matrix.setEntry(row, column, (double)value);
}
#Override
public Object getValueAt(int row, int column) {
return matrix.getEntry(row, column);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
TestTableDims panel = new TestTableDims();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}

Related

How to give keyboard focus to Custom panel in JTable?

I am working on making the Arduino IDE fully accessible for people using access software. This means adding keyboard accessibility to two of the dialogs that currently do not work with access software under windows. The two dialogs are the Board Manager and the LibraryManager. If you want a real example of what I am posting you can get teh Arduino IDE on the Windows Store or download it on line. Just search for Arduino IDE and download the windows version. .
doing all that might be to much for most of you so I have created a more simple table that shows the same problem. If someone can help me understand how to add keyboard and focus access to the below Custom JTable and its custom column which is a JPanel I can work on fixing the Arduino IDE tables.
I understand that the Arduino IDE should probably be made into a
better table but I have yet to figure out a cleaner way to do what they are doing with these tables so I am trying to make what they have accessible.
So below is my problem table. It has two rows and two columsn. The
second column of each row is a panel of controls. I have borrowed
this code from some other stack posts and modified it to fit my
question. So if you think you have seen some of this code before that
is why. The problem is the code works for a mouse. At least I think
it does because I am blind and can not select the controls on the
second column because they can not get keyboard focus.
So Can someone out there help me figure out how to get keyboard focus
to move through this entire table. I have tried using focus
listeners, keyboard listeners, Focus Traversal policies, property
changes, and combinations between all of the above. I have taken out
my terrible attempts. Swing really should work without having to do
all this extra work but Java is what java is.
So if you are great at Java with swing please put away your mouse and run this
code and see if you can make the buttons activate without the mouse.
If you can and you can explain to me how to do it many blind users will
be in your debt.
Note that you should be able to Shift+Control+tab tot he table if the table is not the only control. On my sample it is. Then you should be able to arrow through the rows and columns and tab and shift+tab through the controls on the JPanel.
You can get the zip group of these files if you don't want to
put this all together yourself at this dropbox link:
https://www.dropbox.com/s/h4bdfu1mlo0jsvr/TableAccessTest.zip?dl=0
Here is the files. Each class has a comment with the file name before it. Again get the zip if you don't want to play block and copy.
//TableAccessTest.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
public class TableAccessTest extends JPanel
{
TableModel customRendererEditorTableModel = new TableModel();
JTable table;
public TableAccessTest()
{
setLayout(new BorderLayout());
table = new JTable(customRendererEditorTableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 600));
table.setRowSelectionAllowed(false);
table.setRowHeight(300);
table.getColumn("Custom JPanel").setCellRenderer(new CustomJPanelRenderer());
table.getColumn("Custom JPanel").setCellEditor(new CustomJPanelEditor());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Custom JPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new TableAccessTest();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
//CustomJPanel.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class CustomJPanel extends JPanel
{
JPanel colorPanel = new JPanel(new BorderLayout());
JButton jButton = new JButton("Change Color");
JSlider jSlider = new JSlider(JSlider.VERTICAL);
JTextField jTextField = new JTextField("");
public CustomJPanel()
{
jButton.setOpaque(true);
jButton.setFocusable(true);
jTextField.setEditable(false);
jTextField.setFocusable(true);
setFocusable(true);
jButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(4);
switch (randomInt)
{
case (0):
colorPanel.setBackground(Color.BLUE);
jTextField.setText("blue");
break;
case (1):
colorPanel.setBackground(Color.BLACK);
jTextField.setText("black");
break;
case (2):
colorPanel.setBackground(Color.CYAN);
jTextField.setText("cyan");
break;
default:
colorPanel.setBackground(Color.GREEN);
jTextField.setText("green");
break;
}
}
});
jSlider.setOpaque(true);
setLayout(new BorderLayout());
add(colorPanel, BorderLayout.CENTER);
add(jButton, BorderLayout.SOUTH);
add(jTextField, BorderLayout.NORTH);
add(jSlider, BorderLayout.EAST);
}
}
//TableModel.java
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel
{
private String[] columnNames =
{
"First Column",
"Custom JPanel",
};
private Object[][] data =
{
{"Foo", new CustomJPanel()},
{"Bar", new CustomJPanel()}
};
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col)
{
return col >= 1;
}
public void setValueAt(Object value, int row, int col)
{
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
//CustomJPanelEditor.java
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
public class CustomJPanelEditor extends AbstractCellEditor implements TableCellEditor
{
CustomJPanel customJPanel;
public CustomJPanelEditor()
{
customJPanel = new CustomJPanel();
}
#Override
public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
customJPanel = (CustomJPanel) value;
return customJPanel;
}
#Override public Object getCellEditorValue()
{
return customJPanel;
}
}
//CustomJPanelRenderer.java
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class CustomJPanelRenderer implements TableCellRenderer
{
CustomJPanel rendererJPanel;
public CustomJPanelRenderer()
{
rendererJPanel = new CustomJPanel();
}
#Override
public java.awt.Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
rendererJPanel = (CustomJPanel) value;
return rendererJPanel;
}
}

Adding values to Jtable when button is clicked in java [duplicate]

In my application everything is distributed.
On a action, application retrieves data from DB and saves in ArrayList<T>.
I create an object of RelativeTableModel where I pass the ArrayList<T>.
public void RelationsClicked() {
ArrayList<Relation> data = myParent.dbOperation.getRelations();
RelativeTableModel tModel = new RelativeTableModel(data); // subclass of AbstractTableModel
myParent.SetBrowsePanelData(tModel);
myParent.SetMainPanel(CashAccountingView.BROWSEPANEL);
}
I have a BrowseListPanel class that has a JTable in JScrollPane. Its instance is already created in the main application.
I pass the model to BrowseListPanel and finally show the panel.
Code:
public void SetBrowsePanelData(AbstractTableModel tModel) {
browsePanel.setTModel(tModel);
}
// BrowseListPanel's Code
public void setTModel(AbstractTableModel tModel) {
this.tModel = tModel; // tModel = AbstractTableModel
}
// Show the Panel
public void SetMainPanel(String panel) {
activePanel = panel;
SetFontSize();
cards.show(mainPanel, panel);
mainPanel.revalidate();
mainPanel.repaint();
}
But I don't see the Table. I believe as the object of BrowseListPanel (containing the JTable) is already created & later the TableModel is added. So some sort of event should be fired in setTModel().
Am I right? If so, what event should be thrown and what should be its implementation.
Invoking setModel() on the table should be sufficient, but you might call fireTableStructureChanged() on the model explicitly as a way to help sort things out.
Also, verify that you are working on the event dispatch thread.
Addendum: Here's an sscce that shows the basic approach.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/** #see http://stackoverflow.com/questions/8257148 */
public class SwapTableModel extends JPanel {
public SwapTableModel() {
final JTable table = new JTable(Model.Alpha.model);
table.setPreferredScrollableViewportSize(new Dimension(128, 32));
this.add(new JScrollPane(table));
final JComboBox combo = new JComboBox();
for (Model model : Model.values()) {
combo.addItem(model);
}
this.add(combo);
combo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Model model = (Model) combo.getSelectedItem();
table.setModel(model.model);
}
});
}
private enum Model {
Alpha(), Beta();
private DefaultTableModel model;
private Model() {
Object[] data = {this.toString()};
this.model = new DefaultTableModel(data, 1);
model.addRow(data);
}
}
private void display() {
JFrame f = new JFrame("SwapTableModel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new SwapTableModel().display();
}
});
}
}

JTable component access before row selection

I'm using a jtable and i need to access a component locate in a row different from the selected one.
The problem i have is that when i click the component the row selection change come before the component click event.
This is a SSCE that explain the problem.
package test;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
JButton btnTest = new JButton("test");
btnTest.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.err.println("btnTest clicked!");
}
});
JButton btnTest2 = new JButton("test2");
btnTest2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.err.println("btnTest2 clicked!");
}
});
JTable table = new JTable(new Object[][] {
{ btnTest, "test"},
{ btnTest2, "test2"},
}, new Object[] {"btn", "desc"});
table.setDefaultEditor(Object.class, new Editor());
table.setDefaultRenderer(JComponent.class, new TableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return (JComponent)(value);
}
});
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.err.println("new Selection!");
}
});
table.setSelectionModel(rowSM);
add(table);
setSize(300, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
class Editor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = 1L;
private JComponent item;
#Override
public Object getCellEditorValue() {
return item;
}
#Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
item = (JComponent)value;
item.setBackground(table.getSelectionBackground());
return item;
}
}
}
When i click the button the row unselected this is the console result:
new Selection!
btnTest clicked!
new Selection!
Is possible to fire the "btnTest clicked!" first?
Thanks

Enable and Disable Sorting of JTable dynamically

I'm new to Java Swing and I'm working with JTable.
Actually, I have difficulty in sorting of this component.
I want to enable and disable this function programatically.
I use JTable.setAutoCreateSorter(true) to initialize it, I change to false, nothing happen.
Also, I had use JTable.setRowSorter(null), it works, but I do not know how to enable it again.
Sorry for my poor English!
You can use setSortable(int index, boolean sortable) method of DefaultRowSorter.
for (int i=0 ; i<table.getColumnCount() ; i++) {
sorter.setSortable(i, false);
}
This worked for me. See also JTable sorting programatically only
Inspecting at JTable.setAutoCreateRowSorter(boolean autoCreateRowSorter) source code:
public void setAutoCreateRowSorter(boolean autoCreateRowSorter) {
boolean oldValue = this.autoCreateRowSorter;
this.autoCreateRowSorter = autoCreateRowSorter;
if (autoCreateRowSorter) {
setRowSorter(new TableRowSorter<TableModel>(getModel()));
}
firePropertyChange("autoCreateRowSorter", oldValue,
autoCreateRowSorter);
}
So, you have two options:
Keep a reference to the row sorter to restore it later, as #nachokk suggested.
Set a new TableRowSorter instance:
table.setRowSorter(new TableRowSorter(table.getModel()));
Edit
But now the table's rows are not selectable
That's weird, I didn't have any problem with row selection. Here is the code I've used to test my answer:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class Demo {
private void createAndShowGUI() {
DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0){
#Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case 0: return String.class;
case 1: return Integer.class;
default: return super.getColumnClass(columnIndex);
}
}
};
for (int i = 0; i < 20; i++) {
model.addRow(new Object[]{"Property # " + i, Integer.valueOf(i)});
}
final JTable table = new JTable(model);
table.setAutoCreateRowSorter(true);
JToggleButton toggleButton = new JToggleButton("Disable", true);
toggleButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JToggleButton toggleButton = (JToggleButton)e.getSource();
String text = toggleButton.isSelected() ? "Disable" : "Enable";
TableRowSorter sorter = toggleButton.isSelected() ? new TableRowSorter(table.getModel()) : null;
toggleButton.setText(text);
table.setRowSorter(sorter);
}
});
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.add(toggleButton, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

How do I render a JTable inside of a JTable?

I am trying to nest a JTable inside of another JTable's column (using a CellRenderer).
Example (erroneous) output:
Why does the following example not output a table within a table?
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
public class Test {
public static void main(String[] args) {
JTable table = new JTable(new CustomTableModel());
table.setDefaultRenderer(Person.class, new CustomRenderer());
JPanel panel = new JPanel();
panel.add(new JScrollPane(table));
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
}
#SuppressWarnings("serial")
class CustomTableModel extends AbstractTableModel {
#Override
public int getColumnCount() {
return 2;
}
#Override
public int getRowCount() {
return 1;
}
#Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return new Person("Bob");
} else {
return "is awesome!";
}
}
}
#SuppressWarnings("serial")
class CustomRenderer extends JPanel implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
JTable t = new JTable(new CustomTableModel());
add(new JScrollPane(t));
return this;
}
}
class Person {
public String name = "";
public Person(String name) {
this.name = name;
}
}
Don't add the sub table to a scroll pane. Instead, try adding the table to the center position of a JPanel with a BorderLayout, then add the tables header to the NORTH position
The main reason for this is the scroll pane will not be interactive and may hide data
Updated
I'm on phone, so it's hard to read the code :P
In your main table model, you need to override the getColumnClass method and make sure you are retuning Person.class for the required column (and the correct class types for the other columns to)

Categories

Resources