We help IT Professionals succeed at work.
Get Started

JTable save on exit. And load on start-up

jozio
jozio asked
on
3,247 Views
Last Modified: 2013-11-23
I've looked at many many examples of saving JTable but i'm not getting closer to finishing my project.

I am using DefaultTableModel and JTable. When i open the program everything works and i can add a row, remove a row and such, but I need to save the values inside the JTable at close and open them again when i open the program again.

I've not specified any ArrayList or Vector but it is my understanding that DefaultTableModel saves the inserted rows in Vector.  I will attach all my code so you can maybe find out what is wrong.... I tried inserting fireTableDataChanged, and setValueAt, but maybe i waas doing it wrong

Thank You
kv Jón Orri Sigurðarson


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;

public class jozioforrit extends JPanel {
	
	class AboutDialog extends JDialog {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public AboutDialog(JFrame parent) {
			setMaximumSize(new Dimension(250,150));
			Box b = Box.createVerticalBox();
			try {
				Object columnrow = table.getModel().getValueAt(table.getSelectedRow(),0);
				String banan = columnrow.toString();
				String banani = banan.replace(' ', '+');
				URL url = new URL("http://en.wikiwix.com/export/export.php?backend=html&url=http://en.wikipedia.org/wiki/" + banani);
				String banana = url.toString();
				System.out.println(banana);
				JEditorPane pppane = new JEditorPane();
				pppane.setPage(url);
				JScrollPane skroll = new JScrollPane(pppane);
				b.add(skroll);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			getContentPane().add(b, "Center");
			setSize(750, 500);
			setLocation(400,70);
		}
	}

	public class ButtonColumn extends AbstractCellEditor implements
			TableCellRenderer, TableCellEditor, ActionListener, MouseListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private Action action;
		private JButton editButton;
		private Border focusBorder;
		private boolean isButtonColumnEditor;
		private int mnemonic;

		private Border originalBorder;
		private JButton renderButton;
		private JTable table;
		private String text;

		/**
		 * Create the ButtonColumn to be used as a renderer and editor. The
		 * renderer and editor will automatically be installed on the
		 * TableColumn of the specified column.
		 * 
		 * @param table
		 *            the table containing the button renderer/editor
		 * @param action
		 *            the Action to be invoked when the button is invoked
		 * @param column
		 *            the column to which the button renderer/editor is added
		 */
		public ButtonColumn(JTable table, Action action, int column) {
			this.table = table;
			this.action = action;

			renderButton = new JButton();
			editButton = new JButton();
			editButton.setFocusPainted(false);
			editButton.addActionListener(this);
			originalBorder = editButton.getBorder();
			setFocusBorder(new LineBorder(Color.BLUE));

			TableColumnModel columnModel = table.getColumnModel();
			columnModel.getColumn(column).setCellRenderer(this);
			columnModel.getColumn(column).setCellEditor(this);
			table.addMouseListener(this);
		}

		//
		// Implement ActionListener interface
		//
		/*
		 * The button has been pressed. Stop editing and invoke the custom
		 * Action
		 */
		public void actionPerformed(ActionEvent e) {
			int row = table.convertRowIndexToModel(table.getEditingRow());
			fireEditingStopped();

			// Invoke the Action

			ActionEvent event = new ActionEvent(table,
					ActionEvent.ACTION_PERFORMED, "" + row);
			action.actionPerformed(event);
		}

		@Override
		public Object getCellEditorValue() {
			return text;
		}

		/**
		 * Get foreground color of the button when the cell has focus
		 * 
		 * @return the foreground color
		 */
		public Border getFocusBorder() {
			return focusBorder;
		}

		public int getMnemonic() {
			return mnemonic;
		}

		@Override
		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			text = (value == null) ? "Wiki" : value.toString();
			editButton.setText(text);
			return editButton;
		}

		//
		// Implement TableCellRenderer interface
		//
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (isSelected) {
				renderButton.setForeground(table.getSelectionForeground());
				renderButton.setBackground(table.getSelectionBackground());
			} else {
				renderButton.setForeground(table.getForeground());
				renderButton.setBackground(UIManager
						.getColor("Button.background"));
			}

