Alternate Selection between 2 JTable

Hello Experts,
assume that i have 2 JTables in JPanel and each contain one row is it possible to alternate selection between these tables
in other words if i select the first table then after that i select the second table the first table will be unselected and reverse
Thanx

Thank you camickr for your reply,
I try with Focus Listener
and this is my attempt
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.util.*;
class JTables extends JFrame implements FocusListener {
   int counter = 0;
   JTable previousTable     = null;
   JTable table1 = new JTable();
   JTable table2 = new JTable();
   LinkedList<JTable> x = new LinkedList<JTable>();
   public JTables()
        setBounds(0,0,500,500);
        JPanel pane = new JPanel();
        pane.setLayout(null);
        setContentPane(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setBounds(0,0,500,500);
        String [] cols = {"Col1","Col2","Col3"};
        Object [][] rows = {{"data1","data2","data3"}};
        table1 = new JTable(rows,cols);
        table2 = new JTable(rows,cols);
        JScrollPane scroll1 = new JScrollPane(table1);
        JScrollPane scroll2 = new JScrollPane(table2);
        scroll1.setBounds(10,10,300,50);
        scroll2.setBounds(10,100,300,50);
        table1.setColumnSelectionAllowed(true);
     table1.setRowSelectionAllowed(true);
     table2.setColumnSelectionAllowed(true);
     table2.setRowSelectionAllowed(true);
     table1.addFocusListener(this);
    table2.addFocusListener(this);
    for(int i=0;i<3;i++)
        TableColumn tm1 = table1.getColumnModel().getColumn(i);
        TableColumn tm2 = table2.getColumnModel().getColumn(i);
        tm1.setCellRenderer(new cellRenderer());
        tm2.setCellRenderer(new cellRenderer());
    x.add(table1);
    x.add(table2);
   pane.add(scroll1);
   pane.add(scroll2);
   class cellRenderer extends DefaultTableCellRenderer
        @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
                 Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                 setHorizontalAlignment(SwingConstants.CENTER);
                 int [] selectedRows = table.getSelectedRows();
                 boolean selected = false;
     if (selectedRows != null)
           for(int i=0; i < selectedRows.length; i++)
                 if (selectedRows[i] == row )
                             selected = true;
                             break;
            if(selected)
                    comp.setBackground(Color.BLACK);
     comp.setForeground(Color.WHITE);
     comp.repaint();
                previousTable = table;
             return comp;
    class cellRenderer1 extends DefaultTableCellRenderer
        @Override
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
               Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
               setHorizontalAlignment(SwingConstants.CENTER);
               int [] selectedRows = table.getSelectedRows();
            boolean selected = false;
               if (selectedRows != null)
                    for(int i=0; i < selectedRows.length; i++)
                         if (selectedRows[i] == row )
                             selected = true;
                             break;
            if(selected)
                    comp.setBackground(Color.WHITE);
                    comp.setForeground(Color.BLACK);
                    comp.repaint();
               return comp;
    public void focusGained(FocusEvent e)
             for(int i=0;i<3;i++)
                if(table1.getSelectedRows().length > 0)
                    TableColumn tm1 = table1.getColumnModel().getColumn(i);
                    tm1.setCellRenderer(new cellRenderer());
                else
                    TableColumn tm1 = table2.getColumnModel().getColumn(i);
                    tm1.setCellRenderer(new cellRenderer());
     public void focusLost(FocusEvent e)
             for(int i=0;i<3;i++)
                if(table1.getSelectedRows().length == 0)
                    TableColumn tm1 = table1.getColumnModel().getColumn(i);
                    tm1.setCellRenderer(new cellRenderer1());
                else
                    TableColumn tm1 = table2.getColumnModel().getColumn(i);
                    tm1.setCellRenderer(new cellRenderer1());
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            new JTables().setVisible(true);
}but not work only one cell from the other unselected

Similar Messages

