swing tableChanged() not responding to table changes - java

I'm coding in Swing in Java. I'm using the Netbeans layout manager. I'm having trouble with a jTable. I've applied a customer model to it which extends AbstractTableModel. I want the third column to contain boolean values in the form of checkboxes (this I have done successfully). The dialog I have the jTable in implements TableModelListener. My tableChanged() method has only the following code: System.out.println("Table changed!");
However, whenever I try to check one of the checkboxes, it does the little "suppression" thing when I click and hold, then when I release, it doesn't change the checkbox's state. It also does not print out "The table has changed!" This has been driving me crazy. I've read all about it, but can't figure out why mine's not working. Please help. Here's the relevant code:
In jDialog constructor:
this.chapterTableModel = new ChapterTableModel(chapterList);
chapterTableModel.addTableModelListener(this);
And then a method which does this: chapterTable.setModel(chapterTableModel);
Then below my constructor, I have this method:
#Override
public void tableChanged(TableModelEvent tme) {
System.out.println("Table Changed!");
}
The entire code can be found here: http://collabedit.com/ttcds
and here: http://collabedit.com/qn3kx
Thanks for your help in advance!

Are you calling one of the fire-methods of the parent AbstractTableModel class in the mutators of ChapterTableModel?

You are not overriding setValueAt anywhere so the value in your table isn't being changed.

Related

Handling button event in main class

I'm new to Java and programming. I want to generate an int in a JDialog class by selecting a row in a table and send that int to the main class which created it, or, to handle the button action performed in the main class. Main class creates the subclass here:
private void launchtable() {
DlgBeds dcl = new DlgBeds();
dcl.setLocationRelativeTo(this);
dcl.setVisible(true);
// I want in this place to put something like this:
txtCode.setText(subclassgeneratedint );
}
But first I need to get that int from the subclass., I tried creating a method there that I can use in main to return the int, but it instead sets the text with the first row selected (the default selection), and it can't return more row selections to keep changing txtCode; the method like just dies there.
The main could also have an action performed for the button in the subclass but the parameter would need to be one from the subclass I guess.
protected void actionPerformed[button in the subclass](ActionEvent e) {
//actions
}
I'm really lost, I know I need to study more to solve situations like this but I needed the answer as fast as possible.
Add a ListSelectionListener to your table. You can get the selected row as shown here and pass it to your main window using a PropertyChangeListener as shown here and here.

Calling a method relating to an object in an arrayList through clicking a specific label

I am trying to implement a feature in a project that I am working on but I am having dificulties. The project allows the user to create 3 different objects that all share the same super class. Each object is part of an arrayList and is represented by an ImageIcon inside of a JLabel. I would like to be able to click a specific JLabel and open a message dialog with a toString() method that returns information about the coresponding object.
So far, I have a (poorly implemented) system in place that will allow the user to click any ImageIcon but it will only display information about the most recently created object. I am aware why this code only displays the information it does but I do not know how create the code that I need.
If anyone can help I would be very grateful. If anything is poorly explained or needs elaborating on, please ask. I have attached my current code below, Thank you.
Code explanation: The 'count' variable is used to count the number of objects created (I cannot have more that 9). I know that the current code will just display the 'count-1' object created (which is the newest one). I'm just un aware of what I need to do to find the specific object relating to the lable that is clicked.
label[count].addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(null, myAppliances.get(count-1).toString());
}});
Each object is part of an arrayList and is represented by an ImageIcon inside of a JLabel.
That gives a good hint to it. How about doing following
Class MyObjectLabel extends JLabel something like below:
public MyObjectLabel extends JLabel
{
private YourObjectThatisInList localCopyOfObject;
public MyObjectLabel (YourObjectThatisInList object)
{
super(createIconForTheObject(object));
this.localCopyOfObject=object
}
//add getter setter method for localCopyOfObject
}
2) Now add listener to this class.
3) On event fired of this new Label class, call the getter for localCopyOfObject and display the toString for your localCopyOfObject stored in MyObjectLabel.
Please note that createIconForTheObject, is just a placeholeder method I showed. You can use your own method to create icon
Thanks

Transfer Items from One JComboBox to another JComboBox

