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


Groups > comp.lang.java.programmer > #37634

Re: How do you update the view space of a JTable?

Newsgroups comp.lang.java.programmer
Date 2017-11-01 06:29 -0700
References (5 earlier) <ot9ois$idb$1@dont-email.me> <66fc1800-d494-43a1-963d-a1a54a99f631@googlegroups.com> <ot9qht$3bf$1@dont-email.me> <28c95541-33f4-4502-af4e-31d82c672841@googlegroups.com> <otap2d$uss$1@dont-email.me>
Message-ID <fd194d27-8172-4ba2-b90e-44843e950cf8@googlegroups.com> (permalink)
Subject Re: How do you update the view space of a JTable?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


Apologies for much of the style issues, a fair amount of this example was copy-pasted from somewhere on the internet, not exactly how I would write.  I just modified it enough to demonstrate what functionality I need.

On Tuesday, October 31, 2017 at 5:13:57 PM UTC-4, Eric Sosman wrote:
>      A brief look at the code reveals one serious error (of the
> kind you'll "usually" get away with), one serious error that no
> Java programmer should ever commit, a few venial sins, and some
> opportunities for stylistic improvement.  What it doesn't reveal
> is the code's purpose nor how it fails to fulfill that purpose,
> which makes it a little hard to diagnose ...
> 
>      Herewith, the problems apparent to my brief inspection:
> 
>      - Serious: Swing is not thread-safe, and almost all
> Swingstuff must be done on the event dispatching thread (EDT).
> Your code constructs and launches a JFrame and all its contents from
> whatever thread executes main().  You'll "usually" get away with this,
> because Murphy waits until the Big Demo for the Big Boss.  See
> https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html
> 
Easy enough.  I don't normally do that.  Adjusting that main procedure to look like my other tests would at least do this:

     public static void main(final String[] args) {

          EventQueue.invokeLater(() -> {
               showGui();
          });
     }

     protected static void showGui() {
          final FixedRowExample frame = new FixedRowExample();
          frame.addWindowListener(new WindowAdapter() {

               @Override
               public void windowClosing(final WindowEvent e) {
                    System.exit(0);
               }
          });
          frame.pack();
          frame.setVisible(true);
     }

(while some of my more complex tests involve a SwingWorker)

>      - Serious: `catch (Exception e)'?  Are you KIDDING?
> 
I'm not sure what exception(s) that was expecting to catch.  More specifically it only appears to need:
} catch (UnsupportedFlavorException | IOException e) {

>      - Venial: Various constructors call potentially overridable
> methods, which could be a problem if any of your classes are someday
> extended.  You could cure this by making the involved classes final,
> or by making the methods final, or (since you probably don't need to
> override some of them) by explicitly calling the superclass' method,
> as in `super.getContentPane()' instead of just `getContentPane()'.
> Or, you could just hope no one will ever extend the class and find
> some reason to override setAutoResizeMode() or whatever.
> 
I'm not sure exactly where or why this is bad form?  Obviously the getContentPant() is from Oracle's JFrame which is overridable, and I've found many Oracle classes and methods can be overridden including ones which probably should not be.  Of course this is just a test class so it doesn't hurt for this class and it's subclasses to be final.  Many if not most test classes on the internet seem to extend JFrame, though I've tried not to do that in my own tests, so rather than super.getContentPane() it should really be testFrame1.getContentPane().  I haven't used final on any classes or methods personally since I haven't seen confirmation of it improving performance and I'm not sure when you really need to ensure that no one overrides a method which must be visible.

>      - Venial: Various constructors leak the `this' reference before
> `this' is fully constructed.  The transgression would ordinarily be
> worse than venial, but adding event listeners "should probably be"
> safe since they're unlikely to be called until a good deal later.
> IMHO this is an infelicity in the architecture of Swing/AWT: You
> would *like* a constructor to deliver a fully-functional ready-to-
> rumble instance of a JFrame or whatever, but if you're really not
> supposed to register event listeners in a constructor ...  The
> squeaky-clean approach, I guess, is to have a post-construction
> init() method, which strikes me as Ugly.  Or ignore it and hope.
> 
I'm not sure where exactly you're referring to this or how exactly it could be improved.  Am I just not understanding or do you not like how Oracle does GUI? ie JTable.setModel references this and is called from the constructor.

>      - Venial: The `MyTable' class is package-private, yet some
> `public' methods either return it or use it as a parameter.  Either
> make the class `public' or make the methods package-private: Pay
> attention to your access levels.
> 
A non-issue I simply hadn't noticed due to this being a self contained test class, not a problem making that public as it should be in an API.

>      - Style: Sleekness could be improved with a lambda expression
> instead of an anonymous inner subclass of AdjustmentListener.
> 
I would like to see syntax that improves that.  I haven't worked with a lot of lambdas myself, only implementing them in a couple of places where I couldn't see any other way to code, or where I found a shortcut which doesn't take away from readability as in the previously mentioned EventQueue.invokeLater.

>      - Style: Use `intValue' instead of `new Integer(intValue)' and
> let auto-boxing take care of wrapping it.  (May produce a tiny gain
> in efficiency, too.)
> 
That actually may be required for an older version of Java?  I just took that out altogether and the compiler just accepted the method's int return value for the constructor's Object parameter.  It seems the only reason anymore to explicitly convert ie int to Integer is if you need to call a method, though most Integer methods can be done with static calls.

>      - Style: Instead of calling System.exit() on a window-close event,
> you could just `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)'.
> 
I don't recall if I wrote that part or copied it.  This is another thing which is obviously only done because it's a self contained test.  I normally use the window listener method, but I normally don't exit the application when it closes.  Our standard window close does the "are you sure?" popup.

>      - Style: The types of some fields are unnecessarily specific:
> `AbstractTableModel' instead of `TableModel', that sort of thing.
> It is usually "better form" to use the least specific declaration
> that's suitable -- often, an interface rather than a particular
> implementation of that interface.  That way, you retain flexibility
> to change the implementation later with little or no upheaval.
> 
Just referring to declarations, nor construction? ie
final AbstractTableModel fixedModel = new AbstractTableModel() {
could be
final TableModel fixedModel = new AbstractTableModel() {

I'm not aware of any good table models which don't extend AbstractTableModel, but if it's possible for this table to work properly without extending it that would be fine.

>      I sincerely doubt that fixing any of the above will help you
> in the least with whatever problem you're having in the pursuit of
> whatever goal you're after, but "Cleanliness is next to Godliness,
> also next to impossible."
> 

Agreed, none of this helps toward the goal of having one JTable with a set number of fixed rows followed by a variable number of scrollable rows having methods like setSelectedRow() refer to all the rows as part of a single table (though the solution appears to require the fixed and scrollable regions in separate tables), but any advice which improves coding practice is always welcome.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-17 08:47 -0700
  Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-18 06:11 -0700
  Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-30 17:41 +0100
    Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-30 10:29 -0700
      Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-31 11:10 +0100
        Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 04:46 -0700
          Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 07:59 -0400
            Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 05:21 -0700
              Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 08:32 -0400
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-10-31 06:13 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-10-31 17:13 -0400
                Re: How do you update the view space of a JTable? Daniele Futtorovic <da.futt.news@laposte.net> - 2017-10-31 23:33 +0100
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-11-01 06:29 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-11-01 10:37 -0400
                Re: How do you update the view space of a JTable? Eric Douglas <e.d.programmer@gmail.com> - 2017-11-01 08:37 -0700
                Re: How do you update the view space of a JTable? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-11-01 13:20 -0400

csiph-web