public class WeatherFrame extends JFrame {
private JPanel contentPane;
private JTable table;
HealthData health = new HealthData();
private DefaultTableModel model;
String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
"numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WeatherFrame frame = new WeatherFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public WeatherFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 300);
contentPane = new JPanel();
contentPane.setBounds(100, 100,750, 200);
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 25, 788, 180);
contentPane.add(scrollPane);
populateTable();
table = new JTable(model);
scrollPane.setViewportView(table);
JButton btnInsert = new JButton("insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
InsertFrame frame = new InsertFrame();
frame.setVisible(true);
}
});
btnInsert.setBounds(279, 217, 117, 29);
contentPane.add(btnInsert);
JButton btnDelete = new JButton("delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteFrame delete = new DeleteFrame();
delete.setVisible(true);
}
});
btnDelete.setBounds(412, 217, 117, 29);
contentPane.add(btnDelete);
JButton btnSearch = new JButton("search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SelectFrame search = new SelectFrame();
search.setVisible(true);
}
});
btnSearch.setBounds(530, 217, 117, 29);
contentPane.add(btnSearch);
JLabel lblWeatherTable = new JLabel("Weather Table");
lblWeatherTable.setBounds(149, 6, 107, 16);
contentPane.add(lblWeatherTable);
JButton btnNext = new JButton("update");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpdateFrame update = new UpdateFrame();
update.setVisible(true);
}
});
btnNext.setBounds(150, 217, 117, 29);
contentPane.add(btnNext);
JButton btnRefresh = new JButton("refresh");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
populateTable();
}
});
btnRefresh.setBounds(29, 217, 117, 29);
contentPane.add(btnRefresh);
JButton btnAnalyze = new JButton("Analyze");
btnAnalyze.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ShowAnalyze analyze = new ShowAnalyze(health.analyze());
analyze.setVisible(true);
}
});
btnAnalyze.setBounds(662, 217, 117, 29);
contentPane.add(btnAnalyze);
}
#SuppressWarnings("serial")
public void populateTable() {
model = new DefaultTableModel(){
#Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
for(String name: columnNames)
model.addColumn(name);
ArrayList<Health> temp = new ArrayList<Health>();
temp = health.showAllData();
for(int i = 0; i< temp.size(); i++) {
Object[] data = {temp.get(i).getZipCode(), temp.get(i).getCounty(), temp.get(i).getCounty(), temp.get(i).getState(),temp.get(i).getYear(),
temp.get(i).getMonth(), temp.get(i).getAgeGroup(), temp.get(i).getNumOfVisits(), temp.get(i).getMMax(), temp.get(i).getMMin(), temp.get(i).getMNor()};
model.addRow(data);
}
table.setModel(model);
}
}
I'm trying to refresh the jtable using a refresh button, when I click the button it seems like it is loading, but after that nothing changes on the table. How can I fix this problem? In the action performed method of the refresh button, I called populateTable which is a function to load data into table.
In the populateTable method, you change the table model but you do not pass that new model to the JTable.
Option 1: replace the table model
In the constructor, you call:
table = new JTable(model);
populateTable();
In the populateTable, I would expect something like:
table.setModel(model);
Option 2: update the table model
As mKorbel already suggested, you can also update your table model instead of throwing the existing model away and creating a new one. Your populateTable method could look like this (using Java 8 and with a new initializeModel method to create the table model initially):
public void populateTable() {
boolean firstTime = (model == null);
if (firstTime) {
initializeModel();
} else {
model.getDataVector().clear();
}
for (Health item : health.showAllData()) {
model.addRow(new Vector<>(Arrays.asList(
item.getZipCode(), item.getCounty(), item.getState(), item.getYear(),
item.getMonth(), item.getAgeGroup(), item.getNumOfVisits(),
item.getMMax(), item.getMMin(), item.getMNor()
)));
}
if (firstTime && table != null) {
table.setModel(model);
}
}
private void initializeModel() {
model = new DefaultTableModel() {
#Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
for (String name : columnNames)
model.addColumn(name);
}
JTable and its DefaultTableModel (desclared as private JTable table; and private DefaultTableModel model;) doesn't know something that a new model = new DefaultTableModel(){ is (re)created in public void populateTable() {,
you have to
add a new DefaultTableModel to JTables instance that is already visible in your Swing GUI
(better option is) add a new data directly to private DefaultTableModel model;, this model is designated for
rest is very well described in comment by # Andrew Thompson
Related
I want to assign dir array to comboBox. Is there any error in my code. I tried to display the dir array it contains the values but cannot assign it comboBox. Here is the code.
import java.awt.EventQueue;
public class ExpenseManager {
private JFrame frame;
private JTextField txtUserName;
private JLabel lblNewUserName;
private JButton btnDone;
private JButton btnLogin;
private JComboBox<?> comboBox;
private String[] dir = new String[100];
private String[] hello = {"Hii", "Hello"};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ExpenseManager window = new ExpenseManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ExpenseManager() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
//#SuppressWarnings("unchecked")
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewUser = new JButton("New User");
btnNewUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewUserName.setVisible(true);
txtUserName.setVisible(true);
btnDone.setVisible(true);
}
});
btnNewUser.setBounds(23, 34, 89, 23);
frame.getContentPane().add(btnNewUser);
txtUserName = new JTextField();
txtUserName.setBounds(240, 63, 134, 20);
frame.getContentPane().add(txtUserName);
txtUserName.setVisible(false);
txtUserName.setColumns(10);
lblNewUserName = new JLabel("Enter New UserName");
lblNewUserName.setBounds(240, 38, 134, 14);
lblNewUserName.setVisible(false);
frame.getContentPane().add(lblNewUserName);
btnDone = new JButton("Done");
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newUserName = txtUserName.getText();
if(!newUserName.isEmpty())
{
File f = new File("S:/Expense/" + newUserName);
if (f.exists() && f.isDirectory()) {
JOptionPane.showMessageDialog(null, "User already exists");
txtUserName.setText(null);
}
else{
boolean success = (new File("S:/Expense/" + newUserName)).mkdir();
if(!success){
JOptionPane.showMessageDialog(null, "Unable to register");
}else{
JOptionPane.showMessageDialog(null, "User registered Successfully");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "Kindly enter User Name", "Invalid User Name", JOptionPane.ERROR_MESSAGE);
}
}
});
btnDone.setBounds(240, 103, 89, 23);
btnDone.setVisible(false);
frame.getContentPane().add(btnDone);
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File[] directories = new File("S:/Expense/").listFiles();
for(int i = 0; i < directories.length; i++)
{
dir[i] = directories[i].getName();
}
comboBox.setVisible(true);
}
});
btnLogin.setBounds(23, 68, 89, 23);
frame.getContentPane().add(btnLogin);
comboBox = new JComboBox<Object>(dir);
comboBox.setBounds(24, 138, 72, 20);
comboBox.setSelectedIndex(1);
comboBox.setVisible(false);
frame.getContentPane().add(comboBox);
}
}
You can use setModel with DefaultComboBoxModel which take an array instead like :
comboBox.setModel(new DefaultComboBoxModel(dir));
Most of the code posted is not relevant to the question asked, so it should be removed (see MCVE).
Review the following, note the comments:
public class ExpenseManager {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
new ExpenseManager();
} catch (Exception e) { e.printStackTrace(); }
}
});
}
public ExpenseManager() { initialize(); }
private void initialize() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //better use a layout manger
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setBounds(24, 138, 72, 20);
comboBox.setVisible(false);
frame.getContentPane().add(comboBox);
JButton btn = new JButton("Populate combo");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//populate combo
String[] dir = new String[5];
for(int i = 0; i < dir.length; i++) { dir[i] = "String "+ i;}
comboBox.setModel(new DefaultComboBoxModel<>(dir));
comboBox.setVisible(true);
btn.setEnabled(false); //disable button
}
});
btn.setBounds(23, 68, 89, 23);
frame.getContentPane().add(btn);
frame.setVisible(true);
}
}
EDIT implementation using a layout manager :
private void initialize() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setVisible(false);
frame.add(comboBox, BorderLayout.SOUTH); //using BorderLayout which is the default
JButton btn = new JButton("Populate combo");
btn.setPreferredSize(new Dimension(150,35));
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//populate combo
String[] dir = new String[5];
for(int i = 0; i < dir.length; i++) { dir[i] = "String "+ i;}
comboBox.setModel(new DefaultComboBoxModel<>(dir));
comboBox.setVisible(true);
btn.setEnabled(false); //disable button
frame.pack(); //resize fram to fit the preferred size and layouts
}
});
frame.getContentPane().add(btn, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
My problem is that I have a class that when the user types out the text displayed dispose() is called, which works the first time but if you don't close the program and open it again, dispose() is called, but doesn't do anything which breaks the program.
public class TypeMenu extends JDialog {
protected final JPanel contentPanel = new JPanel();
protected static JTextField inputTxtField;
protected static JTextField textField;
protected static JTextField introTxtField;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
* #param introTxtField2
* #param textField2
* #param inputTxtField2
*/
public TypeMenu(JTextField inputTxtField2, JTextField introTxtField2, JTextField textField2) {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getTextField());
contentPanel.add(getIntroTxtField());
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
protected JTextField getInputTxtField() {
if (inputTxtField == null) {
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
protected JTextField getIntroTxtField() {
if (introTxtField == null) {
introTxtField = new JTextField();
introTxtField.setHorizontalAlignment(SwingConstants.CENTER);
introTxtField.setFont(new Font("Tahoma", Font.BOLD, 15));
introTxtField.setText("Easy Mode");
introTxtField.setEditable(false);
introTxtField.setBounds(56, 11, 314, 29);
introTxtField.setColumns(10);
}
return introTxtField;
}
private JTextField getTextField() {
if (textField == null) {
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setFont(new Font("Monospaced", Font.BOLD, 20));
textField.setBounds(10, 51, 414, 40);
}
return textField;
}
}
This is one of the child classes
public class Easy extends TypeMenu {
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Easy() {
super(inputTxtField, introTxtField, textField);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getIntroTxtField());
getString();
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
textField.selectAll();
}
}
private void getString() {
String str = textField.getText();
if (str.equals("")) {
String generator = StringGenerator.medium();
String nStr = "" + generator;
textField.setText(nStr);
}
}
}
The code that calls this class
public class StartMenu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField introTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
StartMenu dialog = new StartMenu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates the dialog and creates the buttons that take the user to each variation of the game when pressed.
*/
public StartMenu() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
introTxt = new JTextField();
introTxt.setFont(new Font("Tahoma", Font.BOLD, 12));
introTxt.setHorizontalAlignment(SwingConstants.CENTER);
introTxt.setText("Start Menu\r\n");
introTxt.setEditable(false);
introTxt.setBounds(75, 11, 276, 20);
contentPanel.add(introTxt);
introTxt.setColumns(10);
}
{
JButton btnEasyButton = new JButton("Easy Mode");
btnEasyButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
new Easy().setVisible(true);
}
});
btnEasyButton.setBounds(141, 42, 140, 23);
contentPanel.add(btnEasyButton);
}
{
JButton btnMediumButton = new JButton("Medium Mode");
btnMediumButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Medium().setVisible(true);
}
});
btnMediumButton.setBounds(141, 81, 140, 23);
contentPanel.add(btnMediumButton);
}
{
JButton btnHardButton = new JButton("Hard Mode");
btnHardButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Hard().setVisible(true);
}
});
btnHardButton.setBounds(141, 120, 140, 23);
contentPanel.add(btnHardButton);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
}
your text field is static so he will have only one instance across the application.
so in the method getIntroTxtField() you have if statement that says :
if (introTxtField == null)
in the first time this condition is true but when you create the new instance this condition is false because the instance of the static field is all ready created in the first and you are adding the key listener inside the condition
so the action listener will be added only in the first creation.
if you need to keep the static because you need in other class you need to remove the == null
protected JTextField getInputTxtField() {
inputTxtField = null;
{
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
or remove the static from you field declaration static is used in
shared instace only when you are using only one instace across the
application like sessionFactory or any thing that need to be created
only one time.
you code is a lot to read but i think your problem is that you are calling the dispose() of the wrong object. may you are calling it always for the first object and you are creating a new one, and the dispose of this new dialog will never be call,Check you code may be you are creating many object and the dispose() is not called at all. make sure that you have full control of your objects instance to know if you are diposing the wanted dialog.
Hi I need help with filling ComboBox (ClassA) from ClassAddItem. After selecting "Add New Item" from the ComboBox, call the new window. When you click "Add new item" (ClassAddItem) I want to join a new item into the ComboBox - class ClassA.
In a ClassA is also button "Add New Item" it works, but how do I add a new item to the CB from another class???
Item "Add new item" in a ComboBox is deliberately not in the array String[] cbItem, because in my application possibilities Loading from a file and I do not want this item is visible in the file. Also, it would be advantageous if the item "Add new item" still at the end of the ComboBox.
Thank you for your response
sorry for my English :)
public class ClassA extends JFrame {
private JPanel contentPane;
public JComboBox comboBox;
private String[] cbItem = {"A", "B", "C"};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClassA frame = new ClassA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ClassA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 291, 152);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
comboBox = new JComboBox(cbItem);
comboBox.addItem("add new Item");
comboBox.setBounds(10, 11, 116, 23);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("add new Item")) {
ClassAddItem cAI = new ClassAddItem();
cAI.setVisible(true);
}
}
});
contentPane.add(comboBox);
JButton btnAddNewItem = new JButton("add new item");
btnAddNewItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox.addItem("this works");
}
});
btnAddNewItem.setBounds(136, 11, 116, 23);
contentPane.add(btnAddNewItem);
}
}
ClassAddItem:
public class ClassAddItem extends JDialog {
public ClassAddItem() {
setBounds(100, 100, 266, 155);
getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Add new item");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClassA cA = new ClassA();
cA.comboBox.addItem("this does not work");
}
});
btnNewButton.setBounds(62, 11, 120, 23);
getContentPane().add(btnNewButton);
}
}
you can make a constructor using the combobox...
private JComboBox comboBox;
//constructor
public ClassAddItem(JComboBox box) {
this.comboBox = box;
....
}
and use this comboBox within your actionListener
public void actionPerformed(ActionEvent e) {
this.comboBox.addItem("this does work");
}
for Java Kepler Eclipse and Jtable, I am trying to make it so as when a specific table cell is selected, that cell will work as an editorPane; or have the whole column work as editorPane. When I click a cell on column COMMENTS it enlargens the row but I cant get it to work as an editorPane. My project is actualy very different but I wrote this mini one with the table so you can copy, paste and run it to see exactly what the problem is when you click on a COMMENTS cell.
I tried to make the column an editorPane to begin with like I made the column DONE with checkBox, but it doesnt work or I am doing it wrong. I also tried cellRenderer but I couldnt make that work either.
Whether the whole column works as an editorPane or just the selected cell it doesnt matter, whatever is easier and as long as it works
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class JavaTestOne {
JFrame frmApp;
private JTable table;
private JCheckBox checkbox;
DefaultTableModel model;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaTestOne window = new JavaTestOne();
window.frmApp.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JavaTestOne() {
initialize();
}
private void initialize() {
frmApp = new JFrame();
frmApp.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
frmApp.setBounds(50, 10, 1050, 650);
frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmApp.getContentPane().setLayout(new CardLayout(0, 0));
frmApp.setTitle("App");
{
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 42, 984, 484);
frmApp.add(scrollPane);
{
table = new JTable();
table.setFillsViewportHeight(true);
Object[][] data = {
{"I01", "Tom",new Boolean(false), ""},
{"I02", "Jerry",new Boolean(false), ""},
{"I03", "Ann",new Boolean(false), ""}};
String[] cols = {"ID","NAME","DONE","COMMENTS"};
model = new DefaultTableModel(data, cols) {
private static final long serialVersionUID = -7158928637468625935L;
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setModel(model);
table.setRowHeight(20);
table.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = table.rowAtPoint(evt.getPoint());
int col = table.columnAtPoint(evt.getPoint());
table.setRowHeight(20);
if(col==3){
table.setRowHeight(row, 100);
//this is where I need it to work as an editorPane if it is only for the selected cell
}
}
});
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
scrollPane.setViewportView(table);
checkbox = new JCheckBox("OK");
checkbox.setHorizontalAlignment(SwingConstants.CENTER);
checkbox.setBounds(360, 63, 97, 23);
}
}
}
}
Another alternative is to display a popup window to edit the cell:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/*
* The editor button that brings up the dialog.
*/
//public class TablePopupEditor extends AbstractCellEditor
public class TablePopupEditor extends DefaultCellEditor
implements TableCellEditor
{
private PopupDialog popup;
private String currentText = "";
private JButton editorComponent;
public TablePopupEditor()
{
super(new JTextField());
setClickCountToStart(1);
// Use a JButton as the editor component
editorComponent = new JButton();
editorComponent.setBackground(Color.white);
editorComponent.setBorderPainted(false);
editorComponent.setContentAreaFilled( false );
// Make sure focus goes back to the table when the dialog is closed
editorComponent.setFocusable( false );
// Set up the dialog where we do the actual editing
popup = new PopupDialog();
}
public Object getCellEditorValue()
{
return currentText;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
popup.setText( currentText );
// popup.setLocationRelativeTo( editorComponent );
Point p = editorComponent.getLocationOnScreen();
popup.setLocation(p.x, p.y + editorComponent.getSize().height);
popup.show();
fireEditingStopped();
}
});
currentText = value.toString();
editorComponent.setText( currentText );
return editorComponent;
}
/*
* Simple dialog containing the actual editing component
*/
class PopupDialog extends JDialog implements ActionListener
{
private JTextArea textArea;
public PopupDialog()
{
super((Frame)null, "Change Description", true);
textArea = new JTextArea(5, 20);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
textArea.getInputMap().put(keyStroke, "none");
JScrollPane scrollPane = new JScrollPane( textArea );
getContentPane().add( scrollPane );
JButton cancel = new JButton("Cancel");
cancel.addActionListener( this );
JButton ok = new JButton("Ok");
ok.setPreferredSize( cancel.getPreferredSize() );
ok.addActionListener( this );
JPanel buttons = new JPanel();
buttons.add( ok );
buttons.add( cancel );
getContentPane().add(buttons, BorderLayout.SOUTH);
pack();
getRootPane().setDefaultButton( ok );
}
public void setText(String text)
{
textArea.setText( text );
}
/*
* Save the changed text before hiding the popup
*/
public void actionPerformed(ActionEvent e)
{
if ("Ok".equals( e.getActionCommand() ) )
{
currentText = textArea.getText();
}
textArea.requestFocusInWindow();
setVisible( false );
}
}
private static void createAndShowUI()
{
String[] columnNames = {"Item", "Description"};
Object[][] data =
{
{"Item 1", "Description of Item 1"},
{"Item 2", "Description of Item 2"},
{"Item 3", "Description of Item 3"}
};
JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(1).setPreferredWidth(300);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
// Use the popup editor on the second column
TablePopupEditor popupEditor = new TablePopupEditor();
table.getColumnModel().getColumn(1).setCellEditor( popupEditor );
JFrame frame = new JFrame("Popup Editor Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextField(), BorderLayout.NORTH);
frame.add( scrollPane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Using this approach you don't continually manipulate the row size. You could even customize the code to make the dialog fit the width of the cell and appear below the cell.
Seems you need to implement your own TableCellEditor, read more in tutorial.
For example like that:
private class CustomEditor extends AbstractCellEditor implements TableCellEditor{
private JTextPane pane = new JTextPane();
private JScrollPane scroll = new JScrollPane(pane);
private int row = -1;
#Override
public Object getCellEditorValue() {
return pane.getText();
}
#Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
if(this.row != -1)
table.setRowHeight(this.row, 20);
this.row = row;
table.setRowHeight(row, 100);
pane.setText(value == null ? "" : value.toString());
return scroll;
}
}
and then set it as column editor: table.getColumn("COMMENTS").setCellEditor(new CustomEditor());
In my form, when i press ENTER button in my keyboard, the okAction() method should be invoke (and invoke perfectly).
My problem is in focus state, When i fill the text fields and then press the ENTER button, the okAction() didn't invoked, because the focus is on the second text field (not on the panel).
How fix this problem?
public class T3 extends JFrame implements ActionListener {
JButton cancelBtn, okBtn;
JLabel fNameLbl, lNameLbl, tempBtn;
JTextField fNameTf, lNameTf;
public T3() {
add(createForm(), BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new T3();
}
});
}
public JPanel createForm() {
JPanel panel = new JPanel();
panel.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "Button");
panel.getActionMap().put("Button", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
okAction();
}
});
okBtn = new JButton("Ok");
okBtn.addActionListener(this);
cancelBtn = new JButton("Cancel");
tempBtn = new JLabel();
fNameLbl = new JLabel("First Name");
lNameLbl = new JLabel("Last Name");
fNameTf = new JTextField(10);
fNameTf.setName("FnTF");
lNameTf = new JTextField(10);
lNameTf.setName("LnTF");
panel.add(fNameLbl);
panel.add(fNameTf);
panel.add(lNameLbl);
panel.add(lNameTf);
panel.add(okBtn);
panel.add(cancelBtn);
panel.add(tempBtn);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 3, 2, 50, 10, 80, 60);
return panel;
}
private void okAction() {
if (fNameTf.getText().trim().length() != 0 && lNameTf.getText().trim().length() != 0) {
System.out.println("Data saved");
} else System.out.println("invalid data");
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okBtn) {
okAction();
}
}
}
Declare a default button for your GUI's JRootPane:
public T3() {
//!! ..... etc...
setVisible(true);
getRootPane().setDefaultButton(okBtn);
}
In fact with a default button set, I don't see that you need to use key bindings.