1
votes

Have a table I want to show in a panel when I click on a button. I am able to get the table to show, but I am not able to the header to show. Have also tried using the JscrollPane, but here nothing shows ups at all.

Main part of code:

public class GUI extends JFrame { JButton button; static JFrame frame; JTable tableA; JPanel TablePanel;

public static void main(String[] args) {
    frame = new GUI();

}

public GUI() {
    frame = new JFrame();
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    JPanel MainPanel = new JPanel();
    MainPanel.setBackground(new Color(0, 0, 0));
    MainPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
            null, null, null));
    MainPanel.setBounds(0, 0, screen.width, screen.height - 60);
    MainPanel.setPreferredSize(screen);
    frame.getContentPane().add(MainPanel);
    MainPanel.setLayout(null);

    button = new JButton("Show Assignments");
    button.setFont(new Font("Tahoma", Font.PLAIN, 10));
    button.setBounds(MainPanel.getWidth() / 2 - 65, 40, 130, 20);
    MainPanel.add(button);

    TablePanel = new JPanel();
    TablePanel.setBackground(new Color(245, 222, 179));
    TablePanel
            .setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    TablePanel.setBounds(160, 85, MainPanel.getWidth() - 170,
            MainPanel.getHeight() - 95);
    MainPanel.add(TablePanel);
    TablePanel.setLayout(null);

    button.addActionListener(new MyActionListener());

    frame.setExtendedState(frame.MAXIMIZED_BOTH);
    frame.setVisible(true);

}

What happens when you click button.

 class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            String[] columnNames = { "AssignmentID", "Assignmenttype",
                    " AssignmentDesricption", "ClientID", "Status" };
            Object[][] data = {
                    {
                            "028079",
                            "Software Support",
                            "Danzafe is in need software support on their mail program.",
                            "25890", "Waiting" },
                    {
                            "028080",
                            "Software Installing",
                            "Danzafe is in need of installation of new software on thier server.",
                            "25890", "In Progress" },
                    { "028081", "Hardware Setup",
                            "Expert Beslag needs to get a new local server setup.",
                            "25891", "In Progress" },
                    {
                            "028082",
                            "Hardware Support",
                            "Expert Beslag have a computer that is making troubles with the wireless internet card",
                            "25891", "Waiting" },
                    {
                            "028083",
                            "Balacing Question",
                            "Blizzard needs help balacing out the class of World of Warcraft",
                            "00001", "Never Ends" } };

            tableA = new JTable(data, columnNames);
            tableA.setBounds(5, 35, TablePanel.getWidth() - 15,
                    TablePanel.getHeight() - 15);

            TablePanel.add(tableA.getTableHeader());
            tableA.setFillsViewportHeight(true);
            tableA.setVisible(true);
            TablePanel.add(tableA);
            TablePanel.repaint();
        }
    }
}
2
Please learn java naming conventions and stick to them.kleopatra

2 Answers

3
votes
  • have to put JTable into JScrollPane, then JTableHeader should be visible

  • or to change Layout Manager for JPanel from default FlowLayout to the BorderLayout

  • get JTableHeader from JTable and to put to the NORTH area

  • proper way is usage of JScrollPane only

  • don't to use setBounds(), use built_in BorderLayout for JFrame and put JScrollPane (only maybe there isn't reason to use JPanel, only in the case that you'll to planing to use CardLayout) to the CENTER area

  • rest of JComponent(s) put to the separate JPanel, this JPanel put to the NOTHR or SOUTH area came from JFrame

0
votes

You have to put the table into scroll pane, and then add scroll pane into label. It should work this way, you may not have to do more than this.