JTable renders removed JButton instead of a new one - java

The program in general is 3 arrays of a custom objects , with a JTable representing each array. I have a custom renderer and tablemodel. The custom object has inside it an object which is every visual element.
When I remove a row from 1 table and replace , rerender it as it is empty it renders perfectly , but when I add a new row to the table it renders properly JTextField and JLabel , but renders JButtons from previous object.
Table model
public class PositionTableModel extends AbstractTableModel
{
private List<kosilkshik.Position> local;
public PositionTableModel(List<kosilkshik.Position> list)
{
local = list;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return true;
}
#Override
public int getRowCount(){return local.size();}
#Override
public int getColumnCount() {return 24;}
#Override
public Object getValueAt(int rowIndex, int columnIndex)
{
kosilkshik.Position pos = local.get(rowIndex);
switch (columnIndex)
{
case 0:
return pos.view.symbols.get(0);
case 1:
return pos.view.ratios.get(0);
case 2:
return pos.view.symbols.get(1);
case 3:
return pos.view.ratios.get(1);
case 4:
if(pos.view.symbols.size()>2)
{
return pos.view.symbols.get(2);
}else{return null;}
case 5:
if(pos.view.symbols.size()>2)
{
return pos.view.ratios.get(2);
}else{return null;}
case 6:
if(pos.view.symbols.size()>3)
{
return pos.view.symbols.get(3);
}else{return null;}
case 7:
if(pos.view.symbols.size()>3)
{
return pos.view.ratios.get(3);
}else{return null;}
case 8:
return pos.view.name;
case 9:
return pos.view.qt;
case 10:
return pos.view.current;
case 11:
return pos.view.buy;
case 12:
return pos.view.sell;
case 13:
return pos.view.bid;
case 14:
return pos.view.avg;
case 15:
return pos.view.ask;
case 16:
return pos.view.mkt;
case 17:
return pos.view.p;
case 18:
return pos.view.flip;
case 19:
return pos.view.invert;
case 20:
return pos.view.control;
case 21:
return pos.view.control1;
case 22:
return pos.view.control2;
case 23:
return pos.view.control3;
default:
return null;
}
}
}
TableRender does not have any caching manually added.
Method for removing rows from table
AbstractTableModel m =(AbstractTableModel)tableS.getModel();
m.fireTableRowsDeleted(row,row);
Method for inserting rows:
AbstractTableModel m =(AbstractTableModel)tableS.getModel();
m.fireTableRowsInserted(0, suggested.size()-1);

Method for removing rows from table...,
Method for inserting rows...
Those two methods should be part of the custom TableModel, not part of your application code. It is the responsibility of the TableModel to notify the table when the data changes. So you need to create method for you model like "addRow(...)" and "removeRow(...)".
Make sure the "row" values you pass to the fireXXX methods are correct. The DefaultTableModel implements addRow(...) and removeRow(...) method, so check out the source code of the DefaultTableModel to see how that code works.

Related

ListFragment inside viewPager does not display data in some pages and works well in some pages

I have a ListFragment that fetches json data from the internet and displays in a listView.And i reuse this fragment in a viewPager.It loads data and displays in first two pages but keeps loading in the third page.Most pages work but some pages in between does not work.What i dont understand is that i use same fragment across all pages but does not work in some pages.I reuse the same fragment by creating a newInstance and passing the url to fetch data from.If i provide more code the viewers get overwhelmed so i guess this is enough.Are there any rules that i should follow while fetching and display list in ListFragments.Am i doing something wrong?
My adapter:
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Log.d("Adapter","Case 0 called");
return new LisViewFragment2();
case 1:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=al-jazeera-english&sortBy=top&apiKey=my_key");
case 2:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=associated-press&sortBy=top&apiKey=my_key");
case 3:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=my_key");
case 4:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=cnn&sortBy=top&apiKey=my_key");
case 5:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=my_key");
case 6:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=independent&sortBy=top&apiKey=my_key");
case 7:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=metro&sortBy=top&apiKey=my_key");
case 8:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=mirror&sortBy=top&apiKey=my_key");
case 9:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=newsweek&sortBy=top&apiKey=my_key");
case 10:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=new-york-magazine&sortBy=top&apiKey=my_key");
case 11:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=reddit-r-all&sortBy=top&apiKey=my_key");
case 12:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=reuters&sortBy=top&apiKey=my_key");
case 13:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-guardian-uk&sortBy=top&apiKey=my_key");
case 14:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-hindu&sortBy=top&apiKey=my_key");
case 15:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-times-of-india&sortBy=top&apiKey=my_key");
case 16:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-new-york-times&sortBy=top&apiKey=my_key");
case 17:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=the-telegraph&sortBy=top&apiKey=my_key");
default:
return ListViewFragment.newInstance("https://newsapi.org/v1/articles?source=usa-today&sortBy=top&apiKey=my_key");
}
}
#Override
public int getCount() {
return 19;
}
}
override getItemPosition and return POSITION_NONE
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

