Link to home
Start Free TrialLog in
Avatar of razo
razo

asked on

Create a schedule

i need to create a schedule  of appointments using java
the rows heading of the table signify the day while the columns heading are used for time
the user can also have the option of merging multiple rows for a given day to create an appointment that extend for multiple hours

i need this urgently
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

you want us to write the whole thing?

We can't do your homework for you...

Do you have a specific problem?
Avatar of razo
razo

ASKER

first it is not a homework
second i only need an outline like to use for the schedule; does JTable works for that?
 and how i can i merge cells
>> does JTable works for that?
Yeah, why not.

>> how i can i merge cells
You can't. But why should you?
You can change the background color to indicate an appointment that extends for multiple hours.
Hello zzny, the java JTable has only a basic functinality. There are several other grid controls (see www.dundas.com or http://www.quest.com/jclass/index.asp) wich provide an enhance functionality. (Sorry they are not free)
>> Hello zzny
Are you talking to me?

>>the java JTable has only a basic functinality
Which is enough for what the user asks.
Look to Outlook's Calender: No merging of cells when indicating appointements of more than one hour.
Avatar of razo

ASKER

i examined JTable yesterday and i couldn't find a way to put row headings
as for merging cells it is not necessary to merge but i want to give the user to be able to add an appointments thru the grid to extend for multiple hours...it is not only for view
>> i want to give the user to be able to add an appointments thru the grid to extend for multiple hours
That can be done by selecting multiple cells (for one row/day)

>>i examined JTable yesterday and i couldn't find a way to put row headings
Did you put your table in a JScrollPane?
>> That can be done by selecting multiple cells.
Play around with this *very simple* example. Hopefully it gives you ideas.

/*
 * TableDemo.java
 *
 */

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
/**
 *
 */
public class TableDemo extends javax.swing.JFrame {

    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable theTable;
   
    /** Creates new form TableDemo */
    public TableDemo() {
        initComponents();
        initTable();
    }
   
    private void initTable() {
        theTable.setModel(new MyOwnTableModel());
        theTable.setCellSelectionEnabled(true);
    }
   
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        theTable = new javax.swing.JTable();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jScrollPane1.setViewportView(theTable);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            System.exit(0);
        }
       
        new TableDemo().show();
    }
   
    public class MyOwnTableModel extends DefaultTableModel {
   
        final String[] columnNames = { "Day",
                                       "08:00-08:15", "08:15-08:30",
                                       "08:30-08:45", "08:45-09:00" };
                                       
        private Object data[][] = {
                {"Monday", " ", " ", " ", " "},
                {"Tuesday", " ", " ", " ", " "},
                {"Wednesday", " ", " ", " ", " "},
                {"Thursday", " ", " ", " ", " "},
                {"Friday", " ", " ", " ", " "},
                {"Saturday", " ", " ", " ", " "},
                {"Sunday", " ", " ", " ", " "},
        };
       
        public MyOwnTableModel() {
        }
        public boolean isCellEditable(int row, int col) { return false; }
        public Class getColumnClass(int col) {
            return super.getColumnClass(col);
        }        
        public int getRowCount() { return 7; }
        public int getColumnCount() { return 5; }
        public String getColumnName(int col) {
            return columnNames[col];
        }
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
     }
}
Avatar of razo

ASKER

ok that is exactly what i need but please i dont have time to research mutiple selection
please can u explain how can i do multiple selection and how can i allow the user to do that at run-time when adding data
thanks
Replace initTable() by this extended version.
And see the tracing when selecting multiple rows/columns.


    private void initTable() {
        theTable.setModel(new MyOwnTableModel());
        theTable.setCellSelectionEnabled(true);
        theTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        ListSelectionModel rowSM = theTable.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    //Ignore extra messages.
                    if (e.getValueIsAdjusting()) return;
                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    if (lsm.isSelectionEmpty()) {
                        System.out.println("No rows are selected.");
                    } else {
                        int selectedRowFrom = lsm.getMinSelectionIndex();
                        int selectedRowTo = lsm.getMaxSelectionIndex();
                        if (selectedRowTo==selectedRowFrom)
                            System.out.println("Row " + selectedRowFrom
                                           + " is now selected.");
                        else
                            System.out.println("Rows " + selectedRowFrom
                                               + " to " + selectedRowTo + " are now selected.");
                    }
                }
            });
        ListSelectionModel colSM = theTable.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    //Ignore extra messages.
                    if (e.getValueIsAdjusting()) return;

                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    if (lsm.isSelectionEmpty()) {
                        System.out.println("No columns are selected.");
                    } else {
                        int selectedColFrom = lsm.getMinSelectionIndex();
                        int selectedColTo = lsm.getMaxSelectionIndex();
                        if (selectedColFrom==selectedColTo)
                            System.out.println("Column " + selectedColFrom
                                               + " is now selected.");
                        else
                            System.out.println("Columns " + selectedColFrom
                                               + " to " + selectedColTo + " are now selected.");
                    }
                }
        });
    }
