Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable - Cell Editing and headerListings

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
Hi - I am writing an application and giving the user the ablility to enter 12 months of rainfall data. I am trying to have the data summed up on screen and stored for in Arraylists for later use. Would someone please help me with the JTable displaying the months (currently it uses A, B, C...) and let the user enter data. Thanks Ronnie


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BorderFactory.*;
import javax.swing.table.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JMenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GridBagWindow extends JFrame implements ActionListener, FocusListener {

public GridBagWindow() {
setTitle("User Rainfall Data");
JComboBox userCboRainCounty, userCboRainYr;
DefaultTableModel dtmUserRain = new DefaultTableModel();
JMenuBar userRainJMB = new JMenuBar();
JMenu userRainfileMenu = new JMenu("File");
JMenu userRainprintMenu = new JMenu("Print");
JMenu userRainclearMenu = new JMenu("Clear");
JMenu userRaintotalsMenu = new JMenu("Totals");

//Need to add additional menu option because evnet can not be added directly to JMenu
JMenuItem userRainOpen = new JMenuItem("Open");
JMenuItem userRainSave = new JMenuItem("Save");
JMenuItem userRainPrint = new JMenuItem("Print");
JMenuItem userRainClear = new JMenuItem("Clear");
JMenuItem userRainTotals = new JMenuItem("Totals");

userRainJMB.add(userRainfileMenu);
userRainfileMenu.add(userRainOpen);
userRainfileMenu.add(userRainSave);
userRainJMB.add(userRainprintMenu);
userRainprintMenu.add(userRainPrint);

userRainJMB.add(userRainclearMenu);
userRainclearMenu.add(userRainClear);
userRainJMB.add(userRaintotalsMenu);
userRaintotalsMenu.add(userRainTotals);

userRainOpen.addActionListener(this);
userRainSave.addActionListener(this);
userRainPrint.addActionListener(this);
userRainClear.addActionListener(this);
userRainTotals.addActionListener(this);


setJMenuBar(userRainJMB);

Container contentPane = getContentPane();
setFont(new Font("Arial",Font.BOLD,16));
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

int width = 800;
int height =500;

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x, y, 600, 600);

setJMenuBar(userRainJMB);

JLabel userRainLblCounty = new JLabel(" County");
userRainLblCounty.setFont(new Font("Arial",Font.BOLD,16));
buidConstraints(c, 2, 0, 1, 1, 0, 0);
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(1, 25, 1, 1);
gridbag.setConstraints(userRainLblCounty, c);
contentPane.add(userRainLblCounty);

userCboRainCounty = new JComboBox();
userCboRainCounty.setFont(new Font("Arial",Font.BOLD,16));
userCboRainCounty.setBackground(Color.white);
buidConstraints(c, 3, 0, 1, 1, 0, 0);
c.insets = new Insets(1, 1,1, 1);
c.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(userCboRainCounty, c);
userCboRainCounty.addFocusListener(this);
contentPane.add(userCboRainCounty);


TableModel tm = new AbstractTableModel(){
String[][] data = { {"","","","9","","","","","","","","",""} };//, {"","","","9","","","","","","","","",""}};
String[] header = {"Jan","Feb","Mar","Apr","May","June",
"July","Aug","Sep","Oct","Nov","Dec","Total "};


public int getColumnCount(){
return header.length;
}

public int getRowCount(){
return data.length;
}
public Object getValueAt(int row, int column){
return data [row] [column];
}
};


JTable userRainTable = new JTable(tm);
JScrollPane scrollPane = new JScrollPane(userRainTable);


buidConstraints(c, 1, 1, 3, 1, 0, 0);
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(10, 10,1,1);
gridbag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);

JLabel userRainLblRain = new JLabel("Rain (in)");
userRainLblCounty.setFont(new Font("Arial",Font.BOLD,16));
buidConstraints(c, 0, 1, 1, 1, 0, 0);
c.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(userRainLblRain, c);
contentPane.add(userRainLblRain);
userRainTable.setPreferredScrollableViewportSize(new Dimension (600, 20));

JLabel userRainLblRainYr = new JLabel("Rainfall Years");
userRainLblRainYr.setFont(new Font("Arial",Font.BOLD,16));
buidConstraints(c, 0, 2, 1, 1, 0, 0);
c.anchor = GridBagConstraints.SOUTH;
c.insets = new Insets(0, 1,1,1);
gridbag.setConstraints(userRainLblRainYr, c);
contentPane.add(userRainLblRainYr);