			if (hasFocus) {
				renderButton.setBorder(focusBorder);
			} else {
				renderButton.setBorder(originalBorder);
			}

			renderButton.setText((value == null) ? "Wiki" : value.toString());
			return renderButton;
		}

		public void mouseClicked(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		//
		// Implement MouseListener interface
		//
		/*
		 * When the mouse is pressed the editor is invoked. If you then then
		 * drag the mouse to another cell before releasing it, the editor is
		 * still active. Make sure editing is stopped when the mouse is
		 * released.
		 */
		public void mousePressed(MouseEvent e) {
			if (table.isEditing() && table.getCellEditor() == this)
				isButtonColumnEditor = true;
		}

		public void mouseReleased(MouseEvent e) {
			if (isButtonColumnEditor && table.isEditing())
				table.getCellEditor().stopCellEditing();

			isButtonColumnEditor = false;
		}

		/**
		 * The foreground color of the button when the cell has focus
		 * 
		 * @param focusBorder
		 *            the foreground color
		 */
		public void setFocusBorder(Border focusBorder) {
			this.focusBorder = focusBorder;
			editButton.setBorder(focusBorder);
		}

		/**
		 * The mnemonic to activate the button when the cell has focus
		 * 
		 * @param mnemonic
		 *            the mnemonic
		 */
		public void setMnemonic(int mnemonic) {
			this.mnemonic = mnemonic;
			renderButton.setMnemonic(mnemonic);
			editButton.setMnemonic(mnemonic);
		}
	}

	private static void createAndShowGUI() {
		JMenuBar menuBar;
		JMenu menu;
		JMenuItem menuItem;
		menuBar = new JMenuBar();
		menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		menu.getAccessibleContext().setAccessibleDescription(
				"The only menu in this program that has menu items");
		menuBar.add(menu);
		menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));
		menuItem.getAccessibleContext().setAccessibleDescription(
				"This doesn't really do anything");
		menu.add(menuItem);
		menuBar.add(menu);
		// Create and set up the window.
		
		JFrame frame = new JFrame("Tölvuleikjaskipuleggjari jos16");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		jozioforrit newContentPane = new jozioforrit();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.setJMenuBar(menuBar);
		frame.setLocation(20,20);
		frame.setMinimumSize(new Dimension(480,150));
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		createAndShowGUI();
	}

	private JTextField filterText;

	private TableRowSorter<TableModel> sorter;

	private JTable table;

	public jozioforrit() {
		Object rows[][] = {
				{ "Leikurinn", "Veldu tegund úr listanum", "Einkunn", "Bannað?" },
				{ "Prince of Persia", "", "", "" } };
		String columns[] = { "Leikur", "Tegund Leiks", "Einkunn",
				"Aldurshópur", "Um Leikinn"};

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// Create a table with a sorter.
		final DefaultTableModel model = new DefaultTableModel(rows, columns) {
			@Override
			public Class getColumnClass(int column) {
				Class returnValue;
				if ((column >= 0) && (column < getColumnCount())) {
					returnValue = getValueAt(0, column).getClass();
				} else {
					returnValue = Object.class;
				}
				return returnValue;
			}
		};
		sorter = new TableRowSorter<TableModel>(model);
		table = new JTable(model);

		table.setRowHeight(20);
		table.setFont(new Font("Calibri", Font.PLAIN, 13));
		
		table.getColumnModel().getColumn(0).setMinWidth(105);
		table.getColumnModel().getColumn(1).setMinWidth(165);
		table.getColumnModel().getColumn(2).setMinWidth(60);
		table.getColumnModel().getColumn(3).setMinWidth(70);
		table.getColumnModel().getColumn(4).setMinWidth(80);
		
		TableColumn tableColumn = table.getColumnModel().getColumn(1);
		JComboBox comboBox = new JComboBox();
		comboBox.addItem("Hlutverkaleikur");
		comboBox.addItem("Íþróttir");
		comboBox.addItem("Borðspil");
		comboBox.addItem("Fyrstu persónu skotleikur");
		comboBox.addItem("Barnaleikur");
		comboBox.addItem("Ævintýri");
		comboBox.addItem("Herkænskuleikur");
		comboBox.addItem("Púsl og/eða smáleikur");
		comboBox.setEditable(true);
		tableColumn.setCellEditor(new DefaultCellEditor(comboBox));

		TableColumn tableColumn2 = table.getColumnModel().getColumn(2);
		JComboBox comboBox2 = new JComboBox();
		comboBox2.addItem("10");
		comboBox2.addItem("9");
		comboBox2.addItem("8");
		comboBox2.addItem("7");
		comboBox2.addItem("6");
		comboBox2.addItem("5");
		comboBox2.addItem("4");
		comboBox2.addItem("3");
		comboBox2.addItem("2");
		comboBox2.addItem("1");
		comboBox2.setEditable(false);
		tableColumn2.setCellEditor(new DefaultCellEditor(comboBox2));

		TableColumn tableColumn3 = table.getColumnModel().getColumn(3);
		JComboBox comboBox3 = new JComboBox();
		comboBox3.addItem("Leyfilegt öllum");
		comboBox3.addItem("6+");
		comboBox3.addItem("12+");
		comboBox3.addItem("16+");
		comboBox3.addItem("18+");
		comboBox3.setEditable(true);
		tableColumn3.setCellEditor(new DefaultCellEditor(comboBox3));

		JButton button = new JButton("Skrá leik");
		button.setPreferredSize(new Dimension(85, 20));
		button.setBorder(BorderFactory.createEtchedBorder());
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object[] kuki = new Object[] {"Nýr Leikur", "Tegund"};
				model.insertRow(1,kuki);
			}
		});

		add(button, c);

		table.setRowSorter(sorter);
		table.setPreferredScrollableViewportSize(new Dimension(500, 210));
		table.setFillsViewportHeight(true);
		// single selection
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		Action meira = new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				e.getSource();
				Integer.valueOf(e.getActionCommand());

				JDialog f = new AboutDialog(new JFrame());
				f.show();
			}
		};

		ButtonColumn buttonColumn = new ButtonColumn(table, meira, 4);
		buttonColumn.setMnemonic(KeyEvent.VK_W);
		
		JScrollPane scrollPane = new JScrollPane(table);
		c.anchor = GridBagConstraints.NORTH;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 1;
		add(scrollPane, c);

		JLabel l1 = new JLabel("Leita");
		c.anchor = GridBagConstraints.NORTH;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weighty = 0.0;
		c.gridheight = 1;
		c.gridx = 1;
		c.gridy = 0;
		c.insets = new Insets(0, 100, 0, 0);
		add(l1, c);
		filterText = new JTextField();
		// Whenever filterText changes, invoke newFilter.
		filterText.getDocument().addDocumentListener(new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
				if (filterText.getText().length() == 0) {
					sorter.setRowFilter(null);
				} else {
					sorter.setRowFilter(RowFilter.regexFilter(filterText
							.getText()));
				}
			}

			public void insertUpdate(DocumentEvent e) {
				if (filterText.getText().length() == 0) {
					sorter.setRowFilter(null);
				} else {
					sorter.setRowFilter(RowFilter.regexFilter(filterText
							.getText()));
				}
			}

			public void removeUpdate(DocumentEvent e) {
				if (filterText.getText().length() == 0) {
					sorter.setRowFilter(null);
				} else {
					sorter.setRowFilter(RowFilter.regexFilter(filterText
							.getText()));
				}
			}
		});
		l1.setLabelFor(filterText);
		c.anchor = GridBagConstraints.NORTH;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weighty = 0.0;
		c.gridheight = 1;
		c.gridx = 2;
		c.gridy = 0;
		c.insets = new Insets(0, 140, 0, 0);
		add(filterText, c);

	}

}

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE