0

Hello i have a hard time solving this puzzle. professor gave the code and told us to change it to make column head clickable.can anyone change it for me.? wasted almost 4 hours :( not too great in java swings below is the code... below is the code :

package academic.emailClient.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
import academic.emailClient.model.Message;
import academic.emailClient.view.entities.MessageView;

public class MailBox extends JFrame {


JTable tableinbox;

 private final int MAILBOX_FRAME_WIDTH = 1080; 
 private final int MAILBOX_FRAME_HEIGHT = 650; 
 static final boolean VISIBLE = true;

public MailBox() {
    Object[] columnNamesinbox = { "Boolean", "Sender", "Subject", "Body" };
    Object[][] datainbox = {
            { false, "5@gmail.com", "D", "T" },
            { false, "r@gmail.com", "projhsdject work", "I " },
            { false, "r.@ymail.com", "Job Placement","Iut that" },
            { true, "yam@gmail.com", "s1", "as" }
            };

    DefaultTableModel modelinbox = new DefaultTableModel(datainbox,
            columnNamesinbox);
    tableinbox = new JTable(modelinbox) {

        @Override
        public Class getColumnClass(int columninbox) {
            switch (columninbox) {
            case 0:
                return Boolean.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return String.class;
            default:
                return Boolean.class;
            }
        }
    };




    tableinbox.setBackground(Color.lightGray);
    tableinbox.setGridColor(new Color(0,128,0));
    JTableHeader inboxheader = tableinbox.getTableHeader();
    inboxheader.setBackground(Color.DARK_GRAY);
    inboxheader.setForeground(Color.white);

}


public int getMesssagesCount(int i) {
    return 0;
}

public int GetCheckedItemsNumber(int i) {
    return 0;
}

public Object[][] GetCheckedItems() {

    return GetCheckedItems();
}

public Object[][] GetMail() {
    return GetMail();
}

public Object[][] OrderByDate() {

    return OrderByDate();
}

public Object[][] OrderBySender() {
    return OrderBySender();
}

public Object[][] OrderBySubject() {
    return OrderBySubject();
}

public JTable getMailBox() {
    return tableinbox;
}


public void run() {

    JFrame MailBoxFrame = new JFrame();

    MailBoxFrame.setMaximumSize(new Dimension(MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT));
    MailBoxFrame.setPreferredSize(new Dimension(MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT));
    MailBoxFrame.setBounds(0, 0, MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT);
    MailBoxFrame.setVisible(VISIBLE);
    MailBoxFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
  • 1
    See [How to Write a Mouse Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html). Also, follow Java naming conventions. `MailBoxFrame` - variable names should not start with an upper case character. `MAILBOX_FRAME_WIDTH` - only "static final" variables should be completely upper cased. – camickr May 18 '13 at 19:34
  • and int i = inboxheader.columnAtPoint(e.getPoint()); is rest for success, – mKorbel May 18 '13 at 20:06

2 Answers2

1

professor gave the code and told us to change it to make column head clickable

See JTable.setAutoCreateRowSorter(true). E.G. This is what you might see after clicking the Cat. column header. the small upwards pointing arrow indicates the table is sorted on that column.

Clickable table header

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

if you just want to sort your table by clicking the columnheaders, than you simply have to create your own table model and overwrite the get column class function eg.

public class TableViewModel extends AbstractTableModel{

@Override
public Class<?> getColumnClass( int column ) {
    switch(column) {
        case 0:
            return String.class;
        case 1:
            return String.class;

. . .

and after that bind your JTable with that tablemodel and sorter by

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(<YOURJTABLE>.getModel());

.setRowSorter( sorter );