Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33463 > unrolled thread

Swing - Using JTabbedPane

Started bygalt_57@hotmail.com
First post2016-01-16 07:53 -0800
Last post2016-01-18 06:39 -0800
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Swing - Using JTabbedPane galt_57@hotmail.com - 2016-01-16 07:53 -0800
    Re: Swing - Using JTabbedPane Jeff Higgins <jeff@invalid.invalid> - 2016-01-16 19:29 +0000
      Re: Swing - Using JTabbedPane galt_57@hotmail.com - 2016-01-17 06:35 -0800
        Re: Swing - Using JTabbedPane Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-17 10:08 -0500
        Re: Swing - Using JTabbedPane Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-01-17 09:49 -0800
          Re: Swing - Using JTabbedPane Martin Gregorie <martin@address-in-sig.invalid> - 2016-01-17 20:00 +0000
    Re: Swing - Using JTabbedPane Jeff Higgins <jeff@invalid.invalid> - 2016-01-17 14:21 +0000
    Re: Swing - Using JTabbedPane Eric Douglas <e.d.programmer@gmail.com> - 2016-01-18 06:39 -0800

#33463 — Swing - Using JTabbedPane

Fromgalt_57@hotmail.com
Date2016-01-16 07:53 -0800
SubjectSwing - Using JTabbedPane
Message-ID<df13f200-8551-4401-9da0-ad0ecc203a5e@googlegroups.com>
So I'm throwing JTables into JScrollPanes and then adding them as 
new tabs, but when I want to read the selected table again later, I'm 
not sure what to do. Right now the code below seems to be working, 
but I'm not sure if it is a good approach. Also JViewport is a mystery.


int tab = jTabbedPane1.getSelectedIndex();
        
if (tab == -1){
   statusMessageLabel.setText("Error: no tab is selected."); 
   return;
}
        
Component csp = jTabbedPane1.getComponent(0);
Component cvp;
Component cjt;
        
JTable jt = null;
        
if (csp instanceof JScrollPane){
   cvp = ((JScrollPane)csp).getComponent(0);
   if (cvp instanceof JViewport){
       cjt = ((JViewport)cvp).getComponent(0);
       if (cjt instanceof JTable){
           jt = (JTable)cjt;
       }
   }
}
        
if (jt == null){
    statusMessageLabel.setText("Error: selected tab "+ tab + " is not a table.");
    return;
}

[toc] | [next] | [standalone]


#33464

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-16 19:29 +0000
Message-ID<n7e5m2$ojp$1@dont-email.me>
In reply to#33463
On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:

> So I'm throwing JTables into JScrollPanes and then adding them as new
> tabs, 

You've not shown any of this code.

> but when I want to read the selected table again later, I'm not
> sure what to do. 

Here are some resources to help you figure it out.

Java SE API Documentation
https://docs.oracle.com/javase/8/docs/api/index.html

The Java™ Tutorials - Creating a GUI with Swing
https://docs.oracle.com/javase/tutorial/uiswing/index.html

> Right now the code below seems to be working,

Great!

> but I'm not sure if it is a good approach. 

Impossible to tell without the rest of the code.

> Also JViewport is a mystery.

See the above resources.

