-2

I've made a class that extends JTable class. I'm unable to call a public method that is not defined in JTable class. How can I do that? Here is the code of my class:

public class CustomJTable extends JTable
{
    private int id_mod = -1;
    private ModelloCampiLista mcl = ModelloCampiLista.getInstance();
    private TreeMap<Integer, DefaultCellEditor> tm_cbox = new TreeMap<>();
    private Map<String,ModelloCampi> tm;

    public CustomJTable(MyDefaultTableModel dtm)
    {
        super();
        setCustomModel(dtm);
    }

    public void setCustomModel(MyDefaultTableModel dtm)
    {
        super.setModel(dtm);
        update(dtm);
    }

    private void update(MyDefaultTableModel dtm)
    {
        id_mod = dtm.id_modello;
        tm = dtm.tm;
        composeTm_cbox();
    }

    private void composeTm_cbox()
    {
        TreeMap<Integer, ArrayList<String>> temp = mcl.getCBoxElements(id_mod);

        for (Map.Entry<Integer, ArrayList<String>> entry : temp.entrySet())
        {
            int map_key = entry.getKey();
            ArrayList<String> map_value = entry.getValue();
            String[] temp_jcb_el = new String[map_value.size()];

            for (int i = 0; i < map_value.size(); i++)
                temp_jcb_el[i] = map_value.get(i);

            JComboBox tmp_cbox = new JComboBox(temp_jcb_el);
            DefaultCellEditor temp_dce = new DefaultCellEditor(tmp_cbox);

            tm_cbox.put(map_key, temp_dce);
        }
    }

    @Override
    public TableCellEditor getCellEditor(int row, int column)
    {
        if (column == 1)
        {
            if (tm_cbox.getOrDefault(row, null) == null)
                return super.getCellEditor(row, column);

            return tm_cbox.get(row);
        }

        return super.getCellEditor(row, column);
    } 
}

I want to call the public method named setCustomModel but I can't see it.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    Are you getting any errors, or just a warning message from your IDE? To make the warning message go away, use `public final void setCustomModel` – 4castle Mar 21 '17 at 14:25
  • 1
    for me its working. what is the exact error? have you imported anything that you need? have you saved all classes? – XtremeBaumer Mar 21 '17 at 14:26
  • Possible duplicate of [Call a method of subclass in Java](http://stackoverflow.com/questions/2701182/call-a-method-of-subclass-in-java) – Tom Mar 21 '17 at 14:28
  • 1
    You say `I'm unable to call a public method that is not defined in JTable class` How do you know that you are not able to call the method, Can you add the details of the error message you see. – nits.kk Mar 21 '17 at 14:32
  • @Tom this is not duplicate as in the question its the sub class invoking the method defined by itself. – nits.kk Mar 21 '17 at 14:33
  • @nits.kk And this *"sub class invoking the method defined by itself"* is not the problem here. The issue is in the code OP haven't shown use (yet). – Tom Mar 21 '17 at 14:35
  • @Tom i agree :) but As said in the question its the issue with invoking the `setCustomModel` (see in the end of the question). This method is being invoked in the constructor body. – nits.kk Mar 21 '17 at 14:36
  • @nits.kk And that method call doesn't fail. That method is visible and the argument matches the parameter. That means that the issue is somewhere else. – Tom Mar 21 '17 at 14:42
  • @Tommaso Bernasconi , your question does not contain the code or contents of the problem you have. As you mentioned in the comments to the answer you have accepted, kindly update your question with the actual code where you get the error. Here on SO questions asked are not to solve just your issue but they also act as good reference for other users as well. Ask clear and well written questions, in case you miss it in the first go then you always have an option of editing your question. – nits.kk Mar 21 '17 at 15:33

1 Answers1

1

Where do you store the instance? If you are doing something like

JTable table = new CustomJTable(model);

then you are dropping the specification of the subtype for the variable, at runtime will be a CustomJTable but at compile time the variable has JTable type thus you can't call any specialized methods.

But I don't see the whole code and purpose, why do you need to use a custom setCustomModel? why setModel already provided by JTable is not enough?

