Reputation: 418
i am trying to design JTable but column name are not showing i don't know why the array are are declared can some one take a look at this piece of code table = new JTable(data, columnNames); i tried to look in java tutorial but i did not find it tell now
package AnimeAid;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
/**
*
* @author isslam
*/
public class GuiInterface extends JFrame {
JTable table;
public static void main(String[] args) {
GuiInterface is = new GuiInterface("t");
is.setVisible(true);
}
public GuiInterface(String title){
setSize(900, 700);
setTitle(title);
setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
String[] columnNames = {"#","Start","End","Translation column"};
Object[][] data = {
{"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
"full name for the record."},
{"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};
table = new JTable(data, columnNames);
table.setFillsViewportHeight(true);
table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
TableColumn columnA = table.getColumn("#");
columnA.setMinWidth(10);
columnA.setMaxWidth(20);
TableColumn columnB= table.getColumn("Start");
columnB.setMinWidth(80);
columnB.setMaxWidth(90);
TableColumn columnC= table.getColumn("End");
columnC.setMinWidth(80);
columnC.setMaxWidth(90);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
//contaner part
Container cp = getContentPane();
cp.add(table);
}}
Upvotes: 0
Views: 310
Reputation: 310
package com.tom.mom;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
/**
*
* @author isslam
*/
public class GuiInterface extends JFrame {
JTable table;
public static void main(String[] args) {
GuiInterface is = new GuiInterface("t");
is.setVisible(true);
}
public GuiInterface(String title){
setSize(900, 700);
setTitle(title);
setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
String[] columnNames = {"#","Start","End","Translation column"};
Object[][] data = {
{"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
"full name for the record."},
{"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};
table = new JTable(data, columnNames);
table.setFillsViewportHeight(true);
table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
TableColumn columnA = table.getColumn("#");
columnA.setMinWidth(10);
columnA.setMaxWidth(20);
TableColumn columnB= table.getColumn("Start");
columnB.setMinWidth(80);
columnB.setMaxWidth(90);
TableColumn columnC= table.getColumn("End");
columnC.setMinWidth(80);
columnC.setMaxWidth(90);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
}
since you already extended and the JFrame and you are working in the constructor of class which is nothing but a JFrame so you are in that position where there is no need to get the area and put your components.When we do this type of coding in constructor then we do not take getContentPane and place our components.It is required when you are working out of the constructor and trying to put some components in frame
Upvotes: 0
Reputation: 347334
The problem is, you've added the table to the content pane. This effectively removes it from the scroll pane, which is responsible for displaying the tables column headers.
Remove the following lines
Container cp = getContentPane();
cp.add(table);
Upvotes: 5