2

I am having this requirement to give excel like functionality in my Swing project. For that I used JTable with column headers and row headers. And I like to redraw specific rows and columns on the panel also. But it contains a lot of pain in redrawing each JTable cell rows and columns with specific cell customization. Just like to know for such kind of requirement. Is there any other specific component which can be integrated with swing.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
ajay partoti
  • 241
  • 3
  • 4
  • 11
  • Take a look at: http://stackoverflow.com/questions/7306661/importing-excel-xls-in-java-swing – user1249569 Oct 08 '12 at 11:37
  • 1
    I feel that `JTable` is not an appropriate component for representing a worksheet (or whatever it is called) in an Excel spreadsheet. `JTable` might be great for representing the ***purely tabular*** information in one of those sheets, but when it comes to references, formulae, graphs and charts - the limitations of `JTable` begin to show. – Andrew Thompson Oct 08 '12 at 11:47
  • @AndrewThompson but an excel worksheet is purely tabular data. Building a TableModel based on an excel worksheet should be straightforward. After that, rendering and editing may be a little more complex but still I would use a JTable or a JXTable. – Guillaume Polet Oct 08 '12 at 12:22
  • @Gui *"but an excel worksheet is purely tabular data."* Not the ones I make. They will typically have a group of totals (or averages/whatever) on the column header line, and possibly include graphs and charts of the data. I did a little experiment pulling tabular data from ODS spreadsheet files that I'd made (it ignored formulae, references and charts and simply tried to pull the remaining tabular data). When I asked on SO if it was workable I was prompted to surf around and check some of the other ODS files available. Even when they were ostensibly tabular, they were written differently. – Andrew Thompson Oct 08 '12 at 12:28
  • @AndrewThompson Probably a difference of point of view. For me, average, totals, etc... is still just a regular cell with a dedicated formula, but it is still a cell. As for the Graphs etc..., that of course is not a regular cell data, but it could easily be displayed within a JFreeChart and nothing prevents you from adding the JFreeChart to the JTable. Of course, all this can only work with a strong model behind, but the JTable does not seem, at least to me, like a bad component for doing this kind of a job. – Guillaume Polet Oct 08 '12 at 12:34
  • @GuillaumePolet *"is still just a regular cell with a dedicated formula"* Yes.. but when you put that data into a `JTable`, it seems like a lot of futzing to represent that line of totals in the same `JTable`. OTOH perhaps I am being too 'close-minded' about this. Have you successfully implemented (or seen) a spreadsheet app. based around `JTable`? On a slightly different note, perhaps we could see the `JTable` as merely a representation of the purely tabular data, in a **compound component** that might also display other things like summary lines and charts. – Andrew Thompson Oct 08 '12 at 12:40
  • @AndrewThompson To be fair, no I have never seen something like that with a JTable. Yet, that would have been my first thing to consider. Maybe I jumped to conclusions too fast. Unfortunately, this is not a 5-minutes thing to do, so I won't know until somebody else does it ;-). Regarding using a compound component, I have the feeling that you would go into a lot of troubles if your totals are in the middle of the sheet. But I can't be really sure until I have tried that. Anyway, thanks for your feedback on this. ;-) – Guillaume Polet Oct 08 '12 at 12:44
  • 1
    @GuillaumePolet I've enjoyed mulling this over as well. Thanks for your insights. :) – Andrew Thompson Oct 08 '12 at 12:46

1 Answers1

2

Microsoft Excel is a non-trivial application, so I will focus on a single aspect of the program: it evaluates regular expressions and displays the results in a tabular array.

Assuming JTable meets your needs for a tabular view, let your TableModel store expressions as instances of String, and let your implementation of getValueAt() return the result of evaluating the expression. Several libraries, such as this one, are available.

Alternatively, simply open() the spreadsheet, leveraging the user's preferred spreadsheet application.

As far as I know, there is no general purpose library to emulate Excel in Java.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045