6 Replies Latest reply: Oct 7, 2004 1:42 PM by 807587 RSS

    Two JTables

    807587
      Situation: I have two JTables. Now I have selected in one JTable a row and a column and also in the other.
      At some point I want to know the current cell and row in a JTable. Now that works, so that isn't the problem.
      But how do I know which JTable is the selected one? or the default or has the focus (i don't know how you call that)

      Thanks
        • 1. Re: Two JTables
          807587
          please?
          How do I know what JTable is selected?
          • 2. Re: Two JTables
            807587
            What do you mean by "selected" table? Table that has focus? What if neither has focus, or one has
            focus and then you press the "do something to focused table" button only to find that the focused
            component is that damn button! Are you using any particulat listeners? What is your goal, what are you
            trying to do?
            • 3. Re: Two JTables
              807587
              I have two JTables. One left and one right. Both JTables contain private financial information from a database. Because I have a lot of attributes (SQL columns) in the database I have created a JTable with the most common attributes (SQL columns) say 4 columns each JTable.
              Because I want to be able to edit fields which aren't in the JTable, I created a form which gets called from a pulldownmenu.
              What I want and what the problem is that when I select ->the left <- JTable it has to give me the primary key readed from the left JTable (first column from left JTable). When I select the right JTable it has to give me the primary key readed from the right JTable (first column from right JTable).
              That is the problem. I can only use the action from the pulldownmenu on the right JTable.
              class Mainmenu extends JFrame implements WindowListener, ActionListener
              {
                  JTable minusjtable, plusjtable;
              
                  ... 
                  // initialize the database 
                  // filled the jtable from the database
                  // initialize pulldownmenu and put it above
                   ....     
                  public void actionPerformed(ActionEvent a) 
                   {
                        if (a.getSource() == menu_edit) {
                                        // here it needs to find out what JTable has focus
                                        new EditFormWithTextFields(data.returnPrimaryKey(1));   // an int
                   }
              }         
              In this example the statement
               
              new EditFormWithTextFields(data.returnPrimaryKey(1));
              comes with a form window with textfields with data from the left (all finanical minus transactions) and the following statement comes with a form window with textfields from the right.
               
              new EditFormWithTextFields(data.returnPrimaryKey(2));
              I don't know the word for debet/credit in english, so i called it plus and minus, sorry.
              Now I have to change the 1 into 2 and so on and compile the application everytime I want to switch from column. There must be a easier way for the user. So I wondered is it possible to use something like
              boolean b;
              
              left = minusjtable.isFocused();   // or something
              right = plusjtable.isFocus(); // or something
              new EditFormWithTextFields(data.returnPrimaryKey( (left == true ? 1 : 2)));
              You would kindly help me.
              • 4. Re: Two JTables
                807587
                I have two JTables. One left and one right. Both JTables contain private financial information from a database. Because I have a lot of attributes (SQL columns) in the database I have created a JTable with the most common attributes (SQL columns) say 4 columns each JTable.
                Because I want to be able to edit fields which aren't in the JTable, I created a form which gets called from a pulldownmenu.
                What I want and what the problem is that when I select ->the left <- JTable it has to give me the primary key readed from the left JTable (first column from left JTable). When I select the right JTable it has to give me the primary key readed from the right JTable (first column from right JTable).
                That is the problem. I can only use the action from the pulldownmenu on the right JTable.
                class Mainmenu extends JFrame implements WindowListener, ActionListener
                {
                    JTable minusjtable, plusjtable;
                
                    ... 
                    // initialize the database 
                    // filled the jtable from the database
                    // initialize pulldownmenu and put it above
                     ....     
                    public void actionPerformed(ActionEvent a) 
                     {
                          if (a.getSource() == menu_edit) {
                                          // here it needs to find out what JTable has focus
                                          new EditFormWithTextFields(data.returnPrimaryKey(1));   // an int
                     }
                }         
                In this example the statement
                 
                new EditFormWithTextFields(data.returnPrimaryKey(1));
                comes with a form window with textfields with data from the left (all finanical minus transactions) and the following statement comes with a form window with textfields from the right.
                 
                new EditFormWithTextFields(data.returnPrimaryKey(2));
                I don't know the word for debet/credit in english, so i called it plus and minus, sorry.
                Now I have to change the 1 into 2 and so on and compile the application everytime I want to switch from column. There must be a easier way for the user. So I wondered is it possible to use something like
                boolean b;
                
                left = minusjtable.isFocused();   // or something
                right = plusjtable.isFocus(); // or something
                new EditFormWithTextFields(data.returnPrimaryKey( (left == true ? 1 : 2)));
                I know it is a long post but, you would kindly help me.
                • 5. Re: Two JTables
                  807587
                  Okay, I'm not sure, but are you saying "selected table" = "focused table"? You have to careful with focus.
                  You mentioned you want to do something when a menu item is clicked -- but then it has the focus.
                  Let me suggest that the "selected table" is initially the left table (or right, your choice) and from there, the
                  "selected table" is the last of those two tables to gain focus. Write a FocusListener that is added
                  to those two tables and just maintains a "last focused table" reference. Then in your menu item handler,
                  you can ask the focus listener which table last had focus...
                  • 6. Re: Two JTables
                    807587
                    It works! Thanks for helping and still finding out the problem.
                    I will show you the code.
                         Boolean leftColumnSelected = true;
                    
                         plusjtable.addFocusListener(this);
                         minusjtable.addFocusListener(this);
                         ....
                    
                         public void focusGained(FocusEvent e)
                         {     
                              if (e.getSource() == t1) leftColumnSelected = false;
                              if (e.getSource() == t2) leftColumnSelected = true;
                         }
                    
                         public void focusLost(FocusEvent e) 
                         {
                              
                         }
                    I added focus to the two jtables and it works! I can't believe it!
                    Your brilliant