4

I have a JTable which is created using a TableModel JTable t = new JTable(tableModel) I want to add a title to it. I was hoping for something like t.setTitle(graphTitle); but i cant find anything on that lines. I dont mind if the title is on top or below the table. I was using JLabels but it just looks messy.

Can anyone help? Cheers in advance

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Josh
  • 818
  • 2
  • 16
  • 27
  • Placing a JLabel above the JScrollPane containing the JTable looks like the best and easiest solution to me. Why does it look messy? – JB Nizet Mar 02 '12 at 12:18

4 Answers4

15

Another option you could consider is enclosing the JTable in a JPanel and setting a TitledBorder to that JPanel.

Like this:

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class TableTitle
{
    public TableTitle ()
    {
        JFrame frame = new JFrame ();
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel ();
        panel.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
                                                            "Table Title",
                                                            TitledBorder.CENTER,
                                                            TitledBorder.TOP));


        JTable table = new JTable (3, 3);

        panel.add (table);

        frame.add (panel);

        frame.setLocationRelativeTo (null);
        frame.pack ();
        frame.setVisible (true);
    }

    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override public void run ()
            {
                TableTitle t = new TableTitle ();
            }
        });
    }
}

It looks like this:

screenshot1

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • @kleopatra I didn't really focus on that. Just wanted to make the program work :) . – Radu Murzea Mar 02 '12 at 14:14
  • 2
    basics should be correct without needing to focus on them ;-) Or in other words: please edit your example – kleopatra Mar 02 '12 at 14:18
  • 1
    -1 for null Layout (late, probably didn't see it earlier due to the eye hurting naming - +1 for cleaning those up ;-) – kleopatra Jul 13 '12 at 08:44
  • @kleopatra -1 and + 1 equals 0. So why -1 ? Yes, I know null-layout is bad, I was in the habit of constantly using it. I got out of it lately, it wasn't easy :D . – Radu Murzea Jul 13 '12 at 08:58
  • as an incentive to edit, again :-) Errors have a fatal tendency to propagate ... see f.i. another typical and maybe equally frequent mistake http://stackoverflow.com/questions/11464791/how-to-add-image-in-jpanel – kleopatra Jul 13 '12 at 09:12
  • @kleopatra OK, I fixed it :) . I should check to see if I posted something similar in the past. – Radu Murzea Jul 13 '12 at 10:33
  • cool - now on to remove the setPreferredSize. Yeah, I'm trying to drive you to churn out the best :-) – kleopatra Jul 13 '12 at 10:47
  • @kleopatra I know that `setPreferredSize` is the way to go, layout managers rely on it to draw stuff. What do you consider to be a better alternative ? – Radu Murzea Jul 13 '12 at 10:56
  • the table cares for itself - why do you want to interfere? As to why/what/not related to setXXSize: http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519 – kleopatra Jul 13 '12 at 11:02
  • @kleopatra Done :) . I will try to read that question and all those links. This topic on layout managers is one that is very poorly covered (from a best-practice point-of-view) and it can be super easy to fail at doing things right. – Radu Murzea Jul 13 '12 at 11:11
2

You can also add your title the JScrollPane which inscribes jtable in it.

Code:

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBorder(BorderFactory.createTitledBorder ("Table Title"));
2

You would have to add it when you instantiate your DefaultTableModel:

String data[][] = {{"Vinod","MCA","Computer"},
{"Deepak","PGDCA","History"},
{"Ranjan","M.SC.","Biology"},
{"Radha","BCA","Computer"}};

String col[] = {"Name","Course","Subject"};

DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model);

If it already exists, you can do something like this:

ChangeName(table,0,"Stu_name");
ChangeName(table,2,"Paper");

public void ChangeName(JTable table, int col_index, String col_name){
    table.getColumnModel().getColumn(col_index).setHeaderValue(col_name);
}

Courtesy of RoseIndia.net

Hope that helps.

Carlos
  • 1,897
  • 3
  • 19
  • 37
0

I don't think you have much options here. JTable has no functionality to add a titlebar. So using JLabel or other components is your only option. Try putting the JTable in a JTabbedPane.

D-rk
  • 5,513
  • 1
  • 37
  • 55