As it is seen your design is really coupled, which is not a good thing in OOP.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • he wants to set the custom model in the constructor of `CustomJTable`. i think you are answering the question in a wrong way – XtremeBaumer Mar 21 '17 at 14:27
  • @XtremeBaumer: `JTable` already provides a constructor which accepts a `TableModel`. – Jack Mar 21 '17 at 14:28
  • @XtremeBaumer This method call wouldn't fail. – Tom Mar 21 '17 at 14:29
  • i know, but have you read his code? `public CustomJTable(MyDefaultTableModel dtm) { super(); setCustomModel(dtm); } public void setCustomModel(MyDefaultTableModel dtm) { super.setModel(dtm); update(dtm); }` is his problem – XtremeBaumer Mar 21 '17 at 14:29
  • 1
    This does not answer the question. If you would like to ask for clarification, please use the comments on the question. – 4castle Mar 21 '17 at 14:29
  • @Tom which method call do you mean? – XtremeBaumer Mar 21 '17 at 14:31
  • @4castle: I answered the question. The only possible error which doesn't allow you to call a public method on a subclass is that you are storing it in a variable declared as a super class type, there's no other possible reason (if argument types are correct). My second part was a request of clarification/purpose of the code since I wrote tenths of customized tables and never had to subclass `JTable` once. – Jack Mar 21 '17 at 14:31
  • 1
    @XtremeBaumer The method call to `setCustomModel`. It is visible in the mentioned constructor and OP said "he" can't see his method. – Tom Mar 21 '17 at 14:34
  • 1
    @Jack What makes you think there is an error? The OP didn't say so or show any error messages. This answer is way too premature, and it's asking for clarifications from the OP. – 4castle Mar 21 '17 at 14:36
  • @4castle: the OP is saying that he is not seeing the method which implies he's using an IDE which autocompletes methods when writing them. If he's not able to see it, it means that the IDE is not inferring the correct type for the variable he's trying to call the method onto. Which can happen basically just for the aforementioned reason (unless the IDE is broken). – Jack Mar 21 '17 at 14:37
  • @Tom i know that it wouldnt fail. i tried it myself – XtremeBaumer Mar 21 '17 at 14:38
  • 1
    @Jack From my interpretation of the question, they were talking about the code that's visible in the question, where the OP is asking about the warning message in the constructor. – 4castle Mar 21 '17 at 14:40
  • In the initComponents() method it's defined as JTable and not as CustomJTable, now it works, thanks – Tommaso Bernasconi Mar 21 '17 at 14:41
  • @XtremeBaumer Then all your other comments make no sense. You pretend that this call is the issue although you know it isn't? – Tom Mar 21 '17 at 14:42
  • @Tom would you explain to me why? The first comment says, what the code in the question shows. the second shows where exactly the OP thinks his problem is. I don't pretend that it is the issue. i say it is what OP says where the issue is and that the answer does not really help considering the code – XtremeBaumer Mar 21 '17 at 14:44
  • 1
    @4castle Then OP would have posted code which differs from the one he actually runs. – Tom Mar 21 '17 at 14:44
  • 1
    @Jack What I'm saying is, you've answered an unclear question that contains no error messages, description of the problem, nor shows the code that produces the error, which means that anyone browsing the site would find your answer completely unclear, because it makes assumptions on information the question didn't contain. – 4castle Mar 21 '17 at 14:46
  • @XtremeBaumer *"the second shows where exactly the OP thinks his problem is"* No, it shows there _you_ think the problem is. OP hasn't mentioned that. He just posted an incomplete example of his issue and the description "I want to call the public method named setCustomModel but I can't see it.". The constructor and the method call both look fine, so there is only one thing left which matches OPs description and this has been answered here. – Tom Mar 21 '17 at 14:47
  • @4castle: so shall we let the OP in his own problems? We're here to help people, not everyone is able to produce a well defined question, especially if english is not your mother language and you are not much experienced with programming. For me the question was clear and the answer was clear too so I don't see your point. – Jack Mar 21 '17 at 14:50
  • (1+) good logical guess of a simply stated question. I also thought that the OP was using a JTable as the instance variable instead of a CustomJTable when I read the topic title. I'm not sure why there are so many other comments in this posting. Let the OP decide if your assumption is correct or not. – camickr Mar 21 '17 at 15:18
  • I've solved it. I had forgotten to modify the initComponents method that netbeans generate when you use his gui to create jtable. Thanks to all for your help. – Tommaso Bernasconi Mar 27 '17 at 09:41