> 
> 
> int tab = jTabbedPane1.getSelectedIndex();
>         
> if (tab == -1){
>    statusMessageLabel.setText("Error: no tab is selected.");
>    return;
> }
>         
> Component csp = jTabbedPane1.getComponent(0);
> Component cvp;
> Component cjt;
>         
> JTable jt = null;
>         
> if (csp instanceof JScrollPane){
>    cvp = ((JScrollPane)csp).getComponent(0);
>    if (cvp instanceof JViewport){
>        cjt = ((JViewport)cvp).getComponent(0);
>        if (cjt instanceof JTable){
>            jt = (JTable)cjt;
>        }
>    }
> }
>         
> if (jt == null){
>     statusMessageLabel.setText("Error: selected tab "+ tab + " is not a
>     table.");
>     return;
> }

[toc] | [prev] | [next] | [standalone]


#33466

Fromgalt_57@hotmail.com
Date2016-01-17 06:35 -0800
Message-ID<78ecf434-7366-4dd4-a083-7319f78aad4c@googlegroups.com>
In reply to#33464
On Saturday, January 16, 2016 at 1:32:07 PM UTC-6, Jeff Higgins wrote:
> On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:
> 
> > So I'm throwing JTables into JScrollPanes and then adding them as new
> > tabs, 
> 
> You've not shown any of this code.
> 
> > but when I want to read the selected table again later, I'm not
> > sure what to do. 
> 
> Here are some resources to help you figure it out.
> 
> Java SE API Documentation
> https://docs.oracle.com/javase/8/docs/api/index.html
> 
> The Java(tm) Tutorials - Creating a GUI with Swing
> https://docs.oracle.com/javase/tutorial/uiswing/index.html
> 
> > Right now the code below seems to be working,
> 
> Great!
> 
> > but I'm not sure if it is a good approach. 
> 
> Impossible to tell without the rest of the code.
> 
> > Also JViewport is a mystery.
> 
> See the above resources.
> 

Thanks for the references.

Let me post a correction to my original code and add a summary of how I create the tab...

JTable jt;
DefaultTableModel mod;
.
. //fill the table body
.
JScrollPane sp = new JScrollPane(jt);
jTabbedPane01.addTab(tabname,sp); // so here the tab has been created

----------------

// in the routine below I want to read the jtable in the selected tab...

int tab = jTabbedPane01.getSelectedIndex();
        
if (tab == -1){
   statusMessageLabel.setText("Error: no tab selected."); 
   return;
}
        
Component csp = jTabbedPane01.getComponentAt(tab);
Component cvp;
Component cjt;
        
JTable jt = null;
        
if (csp instanceof JScrollPane){
    cvp = ((JScrollPane)csp).getComponent(0);
    if (cvp instanceof JViewport){
        cjt = ((JViewport)cvp).getComponent(0);
        if (cjt instanceof JTable){
            jt = (JTable)cjt;
        }
    }
}
        
if (jt == null){
    statusMessageLabel.setText("Error: selected tab "+ tab + " is not a table.");
    return;
}

[toc] | [prev] | [next] | [standalone]


#33467

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-01-17 10:08 -0500
Message-ID<n7gajo$40b$1@dont-email.me>
In reply to#33466
On 1/17/2016 9:35 AM, galt_57@hotmail.com wrote:
> On Saturday, January 16, 2016 at 1:32:07 PM UTC-6, Jeff Higgins wrote:
>> On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:
>>
>>> So I'm throwing JTables into JScrollPanes and then adding them as new
>>> tabs,
>>
>> You've not shown any of this code.
>>
>>> but when I want to read the selected table again later, I'm not
>>> sure what to do.
>> [... walks the component tree until finding the table ...]

     This should work, but it feels fragile: If anything changes in the
containment pattern you're lost.  (For example, if you decided to omit
the JScrollPane on a "special" table somewhere, or if the next version
of Java rearranges something about the way the viewport works, or ...)

     You might improve robustness by giving each JTable a unique name
(see the setName() and getName() methods of java.awt.Component, from
which JTable eventually descends).  You could then walk the component
tree looking for the desired name; this would make you immune to small
changes in the containment pattern.

     Even better (perhaps) might be to subclass JScrollPane with your
own table-containing JComponent, giving it a getUltimateContent()
method:

	class MyScrollableContainer extends JScrollPane {
	    private Component ultimateContent;

	    public MyScrollableContainer(Component content) {
	        super(content);
	        this.ultimateContent = content;
	    }
	    // Other constructors if desired ...

	    public Component getUltimateContent() {
	        return ultimateContent;
	    }
	}

This way, you don't care how many layers of additional containment
(JViewport, whatever) the JScrollPane class may interpose: You've got
a direct line to the JTable with which you created the thing.

     The larger question, though: Why do you want to find the JTable,
anyhow?  Yes, there are reasons to manipulate it directly: Maybe you
want to auto-select a group of rows in response to something other
than a GUI action, maybe you want to sort or filter the rows, maybe
there's something else you want to do with the table itself.  But IMHO
you'll usually be better off treating the JTable as a GUI gadget, much
like a JButton, and concerning yourself instead with the TableModel
that holds its data.  You could just whack away at the TableModel and
not worry about which JTables might or might not happen to be
displaying it at the moment, whether any of those JTables happen to
be visible, and so on.

     Okay, so that's not always viable (and one reason for wanting to