Hi I'm trying to transfer one object from one combobox to another and vice versa.
To accomplish this,
I use actionListeners or ItemListeners, to no luck that they dont answer my problems, or maybe there's just something wrong in my implementation.
Assuming we have to comboboxes, combobox1 and combobox2.
Basically,
1. I first add the selected item to another combobox (combobox2)
2. I remove the selected item on the first combobox (combobox1)
When trying to debug this, i found out that everytime i am on the step of removing items, the listener of the other combobox fires, which does the same steps as above. This results into a loop, that just deletes the item, and places it back to the original combobox.
When using the ItemListener, with the proper if conditions of being selected or not, it throws a bigger error. Guys please help me..
*on edit mode/currently making an SSCE
Found this, as suggested by sir mKorbel.
It did the trick, setting the model via setModel(DefaultComboBoxModel model) method doesnt trigger the ActionListener when it tries to add the contents of the model passed, versus the addItem(Object obj) method that fires the ActionListener causing the havoc that i described on my question above.
Thanks guys, and i learned about a new thing called DefaultComboBoxModels!
jComboBox12.removeAllItems();
for (int t = 0; t < jComboBox11.getItemCount(); t++)
{
jComboBox12.addItem(jComboBox11.getItemAt(t));
}

JTable runtime error

I have set a JTable to initialize at the beginning of the program, and I set it to a DefaultTableModel:
public DefaultTableModel usernameScraperTableModel = new DefaultTableModel();
private JTable usernameScraperTable;
I then initialize the table inside the constructor as below,
this.usernameScraperTable = new JTable(this.usernameScraperTableModel);
this.usernameScraperTableModel.addColumn("Username");
and then in my actionPerformed(ActionEvent) for my startButton, I run a method. If something is collected, it is supposed to add it to the table.
this.usernameScraperTableModel.addRow(new Object[] { user.getName() } );
It works somewhat. During run-time, the table is blank and doesn't show anything. I know this because in my Eclipse console it's working like it's supposed to. But after the method is done running, it then has the JTable update and all of the names are there.
I'm wondering how I can have my table update in real time inside of an ActionEvent of a button.
EDIT
Thanks for this. It still doesn't work though. I used WindowBuilder from the Eclipse website, and it's a JFrame. When I click start, the GUI kind of "freezes" (I can't click anything else, can't press the exit button, anything) until the method is done with
So I believe you want the method fireTableDataChanged() which is part of DefaultTableModel, which is usernameScraperTableModel in your case

Cast Object to JTable?

I am trying to implement a ListSelectionListener for some of my JTables. Simply (at the moment) the ListSelectionListener is supposed to simply return the text of the cell that was selected.
My program design has several JTables and I would like to have one ListSelectionListener work for them all. In the valueChanged event of the ListSelectionListener I thought it was possible to do something like:
private class SelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e)
{
JTable table = (JTable)e.getSource();
String data = (String) table.getValueAt(table.getSelectedRow(), 0);
// Print data
}
}
Behind the scenes I have used the following code to get the SelectionHandler working with the table in question:
fbTable.setCellSelectionEnabled(true);
ListSelectionModel cellSM = fbTable.getSelectionModel();
cellSM.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(selectionHandler);
When I run the program I get a ClassCastException error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.DefaultListSelectionModel cannot be cast to javax.swing.JTable
at cardboardfantasy.CardboardFantasyView$SelectionHandler.valueChanged(CardboardFantasyView.java:360)
// This is the line in question: JTable table = (JTable)e.getSource();
Is there a way to do something like this? One solution I thought of was to compare the source of the event (e.getSource()) to all my JTables to see if they were equivalent (big if block) and then just calling .getValueAt inside that block but that would making the code in the future difficult if tables were to be added or removed.
Either debug your code in your IDE, set a breakpoint and see what the type of e.getTarget() is:
Object source = e.getSource();
JTable table = (JTable)source; // breakpoint on this line and inspect the variable 'source'
String data = (String) table.getValueAt(table.getSelectedRow(), 0);
Or if debugging is not possible for whatever reason do this:
Object source = e.getSource();
System.out.println(source.getClass());
But: debugging using System.out.println is evil. your debugger is your friend.
As the error implies, the source object in question is a DefaultListSelectionModel not a JTable. This makes sense since the source of the event (that is, the object which fired the event) was the selection model object, not the table. Also, models in themselves make no assumptions about what type of object is using them as a model so there is no way to get a reference to the table via the selection model.
Pass the JTable instance to your selection handler. As long as the handler listens on one table, you'll be able to use that instance instead of relying on the information from the event.
I think there are two main solutions:
Use a JList and register the listener not with model but directly with the list. Then, if the list is contained by a table you could just ask for the list's (Component) parent to find the responsible table
Override DefaultListSelectionModel to (for example) take an additional argument in the constructor, which would be a JTable instance (every table will need a new instance of that model). You would save that instance in an instance variable and could then operate directly on the table when an event occurrs
I do not think that either of these solutions is ideal. I have the feeling that you could make your life easier by using some pattern or idiom to get around having to know which table the source was. But to give you any clues there we'd have to see a lot more of your code.

Categories