• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Title on top of jTable placed in JcontentPane.

I want to place a tiltle at the top center of A Jtable that is Placed on  A JcontentPane.  How can I do this. Do ineed to Place it on Jtable or JcontentPane?
0
DanFlyBoy
Asked:
DanFlyBoy
  • 4
  • 3
1 Solution
 
InteractiveMindCommented:
Dan,
I'm not sure exactly what you mean; are you referring to the title of a column, or just some text above a whole JTable?
If it's the latter, then you can either draw it on the JContentPane, or add a JLabel to the JContentPane.

?
0
 
DanFlyBoyAuthor Commented:
How do you add a JLabel to the JcontentPane?
0
 
InteractiveMindCommented:

it depends on how you've initialized the JContentPane.. Typically, it's something like this:

    getContentPane().add( new JLabel( "The Text you want to add" ) );

But, if you've done something like this instead:

   JPanel pane = new JPanel();
   setContentPane( pane );

then you would, of course, do something like this:

   pane.add( new JLabel( "The Text you want to add" ) );


Regards;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DanFlyBoyAuthor Commented:
I added this piece of code:

JLabel label = new JLabel("Text Label");
                jContentPane.add(label);

But it now overwrites my jTable, I wanted it above my jTable.
0
 
InteractiveMindCommented:
But, to sum all that up, try this:

   getContentPane().add( new JLabel( "The Text you want to add" ) );

Use that statement *before* adding the JTable to the ContentPane.
0
 
InteractiveMindCommented:
okay, can you post *all* of your code so far please?
0
 
DanFlyBoyAuthor Commented:
there are 3 programs.

1.
import javax.swing.*;
import java.awt.Container;
import java.awt.*;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;







/*
 * Created on Apr 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class testcase extends JApplet {
      int rowHeight;
//      YourMouseAdapter yourMouseAdapter = new YourMouseAdapter();
//      JLabel dateText = new JLabel();
//       The text is left-justified and top-aligned
//       The text is left-justified and vertically centered
   

      private javax.swing.JPanel jContentPane = null;
      

      Container c;
      Menu menu;
      

      private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="13,9"
      private JTable jTable = null;
      private JScrollPane scrollPane = null;
//      private JPanel jPanel = null;
      private JLabel jLabel = null;
      /**
       * This is the default constructor
       */
      public testcase() {
            super();
            init();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      public void init() {
      
             
            this.setSize(1000,700);
      
            c = getContentPane();// this section calls Menu
            menu = new Menu();
            setJMenuBar(menu.getMenuBar());
            
            
             
            
            
            
            this.setContentPane(getJContentPane());
      

      }
      
      
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      public javax.swing.JPanel getJContentPane() {
            if(jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(new java.awt.BorderLayout());
      //            jPanel.setLayout(new java.awt.GridBagLayout());
                  
//                  most outer pannel
                 
              JPanel MainPan = new JPanel();
              MainPan.setBackground(Color.YELLOW);
                MainPan.setPreferredSize(new Dimension(1270, 945));
                        
              jContentPane.add( new JScrollPane(getJTable()) ); // adds the table
                jContentPane.setBackground(java.awt.Color.DARK_GRAY);
                jContentPane.setForeground(java.awt.Color.DARK_GRAY);
                  //jContentPane.
                JLabel label = new JLabel("Text Label");
            //    jContentPane.add(label);
            }
            
            return jContentPane;
      }



      
      /**
       * This method initializes jFrame      
       *       
       * @return javax.swing.JFrame      
       */    
      
      /**
       * This method initializes jTable      
       *       
       * @return javax.swing.JTable      
       */
      public JTable getJTable() {
          if (jTable == null) {
                rowHeight = 8;
                jTable = new JTable(new MyTableModel());
                jTable.setRowHeight(30);  //sets the height of all rows
                jTable.setBackground(java.awt.Color.darkGray);
                
                // Stops coloumns from being able to be moved.
                jTable.getTableHeader().setReorderingAllowed(false);
          
                // Controls the Font color and size on Jtable.
            jTable.setGridColor(java.awt.Color.white);
            jTable.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 14));
         
            jTable.setForeground(java.awt.Color.white);
           
           
            // Controls display of selection of a row and/or column.
            jTable.setSelectionForeground(java.awt.Color.darkGray);
            jTable.setSelectionBackground(java.awt.Color.BLUE);
            jTable.setSelectionForeground(java.awt.Color.yellow);
           
           
            JMenuBar mb = new JMenuBar();      // declare and create new menu bar
            // The text is left-justified and top-aligned
          //  JLabel label = new JLabel("Text Label");
            JLabel label = new JLabel("Text Label");
                jTable.add(label);
                  }
                return jTable;      
     
          }  
      
       class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"test1",
                                        "test2",
                                        "test3",
                                        "test4",
                                        "test5",
                                                            "test6",
                                                            "test7",
                                                            "test8",
                                                            "test9",
                                                            "test10",
                                                            "test11",
                                                            "test12",
                                                            "test13"
                                                            };
        private Object[][] data = {
                {new Integer(1), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(2), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(3), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(4), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(5), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(6), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(7), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(8), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(9), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(10), "gt",
                      new Integer(10), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(11), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(12), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(13), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(14), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(15), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(16), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(17), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(18), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(19), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(20), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt6",
                      new Integer(21), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(22), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(23), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(24), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "},
                         {new Integer(1), "gt",
                      new Integer(25), new Integer(5), new Boolean(false), new Integer(6),"2", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Float(10), "   "}
               
            };

            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
           
       }