userCboRainYr = new JComboBox();
userCboRainYr.setFont(new Font("Arial",Font.BOLD,16));
userCboRainYr.setBackground(Color.white);
buidConstraints(c, 1, 2, 1, 1, 0, 0);
c.insets = new Insets(1, 1,1, 1);
c.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(userCboRainYr, c);
// userCboRainCounty.addFocusListener(this);
contentPane.add(userCboRainYr);

JLabel userRainLblTitle = new JLabel("Title");
userRainLblTitle.setFont(new Font("Arial",Font.BOLD,16));
buidConstraints(c, 2, 2, 1, 1, 0, 0);
c.anchor = GridBagConstraints.EAST;
// c.insets = new Insets(1, 14,1,1);
gridbag.setConstraints(userRainLblTitle, c);
contentPane.add(userRainLblTitle);

JTextField userRainTxtTitle = new JTextField(18);
userRainTxtTitle.setFont(new Font("Arial",Font.BOLD,16));
buidConstraints(c, 3, 2, 1, 1, 0, 0);
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(1, 1, 1, 1);
gridbag.setConstraints(userRainTxtTitle, c);
contentPane.add(userRainTxtTitle);

JLabel userLblRainBlank = new JLabel(" ");
buidConstraints(c, 0, 3, 4, 4, 0, 0);
c.insets = new Insets(15, 15,1,1);
gridbag.setConstraints(userLblRainBlank, c);
contentPane.add(userLblRainBlank);

JButton userBtnRainDefault = new JButton(" Default ");
userBtnRainDefault.setFont(new Font("Arial",Font.BOLD,16));
userBtnRainDefault.setBorder(BorderFactory.createRaisedBevelBorder());
buidConstraints(c, 1, 4, 1, 1, 0, 0);
c.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(userBtnRainDefault, c);
contentPane.add(userBtnRainDefault);

JButton userBtnRainCancel = new JButton(" Cancel ");
userBtnRainCancel.setFont(new Font("Arial",Font.BOLD,16));
userBtnRainCancel.setBorder(BorderFactory.createRaisedBevelBorder());
buidConstraints(c, 2, 4, 1, 1, 0, 0);
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(15,0,0,0);
gridbag.setConstraints(userBtnRainCancel, c);
contentPane.add(userBtnRainCancel);

JButton userBtnRainOK = new JButton(" O.K ");
userBtnRainOK.setFont(new Font("Arial",Font.BOLD,16));
userBtnRainOK.setBorder(BorderFactory.createRaisedBevelBorder());
buidConstraints(c, 3, 4, 1, 1, 20, 20);
c.insets = new Insets(15,0,0,150);
c.anchor = GridBagConstraints.EAST;
gridbag.setConstraints(userBtnRainOK, c);
userBtnRainOK.addActionListener(this);
contentPane.add(userBtnRainOK);

// //User Definded Cold Protection
// Input Combo Box for County Selection, two counties per line

userCboRainCounty.addItem( "CHARLOTTE" );
userCboRainCounty.addItem( "CITRUS" );
userCboRainCounty.addItem( "DESOTO" );
userCboRainCounty.addItem( "HARDEE" );
userCboRainCounty.addItem( "HERNANDO" );
userCboRainCounty.addItem( "HIGHLANDS" );
userCboRainCounty.addItem( "HILLSBOROUGH " );
userCboRainCounty.addItem( "LAKE" );
userCboRainCounty.addItem( "LEVY" );
userCboRainCounty.addItem( "MANATEE" );
userCboRainCounty.addItem( "MARION" );
userCboRainCounty.addItem( "PASCO" );
userCboRainCounty.addItem( "PINELLAS" );
userCboRainCounty.addItem( "POLK" );
userCboRainCounty.addItem( "SARASOTA" );
userCboRainCounty.addItem( "SUMTER" );
userCboRainCounty.setSelectedIndex( 5 );

