Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Title on top of jTable placed in JcontentPane.

Posted on 2005-04-18
7
Medium Priority
?
279 Views
Last Modified: 2013-11-23
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
Comment
Question by:DanFlyBoy
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13806438
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
 

Author Comment

by:DanFlyBoy
ID: 13807317
How do you add a JLabel to the JcontentPane?
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 150 total points
ID: 13807371

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:DanFlyBoy
ID: 13807380
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13807383
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13807388
okay, can you post *all* of your code so far please?
0
 

Author Comment

by:DanFlyBoy
ID: 13807553
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question