//       JTableHeader th = table.getTableHeader();
//       th.addMouseListener(yourMouseAdapter);

  //      }

}


      

program 2

/*
 * Created on Apr 13, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**

 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */



import java.awt.print.*;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

//import Framework.MyFrame;

public class Menu implements ActionListener{
     public JMenuBar menuBar = new JMenuBar();
     private JMenuItem mi[],mnuDates,mnuPrint,mnumfgOps,mnuExit,mnuContracts,mnuQuotations;
     private int maxX = 500;
     private int maxY = 500;
     public int numWindows = 0;
     private Point lastLocation = null;
     private JFrame jFrame = null;
     public Menu(){
           Container c;
           DateFile menu;
           Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        maxX = screenSize.width - 50;
        maxY = screenSize.height - 50;
     
           
           Color bcolor = new Color(153,204,255);
           JMenu fileMenu = new JMenu("File");
           fileMenu.setMnemonic('F');
           fileMenu.setBackground(bcolor);
           
           JMenu mnuReports = new JMenu("Reports");
           mnuReports.setMnemonic('R');
           mnuReports.setBackground(bcolor);

           JMenu mnuQuoteStats = new JMenu("Quotation Statistics");
           mnuQuoteStats.setMnemonic('S');
           mnuQuoteStats.setBackground(bcolor);

           JMenu mnuPartQuotes = new JMenu("Open Part Quotations");
           mnuPartQuotes.setMnemonic('O');
           mnuPartQuotes.setBackground(bcolor);

           JMenu mnuSuppQuotes = new JMenu("Open Supplier Quotations");
           mnuSuppQuotes.setMnemonic('S');
           mnuSuppQuotes.setBackground(bcolor);

           mnuReports.add(mnuQuoteStats);
           mnuReports.add(mnuPartQuotes);
           mnuReports.add(mnuSuppQuotes);

           mnuDates = new JMenuItem("Open Date File");
           mnuDates.addActionListener(this);
           mnuDates.setMnemonic('Q');
           mnuDates.setBackground(bcolor);
           fileMenu.add(mnuDates);
           mnuDates.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent event){
                  JDialog popup = new JDialog(jFrame,"Pop up window", true);
                  // fill popup.getContentPane() with controls
                 popup.pack();
                 DateFile menu;
                 popup.getContentPane();
                 popup.getRootPane();
                 
                  // set position if you don't want it to be shown at left-top corner
               

                popup.setSize(1000, 700);
                 popup.setBackground(Color.cyan);
                 popup.setVisible(true);
                 
            }
        });

           
           
           
           
     // Print section      
           
         
           mnuPrint = new JMenuItem("Print");
           mnuPrint.setMnemonic('P');
           mnuPrint.setBackground(bcolor);
           mnuPrint.addActionListener(this);
           fileMenu.add(mnuPrint);
           mnuPrint.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent event){
        //    JDialog popup = new JDialog(jFrame,"Pop up window", true);
            // fill popup.getContentPane() with controls
        //   popup.pack();
        //    DateFile menu;
        //    popup.getContentPane();
       //     popup.getRootPane();
     //            PrintUtilities.printComponent(this);
                 PrinterJob printJob = PrinterJob.getPrinterJob();
          //  printJob.setPrintable(this);
            if (printJob.printDialog())
              try {
                printJob.print();
              } catch(PrinterException pe) {
                System.out.println("Error printing: " + pe);
              }
            // set position if you don't want it to be shown at left-top corner
         

        //    popup.setSize(1000, 700);
        //    popup.setBackground(Color.cyan);
        //    popup.setVisible(true);
           
      }
  });
           
           
           JLabel label = new JLabel("Text Label");
           