userCboRainYr.addItem( String.valueOf( 1 ) );
userCboRainYr.addItem( String.valueOf( 5 ) );
userCboRainYr.addItem( String.valueOf( 10 ) );
userCboRainYr.addItem( String.valueOf( 15 ) );
userCboRainYr.addItem( String.valueOf( 20 ) );
userCboRainYr.addItem( String.valueOf( 25 ) );
userCboRainYr.addItem( String.valueOf( 30 ) );
userCboRainYr.addItem( String.valueOf( 35 ) );
userCboRainYr.addItem( String.valueOf( 40 ) );
userCboRainYr.addItem( String.valueOf( 45 ) );
userCboRainYr.addItem( String.valueOf( 50 ) );
userCboRainYr.addItem( String.valueOf( 55 ) );
userCboRainYr.addItem( String.valueOf( 60 ) );
userCboRainYr.addItem( String.valueOf( 65 ) );
userCboRainYr.addItem( String.valueOf( 70 ) );
userCboRainYr.addItem( String.valueOf( 75 ) );
userCboRainYr.addItem( String.valueOf( 80 ) );
userCboRainYr.addItem( String.valueOf( 85 ) );
userCboRainYr.setSelectedIndex( -1 );

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

pack();
setVisible(true);
}

void buidConstraints (GridBagConstraints c, int gx, int gy, int gw, int gh, int wx, int wy){
c.gridx = gx;
c.gridy = gy;
c.gridwidth = gw;
c.gridheight = gh;
c.weightx = wx;
c.weighty = wy;
}

// public void setValueAt(Object value, int row, int column){
// if (value instanceof String){
// data[row][column] = "0";
// }
// }

public void focusGained(FocusEvent af) {
// if(af.getSource() == userRainTable){
// userRainTable.setBackground(Color.yellow);
// }
}

public void focusLost(FocusEvent af) {

}

public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
}

public void actionPerformed(ActionEvent evt){
String actionName = evt.getActionCommand();
System.out.println("Button \"" + actionName + "\" was pressed.");
// Object o = userRainTable.getValueAt(0,4);
// System.out.println(o);

if(actionName == "Clear"){
for(int i=0; i>14; i++){

// userRainfallTable.setValueAt(0, 1 , 1);


}
System.out.println("Yes Continue");
}
if(actionName == "Totals"){
System.out.println("Yes Continue");
}


}





}
 
