0

I have created a class that inherits from JTextField. The instance of this class should be stored into a Hashtable, it also can store a JTable so the declaration is Hashtable . When I use the method hashCode() the JTextFields disappear except the first one. If I delete or comment the hashCode() method, it works. I have no idea what is heppening, all the code is very long and complex so I can't post it, It is generated by netbeans. Test this code you can see that not the equals textfield are displayed. This is the class:

         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
         import java.util.*;

    public class TextDemo extends JPanel implements ActionListener {
          protected static SelectableTextField textField;
          protected static SelectableTextField textField1;
          protected static SelectableTextField textField2;
          protected static SelectableTextField textField3;
          protected JTextArea textArea;
          private final static String newline = "\n";
          static int i = 0;

public TextDemo() {
    super(new GridBagLayout());

    textField = new SelectableTextField("sox", "name");
    textField1 = new SelectableTextField("sox", "surname");
    textField2 = new SelectableTextField("anam", "ali");
    textField3 = new SelectableTextField("general", "ali");
    textField.addActionListener(this);

    textArea = new JTextArea(5, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Add Components to this panel.
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.HORIZONTAL;
    add(textField, c);
    add(textField1, c);
    add(textField2, c);
            add(textField3, c);

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    add(scrollPane, c);
}

public void actionPerformed(ActionEvent evt) {
    String text = textField.getText();
    textArea.append(text + newline);
    textField.selectAll();

    //Make sure the new text is visible, even if there
    //was a selection in the text area.
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TextDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add contents to the window.
    frame.add(new TextDemo());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
            Hashtable<Object, String> h = new Hashtable<Object, String>();
            h.put(textField, "hello");
            h.put(textField1, "world");
            h.put(textField2, "holy");

        }
    });
}

class SelectableTextField extends javax.swing.JTextField {
   private String tablename = "";
   private String columnname = "";

   public SelectableTextField(String tabname, String colname) {
        super(Integer.toString((i++)));
    //this.addMouseListener(this);
       tablename = tabname;    //get table name
       columnname = colname;    //get column name
   }


@Override
public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof SelectableTextField))
        return false;
    SelectableTextField st = (SelectableTextField) o;
    return st.getTablename().equals(this.tablename);
}

@Override
public int hashCode() { 
    return tablename.hashCode();
}

public String getTablename() { return tablename; }
public String getColumnname() { return columnname; }
 }




  }
blob
  • 439
  • 8
  • 21
  • Are the `tablename`s for your `SelectableTextField`s the same or different? – d.j.brown Oct 23 '17 at 14:36
  • JTextFields could have the same tablename or not, The project is compiling without errors but when I play it the JtextFields disappear from the GUI if I implement the hashCode() method. – blob Oct 23 '17 at 14:38
  • in that case if I interpreted your question correctly, the likely reason you have only 1 is because they have the same key (due to the same hash code) and replacing the existing value. But it's difficult to say without details of your hashtable or how you're using `SelectableTextField`. – d.j.brown Oct 23 '17 at 14:42
  • JTextField already implements hashcode and equals. I see no reason to override the default implementation. – camickr Oct 23 '17 at 14:43
  • I use Hastable'<'Object, ArrayList'<'String'>''>'.I need that the text fields with the same tablename go into an array. So the equal method should compare tablename. The strange thing is that the text field disappear literally from the GUI if I implement hashCode() method. – blob Oct 23 '17 at 14:49
  • `I need that the text fields with the same tablename ` - so you have a method `getTableName` that you should be using. – camickr Oct 23 '17 at 14:57
  • as @camickr said, you should not be changing `equals` and `hashCode` to group all columns from one table into some container data structure. By making all columns from a given table use only `tablename` as their differentiator, this is what you get. Any hash based container will keep adding new objects into the place of the old one, as both `hashCode` and `equals` suggest that you are inserting updated version of the same object. – diginoise Oct 23 '17 at 16:53
  • 1
    What LayoutManager do you use? I suspect that the specific LayoutManager uses hash codes to keep track of components. – Markus Fischer Oct 24 '17 at 05:51
  • I have posted new code, the selectable text fields that are equals are not displayed, only one are displayed, you can test it. – blob Oct 25 '17 at 09:13
  • OK I have not read the previous comment. – blob Oct 25 '17 at 09:25

0 Answers0