//         The text is left-justified and top-aligned
           label = new JLabel("Text Label", JLabel.LEFT);
           label.setVerticalAlignment(JLabel.TOP);
           

           
           
           mnumfgOps = new JMenuItem("Create Manufacturing Operations");
           mnumfgOps.setMnemonic('M');
           mnumfgOps.setBackground(bcolor);
           mnumfgOps.addActionListener(this);
           fileMenu.add(mnumfgOps);
           fileMenu.add(mnuReports);
           fileMenu.setBorderPainted(false);
           mnuQuotations = new JMenuItem("Open Quotations");
           mnuQuotations.setMnemonic('U');
           mnuQuotations.setBackground(bcolor);
           mnuQuotations.addActionListener(this);
           mnuQuoteStats.add(mnuQuotations);
           mnuContracts = new JMenuItem("Contracts Received");
           mnuContracts.setMnemonic('C');
           mnuContracts.setBackground(bcolor);
           mnuQuoteStats.add(mnuContracts);

           mnuExit = new JMenuItem("Exit");
           mnuExit.setMnemonic('X');
           mnuExit.setBackground(bcolor);
           mnuExit.addActionListener(this);
           fileMenu.add(mnuExit);
           
           menuBar.setBackground(bcolor);
           menuBar.add(fileMenu);
           menuBar.add(mnuReports);
           menuBar.add(mnuQuoteStats);
           menuBar.add(mnuPartQuotes);
           menuBar.add(mnuSuppQuotes);

    }
     

     public JMenuBar getMenuBar(){
          return menuBar;
     }
     public void actionPerformed(ActionEvent e){
 
     }
 
     
}

program 3

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
/*
 * Created on Apr 14, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 *
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DateFile extends JFrame {

      private javax.swing.JPanel jContentPane = null;

      private JTable jTable = null;
      private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="79,34"
      Container c;
      
      /**
       * This is the default constructor
       */
      public DateFile() {
            super();
            initialize();
            c = getContentPane();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      private void initialize() {
            this.setSize(300,200);
            this.setContentPane(getJContentPane());
            this.setTitle("JFrame");
      }
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      private javax.swing.JPanel getJContentPane() {
            if(jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(new java.awt.BorderLayout());
            }
            return jContentPane;
      }
      /**
       * This method initializes jTable      
       *       
       * @return javax.swing.JTable      
       */    
      private JTable getJTable() {
            if (jTable == null) {
                  jTable = new JTable();
            }
            return jTable;
      }
      /**
       * This method initializes jScrollPane      
       *       
       * @return javax.swing.JScrollPane      
       */    
      private JScrollPane getJScrollPane() {
            if (jScrollPane == null) {
                  jScrollPane = new JScrollPane();
                  jScrollPane.setViewportView(getJTable());
                  jScrollPane.setSize(380, 178);
            }
            return jScrollPane;
      }
  }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now