Avatar of razo

ASKER

it is givin a syntax error on ListSelectionListener
do i have to import anything other what i imported at the beginning?
Sorry. Add:

     import javax.swing.event.*;
Avatar of razo

ASKER

ok one last thing there is a bug when i edit a cell i am not being able to select another cell
So, you mean:
When editing one cell, you're not able to select another cell.
What happens? Do you get an error?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of razo

ASKER

ok thanks look ill give u 100 more points if u could get me a way that when selecting 2 rows of the same column the lines between the two rows is removed
First of all, thanks for accepting. That keeps us answering your future questions too.

About your extra Q:
Do you mean this:
you select row 4 and row 7 (of say column 3),
then - I suppose - you press a button <delete>
then you want row 5 and 6 to be removed?
Is it something like that?
Avatar of razo

ASKER

hi
just wanted if u can do it or not???
i appreciate ur answer either way
thanks
Avatar of razo

ASKER

no what i mean if the user selects rows 3 to 7 of a certain colum and he adds an appointment
then he sees the three columns having the same background color but with out having the dividing lines between them, the small line that distinguish a row from another row in the same column
Mmmm.
I know the JTable functions:

 void setShowGrid(boolean showGrid)
          Sets whether the table draws grid lines around cells.
 void setShowHorizontalLines(boolean showHorizontalLines)
          Sets whether the table draws horizontal lines between cells.
 void setShowVerticalLines(boolean showVerticalLines)
          Sets whether the table draws vertical lines between cells.

but that's for the whole table, not for individual cells.
In coding everything is possible ;°)
So, there must be some way...
... but I can't find one. :(
Sorry.
Avatar of razo

ASKER

thanks for trying
look can u try to put in an editable cell a textbox that holds the data and whose height could be changed???
something like outlook calander
i really would appreciate it and dont worry about points if it hard and doable i can put more than 100 points
Don't know if I understand...

>> a textbox that holds the data and whose height could be changed
Of no use. The cell height won't change because of containing a higher object.

If you want to change the height of a table row you can use JTable's setRowHeight() function:

public void setRowHeight(int row, int rowHeight)

Sets the height for row to rowHeight, revalidates, and repaints. The height of the cells in this row will be equal to the row height minus the row margin.

Parameters:
row - the row whose height is being changed
rowHeight - new row height, in pixels
Throws:
IllegalArgumentException - if rowHeight is less than 1

>>something like outlook calander
I don't see variable row heights in Outlook's calendar
???

>> i really would appreciate it and dont worry about points if it hard and doable i can put more than 100 points
That's OK. (I guess you mean you'll post another Q than?)
But first I have to know what exactly you want.
Avatar of razo

ASKER

ok if u see outlook calandar when u add an appointment it shown as a text box in the the cell. this textbox can then be modified by increasing its length so that it extends over multiple cells....if we can do this than i don't need to change the cells at all

as for the points do i have to post another question? can i give you the extra points from here?
I see what you mean.
But that's not so easy.
First, you have to change the cursor when on a limit of an appointment (<> every horizontal line)
Secondly you have to change the background of the cells you move to while dragging.

I saw it quite easier:
1) Defining an appointment:
   - Select a range of cells
   - right-click and choose new appointment
   - new appointment dialog comes up (with the from/to times according to the selected cells; can be changed of course)
   - When closed possibly update the cells according to the changed time window

2) Changing the time window of an existing appointment (only not graphically)
   - right-click the appointment and choose edit (or somehting)
   - appointment dialog comes up again
   - When closed possibly update the cells according to the changed time window

I admit: being able to change the time window graphically is cool, but is it really a must for you?

>>can i give you the extra points from here?
No you can't. Once a Q is closed the points can't be incremented. (btw. more than 500 points is also impossible)
So posting another Q is the only option.
Avatar of razo

ASKER

ok i need it graphically it is important
i am ready to give u another 500 points if u can do it
but i have an idea if we add a textbox to the cell and then the user extends this textbox...not the cell, is that possible?
ok if u could do anything graphically can u let me know so that i post the new question
>> if we add a textbox to the cell and then the user extends this textbox...not the cell, is that possible?
I'm afraid not. :(
You can define some text box as cell editor. But what then?
You can't update your table until you're done editing.
And in this case we want the table (cells) to follow the "editing". So... impossible I think.

If we want to do something like this, I think we will need to use a MouseListener and a MouseMotionListener on the table.
And then reacting (= adapting the selections of table cells) on mouse(drag)movements.
I'll see if I can (and find the time to) adapt the demo app with something like that.

I think it won't hurt to post a new Q.
Two or more experts always know more than one ;°)
(And if your Q isn't answered in your opinion, you can always make a request to delete it and refund the points)
Avatar of razo

ASKER

ok ill post a new question