find the JTable is so you can call getModel() on it).  But it's quite
often the case that directly manipulating the GUI gadget is doing things
backwards, and that working with the model instead will do better.

     So: Can you explain why you want to get hold of the JTable?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#33468

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2016-01-17 09:49 -0800
Message-ID<n7gk2k$9qi$1@dont-email.me>
In reply to#33466
On 1/17/2016 06:35, galt_57@hotmail.com wrote:
> On Saturday, January 16, 2016 at 1:32:07 PM UTC-6, Jeff Higgins wrote:
>> On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:
>>
>>> So I'm throwing JTables into JScrollPanes and then adding them as new
>>> tabs,
>>
>> You've not shown any of this code.
>>
>>> but when I want to read the selected table again later, I'm not
>>> sure what to do.
>>
>> Here are some resources to help you figure it out.
>>
>> Java SE API Documentation
>> https://docs.oracle.com/javase/8/docs/api/index.html
>>
>> The Java(tm) Tutorials - Creating a GUI with Swing
>> https://docs.oracle.com/javase/tutorial/uiswing/index.html
>>
>>> Right now the code below seems to be working,
>>
>> Great!
>>
>>> but I'm not sure if it is a good approach.
>>
>> Impossible to tell without the rest of the code.
>>
>>> Also JViewport is a mystery.
>>
>> See the above resources.
>>
>
> Thanks for the references.
>
> Let me post a correction to my original code and add a summary of how I create the tab...
>
> JTable jt;
> DefaultTableModel mod;
> .
> . //fill the table body
> .
> JScrollPane sp = new JScrollPane(jt);
> jTabbedPane01.addTab(tabname,sp); // so here the tab has been created
>
> ----------------
>
> // in the routine below I want to read the jtable in the selected tab...
>
> int tab = jTabbedPane01.getSelectedIndex();
>
> if (tab == -1){
>     statusMessageLabel.setText("Error: no tab selected.");
>     return;
> }
>
> Component csp = jTabbedPane01.getComponentAt(tab);
> Component cvp;
> Component cjt;
>
> JTable jt = null;
>
> if (csp instanceof JScrollPane){
>      cvp = ((JScrollPane)csp).getComponent(0);
>      if (cvp instanceof JViewport){
>          cjt = ((JViewport)cvp).getComponent(0);
>          if (cjt instanceof JTable){
>              jt = (JTable)cjt;
>          }
>      }
> }
>
> if (jt == null){
>      statusMessageLabel.setText("Error: selected tab "+ tab + " is not a table.");
>      return;
> }
>

This seems like a lot of work to find your JTables when you could just 
keep a list of references to them somewhere.  That way, you wouldn't 
care what your component hierarchy was and you could change it without 
having to rewrite your code.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#33469

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-01-17 20:00 +0000
Message-ID<n7grss$bf4$1@dont-email.me>
In reply to#33468
On Sun, 17 Jan 2016 09:49:45 -0800, Knute Johnson wrote:

