JTable vs. column names

Ric Werme's Avatar

Ric Werme

08 Apr, 2021 06:35 PM

I want to create a results table for my net worth program and have two problems:

1) This doesn't show the column names:

    column_names = ['Type', 'Account', 'Column 3']
    row_data = [
        ['Row 1', 'entry 1', 'entry 2'],
        ['Row 2', 'entry 1', 'entry 2']
    ]

    table = JTable(DefaultTableModel(row_data, column_names))
    # Neither does JTable(row_data, column_names)

The Java docs suggests:

JTable(Object[][] rowData, Object[] columnNames)
Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

The row_data gets displayed, but the column_names don't.

2) I'm looking at extract_data.py's do_extract_reminders() for hints, but it's 17 pages long on my printer. I may have missed something. :-)

This feels like either I'm doing something stupid (likely) or the Jython interface is having trouble.

I suppose it might help if I had ever used Java Swing before. I used awt in my only Java class, I think that was about 20 years ago.

  1. 1 Posted by Stuart Beesley ... on 08 Apr, 2021 06:54 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Take a look at my extract_data script for examples.

    https://yogi1967.github.io/MoneydancePythonScripts/

  2. 2 Posted by Stuart Beesley ... on 08 Apr, 2021 06:55 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Whoops - I see you are already.... 😆 sorry. I’ll take a look but might be tomorrow.

  3. 3 Posted by hleofxquotes on 08 Apr, 2021 07:49 PM

    hleofxquotes's Avatar

    Simplest way to fix that is to attach JTable to a JScrollPane. As in

        private void initFrame(Container contentPane) {
            JPanel view = new JPanel(new BorderLayout());
            view.setPreferredSize(new Dimension(600, 400));
    
            String[] columnNames = { "col1", "col2", "col3", };
            String[][] data = { 
                    { "11", "12", "13"}, 
                    { "21", "22", "23"},
            };
    
            JTable table = new JTable(new DefaultTableModel(data, columnNames));
            JScrollPane sp = new JScrollPane(table);
    
            view.add(sp, BorderLayout.CENTER);
    
            contentPane.add(view);
        }
    

    The full class

    public class ShowTableCmd extends JFrame {
    
        public ShowTableCmd(String title) throws HeadlessException {
            super(title);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            initFrame(getContentPane());
        }
    
        private void initFrame(Container contentPane) {
            JPanel view = new JPanel(new BorderLayout());
            view.setPreferredSize(new Dimension(600, 400));
    
            String[] columnNames = { "col1", "col2", "col3", };
            String[][] data = { { "11", "12", "13" }, { "21", "22", "23" }, };
    
            JTable table = new JTable(new DefaultTableModel(data, columnNames));
            JScrollPane sp = new JScrollPane(table);
    
            view.add(sp, BorderLayout.CENTER);
    
            contentPane.add(view);
        }
    
        public static void main(String[] args) {
            final ShowTableCmd cmd = new ShowTableCmd(ShowTableCmd.class.getSimpleName());
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    cmd.showMainFrame();
                }
            });
        }
    
        protected void showMainFrame() {
            setLocation(100, 100);
            pack();
            setVisible(true);
        }
    
    }
    
  4. 4 Posted by Stuart Beesley ... on 08 Apr, 2021 07:59 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Python script... I see hleofxquotes has already replied... I had just knocked this up, so attaching anyway..

  5. 5 Posted by Stuart Beesley ... on 08 Apr, 2021 08:02 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    PS - I didn't bother with the invokeLater() but you should for anything more than a few lines of code....

  6. 6 Posted by Ric Werme on 08 Apr, 2021 08:23 PM

    Ric Werme's Avatar

    Thanks guys, it would have been a while before I tried JScrollPane. Probably a finite amount of time, I figured I'd need it sooner or later.

    Stuart - also on my list to investigate is running Jython code outside of Moneydance. I guess at the very least I need to find some magic path variable. (Perhaps a clue is in your invoke Moneydance shell script. Not a high priority today or even this week.)

    On my Linux system, not in the Moneydance install directory:

    $ python jtable.py
    Traceback (most recent call last):
      File "jtable.py", line 10, in <module>
        from com.infinitekind.moneydance.model import *
    ImportError: No module named com.infinitekind.moneydance.model

  7. 7 Posted by Stuart Beesley ... on 08 Apr, 2021 08:30 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Tip. Outside of Moneydance, you will need to run Jython to run Java/Jython code, not Python.. Or use something like JPype. But you will not (easily/safely) be able to access the Moneydance data outside of MD...

    https://www.jython.org/download.html

  8. 8 Posted by Stuart Beesley ... on 08 Apr, 2021 08:47 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    ... as the dataset is encrypted and you need to load the moneydance libraries and ‘system’ to properly open the dataset. User @dalefurrow did have some success, but the method was advised by Sean as ‘to be avoided’. That method didn't seem to work anyway if you had set a different encryption password. However, as you will see on the next post, hleofxquotes has achieved it with some technical wizardry.

    IMHO - You would be better off launching moneydance using the --invoke_and_quit method to launch MD, execute an extension, then quit.

  9. 9 Posted by hleofxquotes on 08 Apr, 2021 09:43 PM

    hleofxquotes's Avatar

    It doesn’t work anyway if you set a different encryption password.

    A example showing how to run Mike's filedisplay extension as a stand-alone (without MD main UI):

    Then read the README.txt

  10. 10 Posted by Ric Werme on 12 Apr, 2021 12:32 AM

    Ric Werme's Avatar

    Hey, lookee what I came across today in https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html :

    "JTables are typically placed inside of a JScrollPane. By default, a JTable will adjust its width such that a horizontal scrollbar is unnecessary. To allow for a horizontal scrollbar, invoke setAutoResizeMode(int) with AUTO_RESIZE_OFF. Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane) and want the header displayed, you can get it using getTableHeader() and display it separately."

    I wonder why that didn't sear into my memory when I first saw it. :-)

  11. System closed this discussion on 12 Jul, 2021 12:40 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac