Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JTable - multiple lines in a cell AND cells not editable?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question JTable - multiple lines in a cell AND cells not editable?

    Hi all,

    I have a bit of a problem.

    I'm using JTable for displaying a calendar.

    It is important that more than 1 line is displayed in a cell. To do this, I wrote class MultiLineCellRenderer that extends JTextArea and implements TableCellRenderer (solution I found on the Internet).

    Then I realised that the cells should not be editable. For this, I wrote class MyTableModel that extends AbstractTableModel (again, solution from the Internet).

    So I apllied both to the table, like this:

    table.setModel(new MyTableModel(data, columnNames));
    table.setDefaultRenderer(String.class, new MultiLineCellRenderer());

    The problem: It looks like I can use either one or the other. If I use the renderer for my table, multiple lines are shown in cells, but the cells are editable. If I use the table model, or both, then the cells are not editable, but only 1 line is displayed in each cell

    Any suggestions? Can the renderer and the table model be used together?

    Thanks in advance!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: JTable - multiple lines in a cell AND cells not editable?

    I'm not sure why that would be the case. You should probably post an SSCCE (that's a link) that demonstrates what you're talking about. Make sure it's as short as possible while also being runnable and demonstrating the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JTable - multiple lines in a cell AND cells not editable?

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure why that would be the case. You should probably post an SSCCE (that's a link) that demonstrates what you're talking about. Make sure it's as short as possible while also being runnable and demonstrating the problem.
    thanks, I just found out what the problem was my renderer was for string class, but table model worked with object class...

  4. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: JTable - multiple lines in a cell AND cells not editable?

    I m giving u the code which i have written 2 years back for my college project.

    This is not exactly using table but calendar is there.

    package developmentkit.mylabels;

    import javax.swing.*;
    import java.awt.*;

    public class CalLabel extends JLabel{

    private Color cl;
    private boolean blank;
    private int date,month,year,day;

    public CalLabel(String string) {
    super(string);
    }

    public CalLabel() {
    super();
    }


    public void setColor(Color cl){
    this.cl=cl;
    }

    public Color getColor(){
    return cl;
    }

    public void setBlank(boolean blank){
    this.blank = blank;
    }

    public boolean getBlank(){
    return blank;
    }

    public void setDate(int date){
    this.date = date;
    }


    public void setMonth(int month){
    this.month = month;
    }

    public void setYear(int year){
    this.year = year;
    }

    public void setDay(int day){
    this.day = day;
    }


    public int getDate(){
    return date;
    }

    public int getMonth(){
    return month;
    }

    public int getYear(){
    return year;
    }

    public int getDay(){
    return day;
    }

    }


    package developmentkit.calendar;

    import developmentkit.mylabels.*;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.*;
    import java.util.Date;
    import java.awt.event.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.*;

    public class Calendar extends JDialog implements MouseListener,ChangeListener,ActionListener{
    private CalLabel lbl[][];
    private CalLabel lblDays[];
    private JButton btnMonth;
    private JButton btn[];

    private int tDate,tMonth,tYear,tDay,sDate,sMonth,sYear,sDay;
    private final String day[]={"Mon","Tue","Wed ","Thu","Fri","Sat","Sun"};
    private final String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"};
    private Color clTemp,colorHoliday;
    private Date dObj;
    private int currYear,currMonth,currDate,currDay;
    private JPanel pnlTop,pnlBottom,pnlMonth;
    private JButton btnMonths[][];
    private final int days[][]={
    {31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30 ,31,30,31,31,30,31,30,31}
    };
    private Color colorCurrentDate,colorSelectedDate,colorBackground ,colorText;
    private JSpinner spinnerYear;
    private int x,y;

    public Calendar(){
    this(10,10);
    }

    public Calendar(int x,int y){
    dObj= new Date();
    this.x = x;
    this.y=y;
    this.currDate = dObj.getDate();
    this.currMonth = (dObj.getMonth()+1);
    this.currYear = (dObj.getYear()+1900);
    setCalendar();
    }

    public Calendar(int currDate,int currMonth,int currYear,int x,int y){
    this.currDate=currDate;
    this.currMonth=currMonth;
    this.currYear=currYear;
    this.x=x;
    this.y=y;
    setCalendar();
    }

    public Calendar(int currDate,int currMonth,int currYear) {
    this(currDate,currMonth,currYear,10,10);
    }

    public void setCalendarColor(){
    colorCurrentDate=Color.blue;
    colorSelectedDate=Color.orange;
    colorBackground=Color.white;
    colorText=Color.black;
    colorHoliday=Color.red;
    }

    public void setCurrentDateColor(Color colorCurrentDate){
    this.colorCurrentDate=colorCurrentDate;
    }

    public void setSelectedDateColor(Color colorSelectedDate){
    this.colorSelectedDate=colorSelectedDate;
    }

    public void setCalendarBackgroundColor(Color colorBackground){
    this.colorBackground=colorBackground;
    }

    public void setCalendarTextColor(Color colorText){
    this.colorText=colorText;
    }

    public void setHolidayColor(Color colorHoliday){
    this.colorHoliday=colorHoliday;
    }

    public Color getCurrentDateColor(){
    return this.colorCurrentDate;
    }

    public Color getSelectedDateColor(){
    return this.colorSelectedDate;
    }

    public Color getCalendarBackgroundColor(){
    return this.colorBackground;
    }

    public Color getCalendarTextColor(){
    return this.colorText;
    }


    public Color getHolidayColor(){
    return this.colorHoliday;
    }

    public void actionPerformed(ActionEvent ae){

    int index=0;

    if(ae.getSource()==btnMonth){
    pnlMonth.setVisible(true);
    pnlBottom.setVisible(false);
    printCalendar(tMonth,tYear);
    }
    else{
    if(ae.getSource()==btnMonths[0][0]){
    index=1;
    }
    else if(ae.getSource()==btnMonths[0][1]){
    index=2;
    }
    else if(ae.getSource()==btnMonths[0][2]){
    index=3;
    }
    else if(ae.getSource()==btnMonths[0][3]){
    index=4;
    }
    else if(ae.getSource()==btnMonths[1][0]){
    index=5;
    }
    else if(ae.getSource()==btnMonths[1][1]){
    index=6;
    }
    else if(ae.getSource()==btnMonths[1][2]){
    index=7;
    }
    else if(ae.getSource()==btnMonths[1][3]){
    index=8;
    }
    else if(ae.getSource()==btnMonths[2][0]){
    index=9;
    }
    else if(ae.getSource()==btnMonths[2][1]){
    index=10;
    }
    else if(ae.getSource()==btnMonths[2][2]){
    index=11;
    }
    else if(ae.getSource()==btnMonths[2][3]){
    index=12;
    }

    tMonth=index;
    pnlMonth.setVisible(false);
    pnlBottom.setVisible(true);
    printCalendar(tMonth,tYear);
    btnMonth.setText(months[tMonth-1]);

    }//else


    }


    final public void setResizable(boolean b){
    super.setResizable(false);
    }

    private void setCalendar(){
    getContentPane().setSize(210,190);
    setResizable(false);
    getContentPane().setLayout(new FlowLayout());
    getContentPane().setFont(new Font(Font.SANS_SERIF,Font.PLAIN,10));
    pnlTop = new JPanel();
    pnlTop.setLayout(new FlowLayout());

    pnlBottom = new JPanel();
    pnlBottom.setLayout(new GridLayout(7,7));
    pnlBottom.setSize(170,170);
    pnlMonth = new JPanel(new GridLayout(4,3));
    pnlMonth.setSize(200,110);

    add(pnlTop);
    add(pnlMonth);
    add(pnlBottom);
    pnlMonth.setVisible(false);
    lblDays=new CalLabel[7];
    btnMonth = new JButton();
    btnMonth.addActionListener(this);

    spinnerYear = new JSpinner();
    spinnerYear.addChangeListener(this);
    spinnerYear.setSize(100,100);

    lbl=new CalLabel[6][7];

    pnlTop.add(btnMonth);
    pnlTop.add(spinnerYear);

    btnMonths=new JButton[3][4];

    for(int i=0,k=0;i<3;i++){
    for(int j=0;j<4;j++){
    btnMonths[i][j] = new JButton(months[k]);
    btnMonths[i][j].addActionListener(this);
    pnlMonth.add(btnMonths[i][j]);
    k++;
    }
    }

    for(int i=0;i<7;i++){
    lblDays[i]=new CalLabel(day[i]);
    lblDays[i].setHorizontalAlignment(0);
    pnlBottom.add(lblDays[i]);
    }


    for(int i=0;i<6;i++){

    for(int j=0;j<7;j++){
    lbl[i][j] = new CalLabel();
    lbl[i][j].addMouseListener(this);

    }//inner for
    }//outer for

    setCalendarColor();

    }





    public final void showCalendar(){
    showCalendar(this.x,this.y);
    }

    public final void showCalendar(int x, int y){
    this.x=x;
    this.y=y;

    setLocation(x, y);
    setModal(true);
    setAlwaysOnTop(true);
    for(int i=0;i<6;i++){
    for(int j=0;j<7;j++){
    lbl[i][j].setOpaque(true);
    lbl[i][j].setHorizontalAlignment(0);
    lbl[i][j].setForeground(colorText);
    lbl[i][j].setBackground(colorBackground);
    lbl[i][j].setColor(colorBackground);
    lbl[i][j].setBlank(true);
    lbl[i][j].setEnabled(false);
    pnlBottom.add(lbl[i][j]);
    }//inner for
    }//outer for


    printCalendar(currMonth,currYear);
    spinnerYear.setValue(currYear);
    btnMonth.setText(months[currMonth-1]);

    setVisible(true);


    }




    public final void printCalendar(int month,int year){
    int baseYear = 1;
    int exDays=0;
    exDays = year - baseYear;

    for(int i=baseYear;i<year;i++){

    if(isLeap(i)==1)
    exDays++;

    }//for


    int currLeap=isLeap(year);

    for(int i=0;i<month-1;i++){
    exDays+=days[currLeap][i];
    }//for

    exDays=exDays%7;

    tMonth = month;
    tYear = year;




    for(int i=0;i<6;i++){

    for(int j=0;j<7;j++){
    lbl[i][j].setBlank(true);
    lbl[i][j].setText("");
    lbl[i][j].setForeground(colorText);
    lbl[i][j].setBackground(colorBackground);
    lbl[i][j].setColor(colorBackground);
    lbl[i][j].setMonth(month);
    lbl[i][j].setYear(year);
    lbl[i][j].setDay(0);
    lbl[i][j].setEnabled(false);
    }//inner for
    }//outer for


    int k=1,j=0;

    for(int i=0;i<6;i++){

    for(j=0;k<=days[currLeap][month-1] && j<7;j++){

    if(k==currDate && month==currMonth && year==currYear){
    lbl[i][j].setForeground(colorCurrentDate);
    currDay=j;

    }//if

    if(exDays!=0){
    lbl[i][j].setText("");
    exDays--;
    }//if
    else{
    lbl[i][j].setText(String.valueOf(k));
    lbl[i][j].setDate(k);
    lbl[i][j].setMonth(tMonth);
    lbl[i][j].setYear(tYear);
    lbl[i][j].setDay(j);
    lbl[i][j].setEnabled(true);
    if(currDate==k)
    tDay=j;

    lbl[i][j].setToolTipText(lbl[i][j].getText()+months[lbl[i][j].getMonth()-1]+lbl[i][j].getYear());
    lbl[i][j].setBlank(false);

    k++;
    }//else


    }//inner for
    lbl[i][6].setForeground(colorHoliday);


    }//outer for


    }//function print calender





    public void mouseClicked(MouseEvent e) {

    CalLabel temp=new CalLabel();
    temp=(CalLabel)e.getSource();

    if(temp.getBlank()==false){
    setSelectedDate(temp.getDate());
    setSelectedDay(temp.getDay());
    setSelectedMonth(temp.getMonth());
    setSelectedYear(temp.getYear());
    temp.setColor(clTemp);
    temp.setBackground(clTemp);
    dispose();
    }


    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {
    CalLabel temp=new CalLabel();
    temp=(CalLabel)e.getSource();

    if(temp.getBlank()==false) {
    clTemp = temp.getColor();
    temp.setColor(colorSelectedDate);
    temp.setBackground(colorSelectedDate);
    }
    }

    public void stateChanged(ChangeEvent e) {
    if(e.getSource()==spinnerYear){
    tYear=Integer.parseInt(String.valueOf(spinnerYear. getValue()));
    printCalendar(tMonth,tYear);
    }
    }

    public void mouseExited(MouseEvent e) {
    CalLabel temp=new CalLabel();
    temp=(CalLabel)e.getSource();
    if(temp.getBlank()==false){
    temp.setColor(clTemp);
    temp.setBackground(clTemp);

    }
    }



    public int isLeap(int y){

    if(y%100==0){
    if(y%400==0)
    return 1;
    else
    return 0;
    }
    else if(y%4==0)
    return 1;
    else
    return 0;
    }

    public void setCurrentDate(int currDate)
    {
    this.currDate=currDate;
    }

    public void setCurrentDay(int currDay)
    {
    this.currDay=currDay;
    }

    public void setCurrentMonth(int currMonth)
    {
    this.currMonth=currMonth;
    }

    public void setCurrentYear(int currYear)
    {
    this.currYear=currYear;
    }

    public int getCurrentDate()
    {
    return currDate;
    }

    public int getCurrentDay()
    {
    return currDay;
    }

    public int getCurrentMonth()
    {
    return currMonth;
    }

    public int getCurrentYear()
    {
    return currYear;
    }

    public void setSelectedDate(int sDate)
    {
    this.sDate=sDate;
    }

    public void setSelectedDay(int sDay)
    {
    this.sDay=sDay;
    }

    public void setSelectedMonth(int sMonth)
    {
    this.sMonth=sMonth;
    }

    public void setSelectedYear(int sYear)
    {
    this.sYear=sYear;
    }

    public int getSelectedDate()
    {
    return sDate;
    }

    public int getSelectedDay()
    {
    return sDay;
    }

    public int getSelectedMonth()
    {
    return sMonth;
    }

    public int getSelectedYear()
    {
    return sYear;
    }



    }//Calendar
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM
  2. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 06:04 PM
  3. JTable prepareRenderer changes all cell colors
    By BrettStuart in forum AWT / Java Swing
    Replies: 9
    Last Post: July 21st, 2010, 03:51 AM
  4. how to copy cells to a new sheet of excel with JexcelAPI??
    By 19world in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 15th, 2010, 04:49 AM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 03:40 AM