> On 1/17/2016 06:35, galt_57@hotmail.com wrote:
>> On Saturday, January 16, 2016 at 1:32:07 PM UTC-6, Jeff Higgins wrote:
>>> On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:
>>>
>>>> So I'm throwing JTables into JScrollPanes and then adding them as new
>>>> tabs,
>>>
>>> You've not shown any of this code.
>>>
>>>> but when I want to read the selected table again later, I'm not sure
>>>> what to do.
>>>
>>> Here are some resources to help you figure it out.
>>>
>>> Java SE API Documentation
>>> https://docs.oracle.com/javase/8/docs/api/index.html
>>>
>>> The Java(tm) Tutorials - Creating a GUI with Swing
>>> https://docs.oracle.com/javase/tutorial/uiswing/index.html
>>>
>>>> Right now the code below seems to be working,
>>>
>>> Great!
>>>
>>>> but I'm not sure if it is a good approach.
>>>
>>> Impossible to tell without the rest of the code.
>>>
>>>> Also JViewport is a mystery.
>>>
>>> See the above resources.
>>>
>>>
>> Thanks for the references.
>>
>> Let me post a correction to my original code and add a summary of how I
>> create the tab...
>>
>> JTable jt;
>> DefaultTableModel mod;
>> .
>> . //fill the table body .
>> JScrollPane sp = new JScrollPane(jt);
>> jTabbedPane01.addTab(tabname,sp); // so here the tab has been created
>>
>> ----------------
>>
>> // in the routine below I want to read the jtable in the selected
>> tab...
>>
>> int tab = jTabbedPane01.getSelectedIndex();
>>
>> if (tab == -1){
>>     statusMessageLabel.setText("Error: no tab selected.");
>>     return;
>> }
>>
>> Component csp = jTabbedPane01.getComponentAt(tab);
>> Component cvp;
>> Component cjt;
>>
>> JTable jt = null;
>>
>> if (csp instanceof JScrollPane){
>>      cvp = ((JScrollPane)csp).getComponent(0);
>>      if (cvp instanceof JViewport){
>>          cjt = ((JViewport)cvp).getComponent(0);
>>          if (cjt instanceof JTable){
>>              jt = (JTable)cjt;
>>          }
>>      }
>> }
>>
>> if (jt == null){
>>      statusMessageLabel.setText("Error: selected tab "+ tab + " is not
>>      a table.");
>>      return;
>> }
>>
>>
> This seems like a lot of work to find your JTables when you could just
> keep a list of references to them somewhere.  That way, you wouldn't
> care what your component hierarchy was and you could change it without
> having to rewrite your code.
>
Nice one. 

A further thought: if you use a Map to store the reference list you can 
use the name of the corresponding as the reference key for each JTable. 
That could also be generalised into a list of class instances, each 
holding the data and class references you need externally to manage the 
content of each JScrollPanel.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#33465

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-17 14:21 +0000
Message-ID<n7g81d$2l1$1@dont-email.me>
In reply to#33463
On Sat, 16 Jan 2016 07:53:54 -0800, galt_57 wrote:

> So I'm throwing JTables into JScrollPanes and then adding them as new
> tabs, but when I want to read the selected table again later, I'm not
> sure what to do. Right now the code below seems to be working,
> but I'm not sure if it is a good approach. Also JViewport is a mystery.

It may be a good approach.
Here is another which may be good or not.

package scratch;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JTabbedPane;

@SuppressWarnings("serial")
public class MyTabbedPane extends JTabbedPane {
  
  private Map<Integer, TabTypes> tabMap = new HashMap<>();
  
  public enum TabTypes {Empty, Table, Text, Unknown}
  
  public TabTypes getTabType(int index)
  throws IndexOutOfBoundsException {
    return tabMap.get(index);
  }

}

> int tab = jTabbedPane1.getSelectedIndex();
>         
> if (tab == -1){
>    statusMessageLabel.setText("Error: no tab is selected.");
>    return;
> }
>         
> Component csp = jTabbedPane1.getComponent(0);
> Component cvp;
> Component cjt;
>         
> JTable jt = null;
>         
> if (csp instanceof JScrollPane){
>    cvp = ((JScrollPane)csp).getComponent(0);
>    if (cvp instanceof JViewport){
>        cjt = ((JViewport)cvp).getComponent(0);
>        if (cjt instanceof JTable){
>            jt = (JTable)cjt;
>        }
>    }
> }
>         
> if (jt == null){
>     statusMessageLabel.setText("Error: selected tab "+ tab + " is not a
>     table.");
>     return;
> }

[toc] | [prev] | [next] | [standalone]


#33470

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-01-18 06:39 -0800
Message-ID<9ceacb33-c920-4100-9fae-5268bdc09d7e@googlegroups.com>
In reply to#33463
On Saturday, January 16, 2016 at 10:54:12 AM UTC-5, gal...@hotmail.com wrote:
> So I'm throwing JTables into JScrollPanes and then adding them as 
> new tabs, but when I want to read the selected table again later, I'm 
> not sure what to do. Right now the code below seems to be working, 
> but I'm not sure if it is a good approach. Also JViewport is a mystery.
> 
> 
> int tab = jTabbedPane1.getSelectedIndex();
>         
> if (tab == -1){
>    statusMessageLabel.setText("Error: no tab is selected."); 
>    return;
> }
>         
> Component csp = jTabbedPane1.getComponent(0);
> Component cvp;
> Component cjt;
>         
> JTable jt = null;
>         
> if (csp instanceof JScrollPane){
>    cvp = ((JScrollPane)csp).getComponent(0);
>    if (cvp instanceof JViewport){
>        cjt = ((JViewport)cvp).getComponent(0);
>        if (cjt instanceof JTable){
>            jt = (JTable)cjt;
>        }
>    }
> }
>         
> if (jt == null){
>     statusMessageLabel.setText("Error: selected tab "+ tab + " is not a table.");
>     return;
> }

If you have a more complete example code you can get a more complete answer.

I rarely get a component I created from another component.  Normally I keep a pointer to the component for future reference.

I create all tables as a custom class which extends JTable, which adds the table by default to a JScrollPane in the constructor, because why would you need a JTable without a scroll pane?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web