Swing JTable Hack
I have a fairly complicated JTable subclass (WidgetTable
and its WidgetTableModel
) that works fine when I add it to a dummy JPanel for testing purposes.
Since I am absolutely horrid at working with LayoutManagers, I like to use the NetBeans built-in GUI Builder for all my layout work. Then I usually just code-around the autogenerated (GUI builder) code and that has always worked for me. It is the best of both worlds: I get my presentation looking exactly the way I want it, and I als开发者_C百科o get fine-grained control over the componentry.
However, I have never used the GUI Builder tool to make tables. After tinkering around with it for a while last night, it looks as though it is only good for making pretty basic (fixed # of rows, fixed # of columns, etc.) JTables.
My WidgetTable
actually has a dynamic number of both rows and columns, special editors/renderers and many other bells and whistles.
My problem:
I have two conflicting constraints: (1) I need to use the GUI builder to position and size the table exactly where I want it in the container, but, (2) The table component available through the GUI builder is too basic to handle my WidgetTable
.
I need a way to design a "table placeholder" into my container with the GUI builder, such that, once NetBeans autogenerates that placeholder code, I tweak the code and instruct it to dynamically instantiate one of my WidgetTables
instead, consuming the location and size that I defined the placeholder component to take up.
This way I can have my cake, and eat it too. The only problem is, I don't think the GUI builder supports this ability to drag-n-drop abstract JComponents
, position and size them, and then plug subclasses into them elsewhere in the codebase.
Anybody ever have this problem before or have any interesting recommendations? I imagine the best thing to do would be for me to just roll up my sleeves and learn LayoutManagers, but I'm mostly a server-side developer and only come over to the client-side every once in a blue moon; and honestly, don't have the energy to learn the intricacies and nastiness of GroupLayout and its sinister cousins.
Thanks for any help!
Insert a JTable using the GUI builder, reset its model property to the default value, and tweak the construction code so that it looks like
jTable1 = new WidgetTable(this.widgetTableModel);
You may tweak the creation code by right-clicking on the JTable, selecting "Customize code", choosing "custom creation" instead of "default code" in the first combo box, and typing the code for the constructor call.
If you need your jTable1 variable to be of type WidgetTable
rather than JTable
, edit the "Variable declaration code" in the same dialog box.
NetBeans also allows you to create custom components for building UIs. This may be more work than you want to put into your WidgetTable, but if you think you're going to have to build more UIs with custom components, it could be worth learning.
I do this all the time. I have an subclassed JTable that I use with the GUI editor and it is Dynamic.
- Add a JTable to your project using the GUI editor and the layout of your choice.
- Once the table is added, right click on it and click on custom code.
- In the constructor of the JTable, change it to say
new WidgetTable(new WidgetModel())
instead ofnew JTable(new DefaultTableModel())
. - Create a global variable for you WidgetTable. Something like
private WidgetTable widgetTable;
- In you constructor, after the call to initComponents(), cast your JTable to a Widget table and use that from now on. `widgetTable = (WidgetTable)jTable1;
精彩评论