I have a working program using a Model composed by a list of Item.
public class Model {
private List<Item>;}
public abstract class Item{}
public class A extends Item{}
public class B extends Item{}
public class C extends Item{}
But now i need to create a view with some action buttons (add,del,edit) with a JTable on the center showing Specific Item (A,B,C).
Here is my first problem because each specific item will have a column for one of its field, so i need a different table for each item.
A solution could be to have a cardlayout with one table for each type of item, but it comes a new problem how can my view determine how many type of item there is in the model without using instanceof() ?
Moreover i will have others problems after this one, if i have x jtable in my view how will my view get the model of this table ? i can implements for each item an interface like that :
public interface MyModel{
AbstractTableModel getModel();
}
but i can only give to this function a List, so how each item will fill the data array with only its type of item?
PS : if i go further in my reflexion i have a bonus question, i'm wondering how the listener of my action button can simply know which JTable is currently being modify, should i put the listener in the view for simplicity of access to the cardlayout ?
If something is needed to improve my question, ask for it ! Not sure the question is currently clear.
EDIT : adding SSCCE, this is what i'm aiming but it currently doesn't use items of my program and doesn't implement the TableModel/TableModelListener.
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class View extends JPanel implements TableModelListener{
private static final long serialVersionUID = 1L;
private JTabbedPane card;
public View() {
Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
String columnNames[] = { "#", "English", "Roman" };
Object rowData2[][] = { { "1", "B" } };
String columnNames2[] = { "#", "type" };
setLayout(new BorderLayout());
JPanel actionbutton = new JPanel();
JButton but = new JButton("fire");
but.addActionListener(new ButtonListener());
actionbutton.add(but);
add(actionbutton,BorderLayout.SOUTH);
card = new JTabbedPane();
//something should determine how many type of object in a List<Item> w/o using instanceof
//should fill jtable with a specific TableModel for each item type
JTable card1 = new JTable(rowData,columnNames);
JTable card2 = new JTable(rowData2,columnNames2);
card.addTab("Item A",new JScrollPane(card1));
card.addTab("Item B",new JScrollPane(card2));
add(card,BorderLayout.CENTER);
}
private class ButtonListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ("fire".equals(cmd)) {
//do something on the model
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(800, 600);
f.add(new View());
f.setVisible(true);
}
#Override
public void tableChanged(TableModelEvent e) {
//update jtable with the model's modification
}
}
TableModel supports generic types using Class Literals as Runtime-Type Tokens. Your implementation of getColumnClass() (below) defines the types available to the table's columns, and it defines the selection of any non-default renderers and editors. While DefaultTableModel offers considerable convenience, AbstractTableModel is only slightly more difficult and substantially more flexible. As a concrete example, this EnvTableTest illustrates a table modeled on a Map<String, String>.
#Override
public Class<?> getColumnClass(int columnIndex) {
// return a token for the specified column
}
Related
I have a JComboBox that is editable. When the user enters a new item, I want that added to the list and display it as the selected item. I am able to add it to the list but I cannot seem to make it display as the selected item. By default I display an empty string ("") which is what the user would edit to add the new item.
public class EventComboBoxListener implements ActionListener {
private JComboBox<String> eventBox=null;
public EventComboBoxListener(JComboBox<String> event_) {
eventBox=event_;
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected: " + eventBox.getSelectedItem());
System.out.println(", Position: " + eventBox.getSelectedIndex());
if (eventBox.getSelectedIndex() < 0) {
eventBox.addItem(eventBox.getSelectedItem().toString());
eventBox.setSelectedItem(eventBox.getSelectedItem().toString());
}
}
}
It doesn't make sense to me that I have to use setSelectedItem with the getSelectedItem. That it does not work is no surprise but I don't know what else to do. The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time? I can select it after but that should not be necessary.
Added MVCE:
Main
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
String[] list= {"","A","B","C"};
TestTableModel model=new TestTableModel(null,new String[] {"col1","col2"});
JTable table=new JTable(model);
JDialog dialog=new JDialog();
JScrollPane scroller=new JScrollPane(table);
JComboBox<String> box=new JComboBox<String>(list);
box.setEditable(true);
box.setSelectedIndex(0);
box.addActionListener(new EventComboBoxListener(box));
JTextField field=new JTextField();
field.setPreferredSize(new Dimension(75,30));
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLayout(new FlowLayout());
dialog.setSize(new Dimension(400,100));
dialog.add(scroller);
dialog.pack();
dialog.setVisible(true);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(box));
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(field));
model.insertRow(0,new Object[] {"","placeholder"});
}
}
TestTableModel class
import javax.swing.table.DefaultTableModel;
public class TestTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
public TestTableModel(Object[][] data_,String[] columnNames_) {
super(data_,columnNames_);
}
}
First of all some comments about the MCVE (since you will be including one with every question in the future).
We expect the code so be in a single source file so we can easily copy/paste compile and test. We don't want 3 files lying around on our machine that we need clean up after testing.
Only relevant code directly related to the problem should be included. Why do you have the TestTableModel class. Are the "column names" relevant to the problem? The point is always test your MCVE using standard JDK classes when possible.
Regarding the EventComboListener class. Again, this can be added to the combo box by using and annoymouse inner class or a lambda. This keeps the code in a single class.
The newly added item shows up in the list as it should but how do I make it the selected item in the display at the same time?
I found that playing with your MCVE the ActionListener of the combo box is invoked at different times.
So my suggestion is to add the ActionListener to the editor of the combo box. Then we know for sure the ActionListener is only invoked when you press the Enter key. Once you press the Enter key the editor is stopped and the value is saved to the model.
So the logic would be something like:
//box.addActionListener(new EventComboBoxListener(box));
ComboBoxEditor editor = box.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.addActionListener( new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
String item = textField.getText();
DefaultComboBoxModel model = (DefaultComboBoxModel)box.getModel();
if (model.getIndexOf(item) == -1)
{
box.addItem(item);
box.setSelectedIndex( box.getItemCount() - 1 );
}
}
});
So the trick is to set the select index (not the selected item). But first the logic checks to make sure the item has not already been added to the combo box.
I have noticed that when I have a JTable with a TableRowSorter contained by a JScrollPane, the vertical scrollbar does not appear until after I have created SortKeys for the sorter (which is done by calling toggleSortOrder() for one of the columns).
My question is really why? What do SortKeys have to do with a vertical scrollbar?
Update: Added SSCCE that opens a JFrame with a JTable inside a JScrollPane, that sits in a Container along with a "Populate" button. When the table is initially painted, there is no data and hence no need for a scroll bar. After I populate it with 20 rows, there is a need for a scroll bar, but none appears.
There are two ways to make the scroll bar appear:
Click on either of the header cells to cause a sort to occur.
Remove the commented call toggleSortOrder() in the Container's refresh() method.
// table.getRowSorter().toggleSortOrder(0);
toggleSortOrder() calls setSortKeys() calls sort() calls fireRowSorterChanged() and eventually something catches the event and adds the scroll bar.
package test;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
#SuppressWarnings("serial")
public class TestFrame extends JFrame
{
class MyTableModel extends AbstractTableModel
{
public List<String> names = new ArrayList<String>();
public int getRowCount ()
{
return names.size();
}
public int getColumnCount ()
{
return 2;
}
public String getColumnName (int columnIndex)
{
return columnIndex > 0 ? "Name" : "Number";
}
public Class<?> getColumnClass (int columnIndex)
{
return String.class;
}
public Object getValueAt (int row, int col)
{
return row < names.size() ? col == 0 ? Integer.toString(row) : names.get(row) : "";
}
public void refresh (List<String> names)
{
this.names = names;
}
}
class MyContainer extends java.awt.Container implements ActionListener
{
JTable table;
MyTableModel model = new MyTableModel();
private TableRowSorter<MyTableModel> sorter;
public MyContainer()
{
}
public void init ()
{
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setBorder(BorderFactory.createEmptyBorder());
table.setRowHeight(35);
table.getTableHeader().setPreferredSize(new Dimension(200, 35));
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(200, 70));
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 10, 200, 210);
//Add the scroll pane to this panel.
add(scrollPane);
JButton btn = new JButton("Populate");
btn.setActionCommand("populate");
btn.addActionListener(this);
btn.setBounds(10, 220, 200, 35);
add(btn);
}
public void refresh (List<String> rows)
{
model.refresh(rows);
try
{
// Notify sorter that model data (possibly number of rows) has changed.
// Without this call, the sorter assumes the number of rows is the same.
table.getRowSorter().allRowsChanged();
// Do we really want to toggle the sort order every time we refresh?
// User can toggle the sort order himself by clicking on the
// appropriate header cell.
List<?> keys = table.getRowSorter().getSortKeys();
if (null == keys || keys.isEmpty())
{
// table.getRowSorter().toggleSortOrder(0);
}
} catch (Exception e)
{
e.printStackTrace();
}
table.repaint();
}
public void actionPerformed(ActionEvent e)
{
if ("populate".equals(e.getActionCommand()))
{
List<String> rows = new ArrayList<String>();
for (int ii = 0; ii < 20; ii++)
{
rows.add(String.format("%02d", new Integer(ii)));
}
refresh(rows);
}
}
MyTableModel getModel ()
{
return model;
}
}
public static void main (String args[])
{
new TestFrame();
}
MyContainer myContainer = new MyContainer();
TestFrame()
{
myContainer.init();
myContainer.table.getSelectionModel().clearSelection();
add(myContainer);
this.setSize(240, 310);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//pack();
setVisible(true);
}
}
Well, that is not really a SSCCE because you are using a custom TableModel. If you would have created a proper SSCCE you would be using the DefaultTableModel so that you are testing your code with standard JDK classes. If you did this then you would have noticed that the code would work.
So then your next step would be to try the code with your custom TableModel and you would notice that the code did not work.
So then your question on the forum would be why doesn't the code work with my custom TableModel? The point of the SSCCE is to do basic debugging to isolate where the error is happening so we have information to work with. In your original question we had no idea you where using custom classes.
Anyway, the problem is that your custom TableModel is not notifying the table when a change to the data is made. In your refresh(...) method you need to add the following after you reset the List containing the data:
fireTableRowsInserted(0, names.size()-1);
There is no need for table.repaint() in any of your code.
Ok, so I think my problem here stems from a fundamental lack of understanding as to what a JList is actually doing.
I have a simple example class MyList.
package test;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class MyList extends JList<String> {
public MyList(Vector<String> myStrings){
super(myStrings);
this.setCellRenderer(new ListCellRenderer<String>(){
#Override
public Component getListCellRendererComponent(
JList<? extends String> list, String value, int index,
boolean isSelected, boolean cellHasFocus) {
String myString = value.toString();
return new JLabel(myString+" (idx "+index+")");
}
});
this.addMouseListener(new MouseAdapter(){
#Override
public void mousePressed(MouseEvent e) {
MyList p = (MyList) e.getSource();
Component c = p.findComponentAt(e.getX(), e.getY());
System.out.println("am clicking on "+c.getClass()+" in "+p.getClass());
}
});
}
public static void main(String[] args){
Vector<String> myStrings = new Vector<String>();
myStrings.addAll(Arrays.asList("str 1","str 2","str 3"));
MyList mine = new MyList(myStrings);
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(500, 500);
myFrame.add(mine);
myFrame.setVisible(true);
}
}
The output of clicking on any JLabel inside the list is the same:
am clicking on class test.MyList in class test.MyList
I expected it to be:
am clicking on class javax.swing.JLabel in class test.MyList
I think this as something to do with how the ListCellRenderer works, but I am not sure. Why isn't the component I get back from p.findComponentAt(e.getX(), e.getY()); a JLabel? What is going on here?
Why isn't the component I get back from p.findComponentAt(e.getX(), e.getY()); a JLabel? What is going on here?
JLists and JTables put in extra effort to efficiently display a lot of information while minimizing overhead doing so. One way they do this is by displaying information via renderers. What you are seeing in a JList are not true JLabels but images of JLabels (or whatever components are used as the renderer). The JList uses the same renderer to create images for each element in the column. So say you have a JList with 1000 items, rather than create 1000 JLabels, the JList creates just one, uses it to create JLabel images, and then displays the images. So what you are clicking on is truly a JList and not a JLabel.
To test this further, try using a JTextField as a renderer, and you'll quickly find that the cell does not behave like a JTextField and that you can't edit the information that the it displays.
Before I asked this question, I went through many examples, some of them are related, but did not answer my needs:
How to get the selected index of JCheckbox?
Java- Reading whether a checkbox is checked or not
I have a list as keyMessageList which includes id as Long and associated keyWord as String.
I displayed keyWord's name on my jpanel using following code:
for (KeyMessage obj : keyMessageList) {
checkBox = new JCheckBox(obj.getSmsKey());
displayKeywordPanel.checkBooxPanel.add(checkBox);
}
I got the following output:
Now, My requirement is: If I select keywords, I need to get the id associated with the selected keyword. I have done similar things many times in web applications like this, but now I need to do the same thing on swing. I used the following code on web application to fulfill the requirements.
Code for web:
<h:selectOneMenu id="branch" value="#{createUser.branchId}" required="true" requiredMessage="Please choose branch">
<f:selectItems value="#{allBranch}"/>
</h:selectOneMenu>
Can any swing expert please help me.
Note I may select multiple checkBox and keyMessageList returns from JPA query.
I have a list as keyWordsList which includes id and associated keyWord.
Don't use a List. Instead use a Map with the "keyword" as the key and the "id" as the value. Then when you select a check box you get the ID from the Map.
Another option would be to create you JCheckBox as follows:
JCheckBox checkBox = new JCheckBox(keyword);
checkbox.setActionCommand( id );
Then later you access the id of the selected checkbox by using the getActionCommand() method.
You can get displayKeywordPanel.checkBooxPanel.getComponents() array. Iterate through the array and store indexes. Then use the indexes to get elements from the keyMessageList
I think you can extend your own JCheckBox (let say JCheckBoxWithID) which allow you keep the id. Then use a List to store/remove ids everytime a checkbox is selected/unselected using an ItemListener
This way you'll be avoiding iterate over your checkboxPanel asking who is selected and keeping separated responsibilities:
JCheckBox just shows a keyword and holds an id
List just stores selected ids
ItemListener handles the event when a JCheckBox becomes selected/unselected and adds/removes its id to/from List
Hope this example be helpful:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Tests {
private void initGUI(){
/* This list will store selected ids */
final List<Integer> selectedIds = new ArrayList<>();
ItemListener itemListener = new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource() instanceof JCheckBoxWithID){
JCheckBoxWithID checkBoxWithID = (JCheckBoxWithID) e.getSource();
if(checkBoxWithID.isSelected()){
selectedIds.add(checkBoxWithID.getId());
} else {
selectedIds.remove(checkBoxWithID.getId());
}
}
}
};
String[] keyWords = new String[]{"Help 1", "Help 2", "Help 3", "Help 4", "Help 5", "Help 6"};
Integer id = 0;
JPanel checkBoxesPanel = new JPanel(new GridLayout(3,3));
/* Add many checkbox as you keywords you have */
for(String keyWord : keyWords){
JCheckBoxWithID checkBoxWithID = new JCheckBoxWithID(keyWord, id);
checkBoxWithID.addItemListener(itemListener);
checkBoxesPanel.add(checkBoxWithID);
id++;
}
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, Arrays.toString(selectedIds.toArray()), "Selected IDs", JOptionPane.INFORMATION_MESSAGE);
}
});
JPanel content = new JPanel(new FlowLayout(FlowLayout.LEADING));
content.add(checkBoxesPanel);
content.add(submit);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Tests().initGUI();
}
});
}
class JCheckBoxWithID extends JCheckBox {
/* I use Integer but the id could be whatever you want, the concept is the same */
private Integer _id;
public JCheckBoxWithID(String text, Integer id) {
super(text);
_id = id;
}
public void setId(Integer id){
_id = id;
}
public Integer getId(){
return _id;
}
}
}
Here's a print screen:
Why won't table cell 0,1 change from aaa to XXXX?
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
public static void main(String[] args) {
JFrame f = new JFrame("Refreshing JTable");
JPanel p = new JPanel(new GridBagLayout());
DefaultTableModel productsModel;
JTable productsTable;
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
}
REASON:
Apparently trying to update the array where data is stored will result in JTable not changing. Either DefaultTableModel needs to be updated or the whole table needs to be redrawn.
EDIT (possible Solution) One way is using Timer:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class MainFrame {
static JFrame f = new JFrame("Refreshing JTable");
static JPanel p = new JPanel(new GridBagLayout());
static DefaultTableModel productsModel;
static JTable productsTable;
public static void main(String[] args) {
runGui();
}
#SuppressWarnings("serial")
public static void runGui() {
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] tableTitle = new String[] {"ID", "Name"};
String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};
productsModel = new DefaultTableModel(tableData, tableTitle);
productsTable = new JTable(productsModel) {
public boolean isCellEditable(int r, int c) {
return false;
}
};
JScrollPane scrollpane = new JScrollPane(productsTable);
tableData[0][1] = "XXXX";
Timer t = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addColumns();
remakeData();
productsTable.setModel(productsModel);
}
});
t.start();
f.add(p);
p.add(scrollpane);
f.validate();
f.setVisible(true);
}
private static void addColumns() {
productsModel.setColumnCount(0);
productsModel.addColumn("ID");
productsModel.addColumn("Name");
}
private static void remakeData() {
productsModel.setRowCount(0);
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"1", "Dummy item 1"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"2", "Dummy itme 2"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"3", "Dummy item 3"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"4", "Dummy item 4"});
productsModel.insertRow(productsModel.getRowCount(), new Object[] {"5", "Dummy item 5"});
}
}
EDIT(much better solution, the way it worked for me flawlessly) Using a static method. Since I'm adding new data in array through another Frame, I created a static method in MainFrame, which I call every time I add/update/delete Object in array. This method will redo the whole model after update and will therefore refresh table.
One problem with the SSCCE posted on your related thread is that the code changes the array which originally formed the table model, whereas it should be changing the table model itself.
The solution to your problem (and the previous one) is rather simple, but unfortunately a bit too long to post it in a comment
You make sure you do some reading on how a JTable actually works. A good starting point is the Swing tutorial and perhaps even the class javadoc of the most important classes JTable and TableModel. Another good read about Swing and the use of MVC is this document. It is much broader then only JTables but will provide you with more insight in how all Swing components are designed, and how you should use them.
After reading that tutorial, you should understand that if you want to update the data which is shown in the table, you update the model of your table. The model will fire events which are received by the table, and the table will make sure it updates itself. After all, the table is only a view on the data (=the TableModel) so updates on the take should take place on the model side. If this is not clear, go back to step 1
Now take a look at your code and remove the listener. Instead, update the model on the table and see how the table updates itself.
If this does not work, you come back to this site with an SSCCE which you post. That way we can see that you tried something, what you tried and most likely what is wrong in your code. This SSCCE should include your code of the TableModel you use and the code that updates your TableModel since that is the most likely location for your problem.