add a method getColumnName (int idx):
Code:
TableModel tm = new AbstractTableModel()
{
	String[][] data = {{"2","","4","9"} };
	String[] header = {"Jan","Feb","Mar","Foobar "};

	public String getColumnName(int col)
	{
		return header[col].toString();
	}
and please reduce your code to the minimum neccessary to reproduce the bug or missing feature next time (while having a compileable and runnable snipplet is often a good idea).

seeking a job as java-programmer in Berlin:
 
I have helped you to change the code. Now you can see table header and enter data by double clicking on the table cell.

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.BorderFactory.*;
import javax.swing.table.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JMenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GridBagWindow extends JFrame implements ActionListener,
		FocusListener {

	public GridBagWindow() {
		setTitle("User Rainfall Data");
		JComboBox userCboRainCounty, userCboRainYr;
		DefaultTableModel dtmUserRain = new DefaultTableModel();
		JMenuBar userRainJMB = new JMenuBar();
		JMenu userRainfileMenu = new JMenu("File");
		JMenu userRainprintMenu = new JMenu("Print");
		JMenu userRainclearMenu = new JMenu("Clear");
		JMenu userRaintotalsMenu = new JMenu("Totals");

		// Need to add additional menu option because evnet can not be added
		// directly to JMenu
		JMenuItem userRainOpen = new JMenuItem("Open");
		JMenuItem userRainSave = new JMenuItem("Save");
		JMenuItem userRainPrint = new JMenuItem("Print");
		JMenuItem userRainClear = new JMenuItem("Clear");
		JMenuItem userRainTotals = new JMenuItem("Totals");

		userRainJMB.add(userRainfileMenu);
		userRainfileMenu.add(userRainOpen);
		userRainfileMenu.add(userRainSave);
		userRainJMB.add(userRainprintMenu);
		userRainprintMenu.add(userRainPrint);

		userRainJMB.add(userRainclearMenu);
		userRainclearMenu.add(userRainClear);
		userRainJMB.add(userRaintotalsMenu);
		userRaintotalsMenu.add(userRainTotals);

		userRainOpen.addActionListener(this);
		userRainSave.addActionListener(this);
		userRainPrint.addActionListener(this);
		userRainClear.addActionListener(this);
		userRainTotals.addActionListener(this);

		setJMenuBar(userRainJMB);

		Container contentPane = getContentPane();
		setFont(new Font("Arial", Font.BOLD, 16));
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		contentPane.setLayout(gridbag);

		int width = 800;
		int height = 500;

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width - width) / 2;
		int y = (screen.height - height) / 2;
		setBounds(x, y, 600, 600);

		setJMenuBar(userRainJMB);

		JLabel userRainLblCounty = new JLabel(
				"                                                        County");
		userRainLblCounty.setFont(new Font("Arial", Font.BOLD, 16));
		buidConstraints(c, 2, 0, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		c.insets = new Insets(1, 25, 1, 1);
		gridbag.setConstraints(userRainLblCounty, c);
		contentPane.add(userRainLblCounty);

		userCboRainCounty = new JComboBox();
		userCboRainCounty.setFont(new Font("Arial", Font.BOLD, 16));
		userCboRainCounty.setBackground(Color.white);
		buidConstraints(c, 3, 0, 1, 1, 0, 0);
		c.insets = new Insets(1, 1, 1, 1);
		c.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(userCboRainCounty, c);
		userCboRainCounty.addFocusListener(this);
		contentPane.add(userCboRainCounty);
		String[] header = { "Jan", "Feb", "Mar", "Apr", "May", "June",
				"July", "Aug", "Sep", "Oct", "Nov", "Dec", "Total" };
		DefaultTableModel dftModel = new DefaultTableModel(header, 1);
		
		JTable userRainTable = new JTable(dftModel);
		userRainTable.setEnabled(true);
		userRainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane scrollPane = new JScrollPane(userRainTable);
		buidConstraints(c, 1, 1, 3, 1, 0, 0);
		c.anchor = GridBagConstraints.WEST;
		c.insets = new Insets(10, 10, 1, 1);
		gridbag.setConstraints(scrollPane, c);
		contentPane.add(scrollPane);

		JLabel userRainLblRain = new JLabel("Rain (in)");
		userRainLblCounty.setFont(new Font("Arial", Font.BOLD, 16));
		buidConstraints(c, 0, 1, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(userRainLblRain, c);
		contentPane.add(userRainLblRain);
		userRainTable
				.setPreferredScrollableViewportSize(new Dimension(600, 20));

		JLabel userRainLblRainYr = new JLabel("Rainfall Years");
		userRainLblRainYr.setFont(new Font("Arial", Font.BOLD, 16));
		buidConstraints(c, 0, 2, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.SOUTH;
		c.insets = new Insets(0, 1, 1, 1);
		gridbag.setConstraints(userRainLblRainYr, c);
		contentPane.add(userRainLblRainYr);

		userCboRainYr = new JComboBox();
		userCboRainYr.setFont(new Font("Arial", Font.BOLD, 16));
		userCboRainYr.setBackground(Color.white);
		buidConstraints(c, 1, 2, 1, 1, 0, 0);
		c.insets = new Insets(1, 1, 1, 1);
		c.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(userCboRainYr, c);
		// userCboRainCounty.addFocusListener(this);
		contentPane.add(userCboRainYr);

		JLabel userRainLblTitle = new JLabel("Title");
		userRainLblTitle.setFont(new Font("Arial", Font.BOLD, 16));
		buidConstraints(c, 2, 2, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		// c.insets = new Insets(1, 14,1,1);
		gridbag.setConstraints(userRainLblTitle, c);
		contentPane.add(userRainLblTitle);

		JTextField userRainTxtTitle = new JTextField(18);
		userRainTxtTitle.setFont(new Font("Arial", Font.BOLD, 16));
		buidConstraints(c, 3, 2, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		c.insets = new Insets(1, 1, 1, 1);
		gridbag.setConstraints(userRainTxtTitle, c);
		contentPane.add(userRainTxtTitle);

		JLabel userLblRainBlank = new JLabel("     ");
		buidConstraints(c, 0, 3, 4, 4, 0, 0);
		c.insets = new Insets(15, 15, 1, 1);
		gridbag.setConstraints(userLblRainBlank, c);
		contentPane.add(userLblRainBlank);

		JButton userBtnRainDefault = new JButton("     Default     ");
		userBtnRainDefault.setFont(new Font("Arial", Font.BOLD, 16));
		userBtnRainDefault.setBorder(BorderFactory.createRaisedBevelBorder());
		buidConstraints(c, 1, 4, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(userBtnRainDefault, c);
		contentPane.add(userBtnRainDefault);

		JButton userBtnRainCancel = new JButton("     Cancel     ");
		userBtnRainCancel.setFont(new Font("Arial", Font.BOLD, 16));
		userBtnRainCancel.setBorder(BorderFactory.createRaisedBevelBorder());
		buidConstraints(c, 2, 4, 1, 1, 0, 0);
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(15, 0, 0, 0);
		gridbag.setConstraints(userBtnRainCancel, c);
		contentPane.add(userBtnRainCancel);

		JButton userBtnRainOK = new JButton("       O.K       ");
		userBtnRainOK.setFont(new Font("Arial", Font.BOLD, 16));
		userBtnRainOK.setBorder(BorderFactory.createRaisedBevelBorder());
		buidConstraints(c, 3, 4, 1, 1, 20, 20);
		c.insets = new Insets(15, 0, 0, 150);
		c.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(userBtnRainOK, c);
		userBtnRainOK.addActionListener(this);
		contentPane.add(userBtnRainOK);

		// //User Definded Cold Protection
		// Input Combo Box for County Selection, two counties per line

		userCboRainCounty.addItem("CHARLOTTE");
		userCboRainCounty.addItem("CITRUS");
		userCboRainCounty.addItem("DESOTO");
		userCboRainCounty.addItem("HARDEE");
		userCboRainCounty.addItem("HERNANDO");
		userCboRainCounty.addItem("HIGHLANDS");
		userCboRainCounty.addItem("HILLSBOROUGH   ");
		userCboRainCounty.addItem("LAKE");
		userCboRainCounty.addItem("LEVY");
		userCboRainCounty.addItem("MANATEE");
		userCboRainCounty.addItem("MARION");
		userCboRainCounty.addItem("PASCO");
		userCboRainCounty.addItem("PINELLAS");
		userCboRainCounty.addItem("POLK");
		userCboRainCounty.addItem("SARASOTA");
		userCboRainCounty.addItem("SUMTER");
		userCboRainCounty.setSelectedIndex(5);

		userCboRainYr.addItem(String.valueOf(1));
		userCboRainYr.addItem(String.valueOf(5));
		userCboRainYr.addItem(String.valueOf(10));
		userCboRainYr.addItem(String.valueOf(15));
		userCboRainYr.addItem(String.valueOf(20));
		userCboRainYr.addItem(String.valueOf(25));
		userCboRainYr.addItem(String.valueOf(30));
		userCboRainYr.addItem(String.valueOf(35));
		userCboRainYr.addItem(String.valueOf(40));
		userCboRainYr.addItem(String.valueOf(45));
		userCboRainYr.addItem(String.valueOf(50));
		userCboRainYr.addItem(String.valueOf(55));
		userCboRainYr.addItem(String.valueOf(60));
		userCboRainYr.addItem(String.valueOf(65));
		userCboRainYr.addItem(String.valueOf(70));
		userCboRainYr.addItem(String.valueOf(75));
		userCboRainYr.addItem(String.valueOf(80));
		userCboRainYr.addItem(String.valueOf(85));
		userCboRainYr.setSelectedIndex(-1);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		pack();
		setVisible(true);
	}

	void buidConstraints(GridBagConstraints c, int gx, int gy, int gw, int gh,
			int wx, int wy) {
		c.gridx = gx;
		c.gridy = gy;
		c.gridwidth = gw;
		c.gridheight = gh;
		c.weightx = wx;
		c.weighty = wy;
	}

	// public void setValueAt(Object value, int row, int column){
	// if (value instanceof String){
	// data[row][column] = "0";
	// }
	// }

	public void focusGained(FocusEvent af) {
		// if(af.getSource() == userRainTable){
		// userRainTable.setBackground(Color.yellow);
		// }
	}

	public void focusLost(FocusEvent af) {

	}

	public static void main(String args[]) {
		GridBagWindow window = new GridBagWindow();
	}

	public void actionPerformed(ActionEvent evt) {
		String actionName = evt.getActionCommand();
		System.out.println("Button \"" + actionName + "\" was pressed.");
		// Object o = userRainTable.getValueAt(0,4);
		// System.out.println(o);

		if (actionName == "Clear") {
			for (int i = 0; i > 14; i++) {

				// userRainfallTable.setValueAt(0, 1 , 1);

			}
			System.out.println("Yes Continue");
		}
		if (actionName == "Totals") {
			System.out.println("Yes Continue");
		}

	}

}

Chinese Java Faq Forum
 
Thanks for your help. It worked except I am having problems with getValueAt. Any help/suggestions would be appreciated. Ronnie
 
having problems with getValueAt

What kind of problems?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi Tim - I am haveing problems using the getVaule in order to utilize the JTable inputs. Thanks Ronnie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top