tags:

views:

52

answers:

3

Hello,

I have a JTable whose associated TableModel could be initially empty. Therefore, it currently shows a JTable with its columns and no rows.

In order to fill this JTable, I want the user to drag and drop elements from another component. The problem is that I would like to hint the user that he/she should drag elements to this table, with some message like "Drag xxx here to add a row".

I thought that I could achieve this by putting a panel over the JTable , but I don't think it is possible with any java layout.

Does anyone know how to do this? Or should I stick to a CardLayout to switch to/from the hint and the JTable?

Thanks a lot

+1  A: 

A couple of suggestions:

  • Add a tooltip to the JTable using setToolTipText(String). You'll need to add it to the surrounding JScrollPane in order for the tooltip to be displayed when the user hovers on the empty viewport.
  • Add a titled Border; e.g. scrollPane.setBorder(BorderFactory.createTitledBorder("Drag items here"));
Adamski
The only problem is that the user must hover in order to get the tip... I was looking for a solution where the tip is already there... The second option is an interesting tip though (+1).
YuppieNetworking
+2  A: 

Take a look at OverlayLayout, I think it might do what you want.

Kevin K
Exactly. Thank you
YuppieNetworking
A: 

I would do it like this...Create a JPanel to contain the JTable on top and a JLabel on the bottom. Something like this...

JPanel container = new JPanel( new BorderLayout() );
container.add( table, BorderLayout.CENTER );
JLabel label = new JLabel( "Drag XXX here to add a row" );
container.add( table, BorderLayout.SOUTH );

Then make both the label and table drop targets. You might need to play with the label to make it look and behave right. Set opaque to true possibly? Set its background to white to match the table? Also maybe play with the borders of all three to make them look integrated with each other. You could remove the label after the first row is added if that is the behavior you want.

staticman