  • Multiple Cell Selection in a JTable (again...)

    Hi All -
    I'm trying to get a JTable working well with the ability to select multiple cells in the way you'd expect to be able to (in Excel for example). By this I mean, hold down ctrl to add to a selection, hold shift to select between the last selection and the new one, be able to drag multiple regions with the mouse and have them be selected.
    I've seen lots of talk about this, for example on this thread:
    http://forum.java.sun.com/thread.jspa?threadID=619580&start=0&tstart=0
    (the code here will not work in the 'general' case, for example it doesn't really support dynamic table resizing)
    ...and found some pretty extensive code from here:
    http://www.codeguru.com/java/articles/663.shtml
    ...that kinda half works. (but the selection model is very strange once you get it running. Not at all what the average user would be used to)
    Does anyone have this working 100% with code they can share? It is very surprising to me that this is not the default behavior in Swing to be honest.
    I'm sure that I can come up with a solution for my situation, but I may as well not waste the time if someone already has something working for this.
    Thanks!

    If you have columnSelectionAllowed,rowselectionAllowed and cellSelectionEnabled then you can select cell by cell if you use the control key,
    Now the problem is, if you don't have input by your keyboard, in my case, I have a big table and I need to select diferent cells that match a certain criteria,
    jTable2.setRowSelectionInterval(j,j); //but this only works not for different rows 5 - 120 - 595
    I'm trying to use:
    jTable2.changeSelection()
    but still don't have good results....
    If any one could help to this matter will be really appreciated...
    thanks...

  • Selection Problem with JTable

    Hello,
    i have a selection problem with JTable. I want to allow only single cell selection and additionally limit the selection to the first column.
    I preffered the style from MS Outlook Express where you can select the email accounts to edit.
    It is a table like this:
    Account name  |   Type  |   ...
    --------------|---------|---------------------
    Hotmail       |   POP3  |
    GMX           |   IMAP  |The selection should be only avaibable at 'Hotmail' or 'GMX' - not at 'POP3', 'IMAP' or as complete row selection.
    Please help me!
    Thanks.
    Warlock

    Maybe this will helpimport java.awt.*;
    import javax.swing.*;
    public class Test3 extends JFrame {
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One", "Two"};
        String[][] data = {{"R1-C1", "R1-C2"}, {"R2-C1", "R2-C2"}};
        JTable jt = new JTable(data, head);
        jt.getColumnModel().setSelectionModel(new MyTableSelectionModel());
        content.add(new JScrollPane(jt), BorderLayout.CENTER);
        jt.setCellSelectionEnabled(true);
        jt.setRowSelectionAllowed(false);
        jt.setColumnSelectionAllowed(false);
        setSize(300, 300);
        setVisible(true);
      public static void main(String[] arghs) { new Test3(); }
    class MyTableSelectionModel extends DefaultListSelectionModel {
      public void setSelectionInterval(int index0, int index1) {
        super.setSelectionInterval(0, 0);
    }

  • How to get the default selection color from JTable

    Hi, there,
    I have a question for how to get the default selection color from JTable. I am currently implementing the customized table cell renderer, but I do want to set the selection color in the table exactly the same of default table cell renderer. The JTable.getSelectionBackgroup() did not works for me, it returned dark blue which made the text in the table unreadable. Anyone know how to get the window's default selection color?
    Thanks,
    -Jenny

    The windows default selection color is dark blue. Try selecting any text on this page. The difference is that the text gets changed to a white font so you can actually see the text.
    If you don't like the default colors that Java uses then use the UIManager to change the defaults. The following program shows all the properties controlled by the UIManager:
    http://www.discoverteenergy.com/files/ShowUIDefaults.java
    Any of the properties can be changed for the entire application by using:
    UIManager.put( "propertyName", value );

  • In Mountain Lion how does OS select between ethernet and WiFi when both are activated??

    In Mountain Lion how does OS select between ethernet and WiFi when both are activated??  How does OS use "both" networl accesses??

    It will pick the highest service in the list which has an internet connection.
    To order the list, open Network System Preferences and select Set order of network services from the button below the service list.

  • Drag and Drop between two JTables

    I tried to implement drag and drop between two JTables using data transfer, but i did not get it to work, that dnd works in both directions. i have my own transferhandler, but if is use setTransferHandler(handler), i'm no longer able to start a drag from that table. i also tried to use a mouse listener to call exportAsDrag, but i only works in one direction. any ideas or examples?

    That is a rather large request, and indicates that you have likely not spent much time researching the subject before posting. Read up on Swing drag & drop here (http://java.sun.com/docs/books/tutorial/dnd/), then ask specific questions about something you don't understand, problems with your implementation, etc.
    Mike

  • Why i am unable to select between 2 anchor points with in a object while dragging with direct select

    why i am unable to select between 2 anchor points with in a object while dragging with direct selection tool instead it moves

    Another option is to temporarily change your view to outline mode, when your done switch back to preview mode. Ctrl-Y or View>Outline {View>Preview} The menu option will change depending on which mode you are in.
    And another option, double click on the object in question to place it in Isolation mode. You can now edit to your hearts content. When done, click on the gray border at top of document.
    So as you can see there are multiple ways of accomplishing the same thing.

  • If i have two option to select between XI or BW which one in better and why

    if i have two option to select between XI or BW which one in better and why
    both in terms of money and in terms of my career growth.......

    Sheetika,
    XI if you  are good in JAVA.The rest is same for both XI and BW.
    K.Kiran.

  • Alternate Selection in a JTree

    I am doing a GUI application for a customer who wants support for
    Windows, Solaris, and Linux. The customer is strongly windows biased
    and insists on having alternate selection (right click) to them.
    The application is a 6 panel MDI GUI and they want consistent behavior in all panels. I can do it for 5 of them but am having some trouble figuring out how to do it for the sixth.
    The sixth panel is a JTabbedPane that contains any number of JTrees embedded one for one in a JScrollPane. I have the Swing and AWT books from SUN. I have been through everything to do with JTrees and TreeSelectionListeners in the books and at www.java.sun.com.
    I can't see any support for alternate selection (Unix style) or right-click in the selection listeners. I would sure hate to have to write my own Selection listeners, handling mouse clicks, and handling paths just to be able to tell when a customer wants to right click something in a directory hierarchy and be consistent with what he gets in a topology map.
    They point out that other competitive products (competitive to them) that use other development environments support this. I would appreciate any pointers to how to do this.

    OK, Does isPopupTrigger() supposed to be true for a button3 click in
    a MS window's environment? It isn't when I test it. When I use isMetaDown() it is true. When I check the modifier values it is true, but isPopupTrigger() returns false.
    The direction of my question is looking for somewhere in the selection logic for the JTree to put the over-ride for MouseClick. TreeSelectionListener is intuitively the place to put it. But so far I can find the correct place.
    The Java Swing Manual on page 1345-1347 shows an example of how to get access to the mouse event that initiates the selection. But using it produces some anomalous results. First, the isPopupTrigger doesnt work as advertised. Second, when you detect for isAltDown() and find it down and try to consume() the event to prevent default selection behavior, it doesn't work.
    I can get it to do the right thing in the MS Window's environment because the JTree ignores right mouse clicks so when I use them for PopUp selection (by using an OR of isAltDown() and isMetaDown() to avoid the bug in isPopupTrigger() ). However, when I test it using the left mouse button and the ALT Key, it still selects the object and adds it to the list of paths for the tree, even though I have issued a consume() on the mouse event. I strongly suspect that the JTree selection logic only looks at MouseButton1 and does not check to see if
    the altKey is down or not. That will create issues for a Unix platform.
    I am looking for the correct place to intercept the mouse clicks and either consume them or pass them on.

  • Shortcut key to select between languages in Yosemite

    I would like to know the short cut key or create a custom short cut key to select between the two languages (English and Tamil, in my case). Since Command+Spacebar is used for Spotlight search - which is essential to me - I need to know how to overcome this?
    Any support is much appreciated.
    Thanks

    Thanks Alfred.
    I found F1 free and used it for language selection. That worked. (I didn't want to change Spotlight from command+space, since I use it frequently. :-)
    However, your reply helped me to achieve it. Thanks again! Long live. :-)

  • Difficulty in selecting between HV segmentation and classical(LV) segmentat

    Hi All,
    I am in a dilema to select between the HV seg. or the old LV one. Can anyone tell me is it feasable to work on LV segmentation with 20million plus BPs.We need it only for BP segmentation.Will the performance be affected drastically if we use LV seg.I am just trying to understand the pros and cons of both the solutions(i.e, hv and lv seg). It would be great if someone can give me more clarity on this so that we can choos ethe right solution for our client.
    thanks!

    Hi,
    starting with CRM 7.0 EhP1 there will be one single UI for HV and LV. You decide via the usage which one is used. However, if you want to use HV segmentation, you must have a TREX server up and running, whereas LV runs without. I dont know exactly what you mean by "Is there any such capacity for LV seg to be used". HV segmentation will always be faster.
    Best Regards
    --klaus

  • Selection between month type, currency

    Hi
    I have an requirement in OBIEE 10g where in we should have an option to select between
    1) calendar type(Normal calendar or Enterprise calendar),
    2) Currency Type (INR or USD)
    3) Value or Quantity
    For eg My report is Monthly Invoice (with Year Ago)
    The table & column structure is
    Time_table>Year ,Quarter,Month,Week, Ent Year,Ent Quarter,Ent Month
    Fact_invoice> Invoice Value in USD, Invoice Value in INR, Invoice QTY
    As user require Year Ago data as well I have created measure like - Year Ago Invoice Value in USD, Year Ago Invoice Value in INR, Year Ago Invoiced QTY
    Now user want a report like
    three Selection option at the top (selection as stated above) + selection option for table view and graph view
    I cant use view selector/column selector as I am using ago function so the request gives error (if I include all the above column in single request)
    Is there any way out
    Thanks & Regards
    Sameer

    35055 wrote:
    I cant use view selector/column selector as I am using ago function so the request gives error (if I include all the above column in single request)
    Is there any way outA simple workaround I can think off is to create two different reports, and place them in two different sections of the dashboard and use guided navigation to hide each section based on the selection of the prompt.
    Let me know if you need more details on how to use the guided navigation to achieve this.
    Thanks,
    - A.Y.

  • Select between clips not working

    I toggled something and am not sure what I did. I have been selecting between clips all day long, and command-T to add cross dissolve. Now I don't get the hourglassed shaped cursor to indicate that it is selecting (i.e. I cant select the line between two adjacent clips. If I copy to a new timeline it works. What did I toggle?

    Hi:
    Stupid question:
    Did you try saving your work, quitting and restarting FCP?
    Thanks

  • Keyboard Shortcut for Selecting between volumes?

    How do I select between volumes without having to use the mouse to select the upper left hand section if the finder window? It seems like I can only navigate the folders contained within the drives using the keypad, but not between the drives themselves. Anybody know if this is possible?

    iKey is a front end program that simulates typing and mouse movements. I use iKey to remap the Function keys.
    "iKey is an automation utility, a program that creates shortcuts to accomplish repetitive tasks. In essence, an iKey shortcut is a little program in its own right, but you don't need to know the first thing about programming to create an iKey shortcut. All you have to do is put together three necessary parts of a shortcut: One or more commands that give the shortcut its functionality, a context in which it runs, and a launcher that defines how the shortcut is activated."
    http://www.scriptsoftware.com/ikey/
    iKey has a little more function then the previous free version called youpi key. For many years, I used youpi key before switching to iKey. It works fairly well for me in MAC OS 10.4 although not officially supported.
    http://www.versiontracker.com/dyn/moreinfo/macosx/11485&vid=75326
    I have the common programs that I use assigned to function keys. I have F4 assigned to Firefox. When I want to start FireFox, I press F4. When I want to switch to firefox, I press F4! Starting & switching to an application in Mac OS are the same thing in Mac OS.
    Here is an example of to assign volumn control to a function key.
    http://discussions.apple.com/message.jspa?messageID=10361085#10361085
    Here is my script for listing my application folder. I have it assigned to function-key 6.
    !http://farm3.static.flickr.com/2689/4292832695_f3a8f1122e.jpg!
    tell application "Finder"
       set windowNames to windows whose name contains "Applications"
       repeat with wNames_ref in windowNames
          close wNames_ref
       end repeat
       activate
       select window of desktop
       make new Finder window to startup disk
       select Finder window 1
       set target of Finder window 1 to folder "Applications" of startup disk
       select Finder window 1
       set position of Finder window 1 to {60, 45}
    end tell
    The second portion of this script was generated in the script editor record mode. After I recorded the script and did some editing, I copy the script to ikey/youpi key.
    I have all of my frequently used applications set to function keys.

  • Use a button to select contents in Jtable

    I have a jTable which holding a data list, I need two buttons, one is 'up' and one is 'down', to move selections in the jTable. The idea is if a client don't have a mouse, they can use these two buttons to navigate through the jTable.
    Problem One: if the jTable is not selected, how can press a button make the first record been selected?
    Problem Two: if the jTable is selected, how can press a button move the selection
    I tried to mod ListSelectionModel, but I cannot find a method that does this. I thought if I can get this jTable selected (any record), I probably can move the selection by setting the selectionIndex or some...
    Thank you

    Darryl.Burke wrote:
    I'd still like to know how OP expects the clients to click a button without a mouse. Yes, I know about mnemonics, but if the clients did too, they should surely be able to work out the keyboard navigation as mentioned by WalterLaan.
    db<sarcasm>
    I know what he can do if their is no mouse.
    OP can use the Robot class and keylisteners then move the mouse cursor according to which listener method is called and which key is responsible for the call.
    I mean this is so brilliant, can you imagine how much money people will save on buying a mouse if this catches on.
    I am telling you this is the future, say good by mouse your history now. Hahahaha
    </sarcasm>

Maybe you are looking for

  • Error in executing ODCIIndexStart()

    I a Search Application,I need to find the documents, where a StringAttribute (Author) contains any of the values in a specified array. For this purpose, I use an AnyOf-query (combined with some other queries). Alas I keep getting the following error-

  • Help needed in implementing validation event handler in OIM 11g

    Hello experts, I am trying to set username policy by implementing a validation event handler in OIM 11.1.1.5. Following are the steps followed 1. Import metadata Created a file called Eventhandler.xml as below and imported using weblogicImportmetadat

  • How to sync pdf files in iBook on Mac to iBook on iPad/phone?

    I used to easily move pdf files I created on my Mac to my iPad by using iTunes to sync them as books.  But now books are separate from iTunes (as they should be), and iBooks doesn't sync my pdfs to my iPad.  I've done it using Dropbox as an intermedi

  • Inserting forms in Pages

    Why is it that when I want to insert a text box or a form, the start of it ends up always in middle of the page where I then have to place it where I want to? Is there any way to override this annoying thing and have the text box placed wherever I ha

  • Format the date in varchar type to mm/dd/yyyy

    Hi, I have a column originally in date format. Later on, I changed it to nvarchar and all the existing date changed to this format "yyyy-mm-dd 00:00:00". How could I update all those dates to mm/dd/yyyy format. Should I use the expression? Thanks.