Returning child object from parent class type method

I am trying to write a method that creates random plants and returns the object of created plant as Plant. In example below the method createPlant() is of type Plant and returns an object of child class Tree. As it turns out my way of thinking is erroneous. The error provided by Eclipse is: "This method must return a result of type Plant". So how should I go about creating such method?
public abstract class Plant {
...
}
public class Tree extends Plant {
...
}
public class Bush extends Plant {
...
}
public class Map {
private Plant plant;
...
public static Plant createPlant(float x, float y) { // This method must return a result of type Plant
Random generator = new Random();
switch (generator.nextInt(2)) {
case 0:
return new Tree(x, y);
case 1:
return new Bush(x, y);
}
}
}
Add default case of null.
switch (generator.nextInt(2)) {
case 0:
return new Tree(x, y);
case 1:
return new Bush(x, y);
default: // Requires default case
return null;
}
Or create a dummy NoPlant class
public class NoPlant extends Plant {
...
}
Now use in this way
switch (generator.nextInt(2)) {
case 0:
return new Tree(x, y);
case 1:
return new Bush(x, y);
default: // Requires default case
return new NoPlant();
}
--EDIT--
Try in this way also
int random=generator.nextInt(2); // return either 0 or 1
if(random==0){
return new Tree(x,y);
}else{
return new Bush(x, y);
}
No, from an object oriented perspective this is absolutely okay. A plant is a general entity, whereas the tree is specialized in some way.
The main point is that the tree is also a plant (is-a-relation) so a method returning a plant can return anything that is at least as general as a plant but also may be more specialized.

jtable checkbox with action binding [duplicate]

This question already has answers here:
JTable with editable checkbox
(2 answers)
Closed 8 years ago.
hi my java swing project have a table with last column is boolean values..i changed it into chceckbox.but i need to bind event on it and know if it is check or not..!!
Below Code works it is showing checkbox
retunTable=new JTable(model){
private static final long serialVersionUID = 1L;
/*#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}*/
#Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Object.class;
case 1:
return Object.class;
case 2:
return Object.class;
case 3:
return Object.class;
default:
return Boolean.class;
}
}
};
i dont know where to put addActionListener..!!
Help needed..!!
i dont know where to put addActionListener..!!
TableCellEditort/Renderer isn't real JComponent
override setValueAt in XxxTableModel
I woudln't suggest to use custom TableCellEditort/Renderer for this job
override getColumnClass in XxxTableModel instead for subclassing JTable

Implementing ArrayLists to Table models

Hi I have an arraylist of a class I created called Pets which has the variables below
private String name;
private String species;
private int age;
I wanted to display this arraylist into a jTable and I did that succesfully by using defaultTableModel and calling setModel().
However I needed to add a sorting and filtering function for the Jtable. I took a look at the java tutorials were they were creating a subclass of AbstractTableModel in order to sort and filter. However they were using arrays to store the data. So I tried modifying the code to use an arraylist isntead but Im stuck with this method
public Object getValueAt(int row, int col) {
return data[row][col];
}
How do I get all the values from one object from th arraylist?
Any help will be greatly appreciated. Thanks in advance.
Does your ArrayList hold a row that is it's own type of object? If so, and if your ArrayList is a generic ArrayList<RowItem> then you could do something like:
#Override
public Object getValueAt(int row, int col) {
if (row > getRowCount()) {
// throw an exception
}
RowItem rowItem = rowItemList.get(row);
switch (col) {
case 0:
return rowItem.getName();
case 1:
return rowItem.getLastSpecies();
case 2:
return rowItem.getAge();
}
return null; // or throw an exception
}
You can try this:
public Object getValueAt(int row, int col) {
switch(col) {
case 0:
return ((Pets)data.get(row)).getName();
case 1:
return ((Pets)data.get(row)).getSpecies();
case 2:
return ((Pets)data.get(row)).getAge();
}
return null;
}

Disable user edit in JTable [duplicate]

This question already has answers here:
How to make a JTable non-editable
(7 answers)
Closed 1 year ago.
When a JTable component is created, cell editing is enabled by default. How can I prevent the user from editing the content of a JTable?
You can create a JTable using following code:
JTable jTable = new JTable() {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
return false;
};
};
Basically what we are doing here is overriding isCellEditable and always returning false from it. This will make a non editabe JTabel.
A JTable uses an AbstractTableModel object. This is the thing you pass into the constructor of the JTable. You can write your own AbstractTableModel as follows
public class MyTableModel extends AbstractTableModel {
public boolean isCellEditable(int row, int column){
return false;
}
}
and then initialize your JTable as
JTable myTable = new JTable(new MyTableModel());
myTable.setDefaultEditor(Object.class, null);
Have you tryed simply:
JTable table = new JTable();
table.setEnabled(false);
About JComponent.setEnabled(boolean) it sayes:
Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.
When it comes to JTable it doesnt seem to give any visual feedback at all. With the perk of still being able to click on the column headers. And in my implementation the application could still change the contents of the cells.
Hi I'm working a lot on java so I'm gonna give you my way:
There are two possibilities the first under netbeans. Go to customize code and make it like this:
JTArticleJPAddArrticle = new javax.swing.JTable();
JTArticleJPAddArrticle.setBackground(new java.awt.Color(204, 204, 255));
JTArticleJPAddArrticle.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Reference","Libellé","Marque","Prix d'achat","Prix de vente","Quantité","Total","Etat"
}
){
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
});
jScrollPane8.setViewportView(JTArticleJPAddArrticle);
My other way is to do it is to make an instance of the table model. This is the second way:
model=new DefaultTableModel(head, 0){
#Override
public boolean isCellEditable(int i, int i1) {
return false; //To change body of generated methods, choose Tools | Templates.
}
};
jtable.setmodel(model);
Enjoy this is working well for me. All I want to do is help you guys out because I was helped out a lot earlier.
Well on netbeans you can right click on the table and click on table contents, then you go to the column tab and uncheck the "Editable" checkbox. Greetings!!
I know I am late but hope someone get use of this. You can simple add mouse listener like this:
jtable.addMouseListener( new MouseAdapter () {
#Override
public void mouseClicked ( MouseEvent e ) {
columnIndex = replacedAssets.getSelectedColumn ();
System.out.println ( "Double click on jtable" );
if ( columnIndex == 1 || columnIndex == 2 ) {
JOptionPane.showMessageDialog ( parent , "Editing this Field may cause error in the data." , "Error Edit Not Permitted For This Field" , JOptionPane.ERROR_MESSAGE );
}
}
});
this code prevent editing the columns of indexes 1 and 2 you can remove the if condition to make this work for all columns.
tm = new javax.swing.table.DefaultTableModel()
{
public Class<?> getColumnClass(int column)
{
switch(column)
{
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return String.class;
case 6:
return String.class;
case 7:
return String.class;
case 8:
return String.class;
case 9:
return String.class;
case 10:
return String.class;
case 11:
return Boolean.class;
default:
return String.class;
}
}
#Override
public boolean isCellEditable(int row, int column) {
/* Set the 11th column as editable and rest non-
editable */
if(column==11){
return true;
}else{
//all other columns to false
return false;
}
}
};
table = new javax.swing.JTable(tm);
In this method "isCellEditable" we can enable and disable user edit for particular column. In this case enable column=11 and disable rest of the column

Categories