Java Swing getvalue from combobox object - java

I made simple project in Java using swing. Below is code, I would like to get selected value from combobox placed in table, how I can do this? Good to be if thats data were placed in array. Thx for all answers.
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JFormattedTextField;
public class SWD_GUI implements ActionListener {
JButton button;
public JFrame frame;
private JTable table;
private JTable table_1;
private JTable table_2;
private JTable table_3;
private JFormattedTextField formattedTextField;
JComboBox<ComboItem> comboBox;
TableColumn column_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SWD_GUI window = new SWD_GUI();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SWD_GUI() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 846, 546);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.getContentPane().setLayout(null);
JLabel lblSwdTelephone = new JLabel("SWD TELEPHONE");
lblSwdTelephone.setHorizontalAlignment(SwingConstants.CENTER);
lblSwdTelephone.setFont(new Font("Tahoma", Font.BOLD, 16));
lblSwdTelephone.setBounds(10, 11, 810, 20);
frame.getContentPane().add(lblSwdTelephone);
button = new JButton("DO IT!");
button.setBounds(379, 432, 89, 23);
frame.getContentPane().add(button);
button.addActionListener(this);
createTables();
}
private void createTables() {
createFirstTable();
createSecondTable();
createThirdTable();
createFourthTable();
}
private void createFourthTable() {
TableModel tableModel_3 = new DefaultTableModel(new Object[][] {
{ "BATTERY", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_3, tableModel_3, 304);
}
private void createThirdTable() {
TableModel tableModel_2 = new DefaultTableModel(new Object[][] {
{ "SPECIFICATION", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_2, tableModel_2, 214);
}
private void createSecondTable() {
TableModel tableModel_1 = new DefaultTableModel(new Object[][] {
{ "PRICE", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_1, tableModel_1, 124);
}
private void createFirstTable() {
TableModel tableModel = new DefaultTableModel(new Object[][] {
{ null, "Price", "Specification", "Baterry" },
{ "Price", new Double(1.0), null, null },
{ "Specification", null, new Double(1.0), null },
{ "Baterry", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table, tableModel, 39);
}
private void createTable(JTable table, TableModel tableModel, int y) {
table = new JTable();
table.setColumnSelectionAllowed(true);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
table.setBounds(10, y, 424, 64);
frame.getContentPane().add(table);
table.setModel(tableModel);
addColumn(table, 1);
addColumn(table, 2);
addColumn(table, 3);
}
class ComboItem
{
public String key;
public String value;
public ComboItem(String key, String value)
{
this.key = key;
this.value = value;
}
//#Override
public String toString()
{
return key;
}
public String getKey()
{
return key;
}
public String getValue()
{
return value;
}
}
public void addColumn(JTable table, int columnIndex) {
{
TableColumn column_1 = table.getColumnModel()
.getColumn(columnIndex);
JComboBox<Object> comboBox = new JComboBox<Object>();
comboBox.addItem(new ComboItem("1", "1.0"));
comboBox.addItem(new ComboItem("3", "3.0"));
comboBox.addItem(new ComboItem("5", "5.0"));
comboBox.addItem(new ComboItem("7", "7.0"));
comboBox.addItem(new ComboItem("1/3", "0.333333333"));
comboBox.addItem(new ComboItem("1/5", "0.2"));
comboBox.addItem(new ComboItem("1/7", "0,1428571428571429"));
//comboBox.addItem((double) 1);
//comboBox.addItem((double) 3);
//comboBox.addItem((double) 5);
//comboBox.addItem((double) 7);
//comboBox.addItem((double) 9);
//comboBox.addItem((double) (1.0/3.0));
//comboBox.addItem((double) (1.0/5.0));
//comboBox.addItem((double) (1.0/7.0));
//comboBox.addItem((double) (1.0/9.0));
column_1.setCellEditor(new DefaultCellEditor(comboBox));
Object item = comboBox.getSelectedItem();
String value = ((ComboItem)item).getValue();
System.out.println(value);
}
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(462, 36, 358, 333);
frame.getContentPane().add(formattedTextField);
}
//public void getContents() {
// TODO Auto-generated method stub
//}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stud
Object source = e.getSource();
if (source == button) {
}
//for(int i = 0; i<7; i++){
//System.out.println(anArrays[i]);
//}
// getContents();
//int row = table.getSelectedRow();
//String idS = table.getValueAt(row, 0).toString();
//String al = column_1.getMaxWidth().toString();
//System.out.println(al);
// System.out.println(item);
// formattedTextField.add(table);
//formattedTextField.setValue(table);
}
}

You get the data from the table, not the combo box.
ComboItem item = (ComboItem)table.getValueAt(...);
System.out.println( item.getValue() );

Related

How to set Key Binding to editable JTable in swing?

I want to remove selected row of JTable by pressing only Delete button.
When I press Delete, selected cell becomes editable and my action(on jframe) does not receive KeyEvent.
Please, run this demo to see the effect:
public class TestTableKeyBinding extends JFrame {
// private static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TestTableKeyBinding test = new TestTableKeyBinding();
test.setVisible(true);
}
});
}
TestTableKeyBinding() {
super();
initUI();
addKeyBindings();
}
private void initUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] headers = new String[] { "apples", "bananas" };
String[][] data = new String[][] { { "1", "2" }, { "4", "6" }, { "5", "7" }, { "1", "3" }, { "2", "11" } };
table = new JTable(data, headers);
table.setRowSelectionAllowed(true);
this.add(new JScrollPane(table));
this.pack();
this.setSize(new Dimension(300, 400));
}
private void addKeyBindings() {
// root maps
InputMap im = this.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = this.getRootPane().getActionMap();
// add custom action
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "save");
am.put("save", saveAction());
}
private AbstractAction saveAction() {
AbstractAction save = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
table.editingCanceled(null);
table.editingStopped(null);
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
((DefaultTableModel) table.getModel()).removeRow(selectedRow);
}
}
};
return save;
}
}
Thanks for help!
I have tested your code and made some changes as mentioned below. Now it's working fine.
private void addKeyBindings() {
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
table.getActionMap().put("save", saveAction());
table.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, "save");
}
Make one more change as mentioned below
table = new JTable(new DefaultTableModel(data,headers));
otherwise it will result in ClassCastException at below line
((DefaultTableModel) table.getModel()).removeRow(selectedRow);
Here is complete code:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;
public class TestTableKeyBinding extends JFrame {
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TestTableKeyBinding test = new TestTableKeyBinding();
test.setVisible(true);
}
});
}
TestTableKeyBinding() {
super();
initUI();
addKeyBindings();
}
private void initUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] headers = new String[] { "apples", "bananas" };
String[][] data = new String[][] { { "1", "2" }, { "4", "6" }, { "5", "7" }, { "1", "3" },
{ "2", "11" } };
table = new JTable(new DefaultTableModel(data,headers));
table.setRowSelectionAllowed(true);
this.add(new JScrollPane(table));
this.pack();
this.setSize(new Dimension(300, 400));
}
private void addKeyBindings() {
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
table.getActionMap().put("save", saveAction());
table.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, "save");
}
private AbstractAction saveAction() {
AbstractAction save = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
table.editingCanceled(null);
table.editingStopped(null);
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
((DefaultTableModel) table.getModel()).removeRow(selectedRow);
}
}
};
return save;
}
}

Modify the instancer object into instanced object

I have an object A that instance another object B.
I was wondering whether or not is possible to modify A with instructions in B.
In my circumstance, I have a Timetable (its code is under "Object A") that open (by InsertLesson.setVisible(true);) a Window to let the user compile its cells with lesson. At this time, the Window (InsertLesson, code under "object B") get the lesson selection by user but it is not able to write in the table that selection. How can I do?
Here the code
Object A:
public class TablePanel extends JPanel
{
private JTable table;
public Tabella()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane jps = new JScrollPane(table);
add(jps);
add(new JScrollPane(table));
table.setCellSelectionEnabled(true);
table.addMouseListener(
new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
if (col>0) {
if (e.getClickCount() > 1) {
if (row == 5 | row == 6)
{
JOptionPane.showMessageDialog(null, "Impossible to set lesson.");
return;
}
else {
table.getColumnName(col);
String day = table.getColumnName(col);
String hour = (String) table.getValueAt(row, 0);
InsertLesson cell = new InsertLesson(day, hour);
cel.setVisible(true);
}
}
}
}
}
);
}
private class MyTableModel extends AbstractTableModel {
private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private String[][] data = {{"8:30 - 9:30","","","","","",""},
{"9:30 - 10:30","","","","","",""},
{"10:30 - 11:30","","","","","",""},
{"11:30 - 12:30","","","","","",""},
{"12:30 - 13:30","","","","","",""},
{"13:30 - 14:30","","","","","",""},
{"14:30 - 15:30","","","","","",""},
{"15:30 - 16:30","","","","","",""},
{"16:30 - 17:30","","","","","",""}};
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columns[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
Object B (which has to modify A):
public InsertLesson (String day, String hour)
{
initialize(day, hour);
}
private void initialize(String day, String hour) {
this.setSize(600,200);
this.setTitle("Insert Lesson");
this.setLocationRelativeTo(null);
String[] lessons = {"math", "english", "art"};
String [] classrooms = {"class1", "class2"};
JPanel centralPnl = new JPanel();
this.getContentPane().add(centralPnl, BorderLayout.CENTER);
final JComboBox classBox = new JComboBox(classrooms );
centralPnl.add(classBox);
final JComboBox lessonsBox = new JComboBox(lessons);
centralPnl.add(lessonsBox);
JPanel southPnl = new JPanel();
this.getContentPane().add(southPnl, BorderLayout.SOUTH);
JButton insLessonBtn = new JButton("Insert Lesson");
southPnl.add(insLessonBtn);
lessonsBox.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
selectedLesson = lessonsBox.getSelectedItem().toString();
}
}
});
classBox.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
{
selectedClass = classBox.getSelectedItem().toString();
}
}
});
class MouseSpy implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(null,"Do something for modify table with\n"
+ "values of selectedLesson and selectedClass");
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
MouseListener listener = new MouseSpy();
insLessonBtn.addMouseListener(listener);
}
}
}
To update the table in A, B must invoke the method setValueAt() on the TableModel of A. Alternatively, add a method to your TableModel that does the update. A typical implementation of setValueAt() is seen here. If that doesn't help, please edit your question to include an sscce that exhibits the problem you encounter.
Addendum: I want to update the table … after the user press the … button.
As a concrete example using your TableModel, the Update button below updates the table's model with each press. Compare the implmentation of setValueAt () to the one cited above. The button's actionPerformed() method accesses a final reference to the TableModel in the enclosing scope, but you can pass a reference to your TableModel as a parameter to the constructor of InsertLesson.
Addendum: Would you write [it for] me?
No, but I will outline the approach, assuming a class InsertLesson,
TableModel model = new MyTableModel();
JTable table = new JTable(model);
InsertLesson cell = new InsertLesson(day, hour, model);
…
class InsertLesson {
TableModel model;
public InsertLesson(String day, String hour, TableModel model) {
this.model = model;
…
}
…
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* #see http://stackoverflow.com/a/18764073/230513
*/
public class Test {
private static class MyTableModel extends AbstractTableModel {
private String[] columns = {
"Time", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private String[][] data = {
{"8:30 - 9:30", "", "", "", "", ""},
{"9:30 - 10:30", "", "", "", "", ""},
{"10:30 - 11:30", "", "", "", "", ""},
{"11:30 - 12:30", "", "", "", "", ""},
{"12:30 - 13:30", "", "", "", "", ""},
{"13:30 - 14:30", "", "", "", "", ""},
{"14:30 - 15:30", "", "", "", "", ""},
{"15:30 - 16:30", "", "", "", "", ""},
{"16:30 - 17:30", "", "", "", "", ""}};
#Override
public int getColumnCount() {
return columns.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columns[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public void setValueAt(Object aValue, int row, int col) {
data[row][col] = (String) aValue;
fireTableCellUpdated(row, col);
}
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final TableModel model = new MyTableModel();
f.add(new JScrollPane(new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(600, 128);
}
}));
f.add(new JButton(new AbstractAction("Update") {
#Override
public void actionPerformed(ActionEvent e) {
model.setValueAt(String.valueOf(e.getWhen() % 1000000), 1, 1);
}
}), BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}

The Auto Completion For JTable Using J2s [Auto complete ComboBox] Not Working

With refrence To The Question! By abg and answer By mKorBel.
And Using J2s Auto Complete Combo Box!
The Code in the Answer By mKorBel For My Case Is Not Working When I use Item Class instead of String for ArrayList items.
Vector<Vector<String>> comboData=util.GetComboData();
ArrayList<String> listSomeString = util.GetListForComboBox(comboData);
Java2sAutoComboBox comboBox = new Java2sAutoComboBox(listSomeString);
comboBox.setDataList(listSomeString);
column.setCellEditor(new DefaultCellEditor(comboBox));
The Code For Function GetListForComboBox is as follows
public ArrayList GetListForComboBox(Vector<Vector<String>> comboData) {
//Vector model = new Vector();
ArrayList<Item_> listSomeString = new ArrayList<Item_>();
try {
String strdata;
for (int i = 0; i < comboData.size(); i++) {
String id = comboData.get(i).elementAt(0).toString();
if (comboData.get(i).elementAt(1) != null) {
strdata = comboData.get(i).elementAt(1).toString();
} else {
strdata = "";
}
Item_ it = new Item_(id, strdata);
//model.addElement(it);
listSomeString.add(it);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return listSomeString;
}
The Code for Item_ class is as follows
public class Item_
{
private String description;
private String id;
public Item_( String id,String description)
{
this.description = description;
this.id = id;
}
public String getId()
{ return id;
}
public String getDescription()
{ return description;
}
public String toString()
{ return description;
}
public void setDescription(String desc)
{ description=desc;
}
}
no idea, have to create an HashMap or MutableComboBoxModel, because AutoComplete is strictly based on Array, have to change constructor
my curiosity about Bug (I can't be able to simulating this issue), is presented from this code too (disable comboBox.setRenderer(new ItemRenderer());)
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class TableRenderDemo extends JPanel {
private static final long serialVersionUID = 1L;
public TableRenderDemo() {
super(new BorderLayout(5, 5));
final JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
table.setRowHeight(20);
JScrollPane scrollPane = new JScrollPane(table);
initColumnSizes(table);
setUpSportColumn(table, table.getColumnModel().getColumn(2));
add(scrollPane, BorderLayout.CENTER);
JButton resetButton = new JButton("Reset to default");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < table.getRowCount(); i++) {
table.getModel().setValueAt("None of the above", i, 2);
}
}
});
add(resetButton, BorderLayout.SOUTH);
}
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
private void setUpSportColumn(JTable table, TableColumn sportColumn) {
ArrayList<String> listSomeString = new ArrayList<String>();
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
listSomeString.add("Pool");
listSomeString.add("None of the above");
JComboBox comboBox = new JComboBox();
comboBox.addItem(new Item(1, "-"));
comboBox.addItem(new Item(2, "Snowboarding"));
comboBox.addItem(new Item(3, "Rowing"));
comboBox.addItem(new Item(4, "Knitting"));
comboBox.addItem(new Item(5, "Speed reading"));
comboBox.addItem(new Item(6, "Pool"));
comboBox.addItem(new Item(7, "None of the above"));
comboBox.setMaximumRowCount(3);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Item item = (Item) comboBox.getSelectedItem();
System.out.println(item.getId() + " : " + item.getDescription());
}
});
comboBox.setRenderer(new ItemRenderer());
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
class ItemRenderer extends BasicComboBoxRenderer {
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
Item item = (Item) value;
setText(item.getDescription().toUpperCase());
}
if (index == -1) {
Item item = (Item) value;
setText("" + item.getId());
}
return this;
}
}
class Item {
private int id;
private String description;
public Item(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
#Override
public String toString() {
return description;
}
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
{"John", "Doe", "Rowing", new Integer(3), true}, {"Sue", "Black", "Knitting", new Integer(2), false},
{"Jane", "White", "Speed reading", new Integer(20), true}, {"Joe", "Brown", "Pool", new Integer(10), false}};
public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println("New value of data: " + getValueAt(row, col));
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableRenderDemo newContentPane = new TableRenderDemo();
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
have to play with Caret (quick implementations for Caret, sorry too lazy, without any idea), otherwise all editor is selected, backspace selected 1st Item by default
import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.JTextComponent;
public class TableRenderDemo extends JPanel {
private static final long serialVersionUID = 1L;
public TableRenderDemo() {
super(new BorderLayout(5, 5));
final JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
initColumnSizes(table);
setUpSportColumn(table, table.getColumnModel().getColumn(2));
add(scrollPane, BorderLayout.CENTER);
JButton resetButton = new JButton("Reset to default");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < table.getRowCount(); i++) {
table.getModel().setValueAt("None of the above", i, 2);
}
}
});
add(resetButton, BorderLayout.SOUTH);
}
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
private void setUpSportColumn(JTable table, TableColumn sportColumn) {
ArrayList<String> listSomeString = new ArrayList<String>();
listSomeString.add("-");
listSomeString.add("Snowboarding");
listSomeString.add("Rowing");
listSomeString.add("Knitting");
listSomeString.add("Speed reading");
listSomeString.add("Pool");
listSomeString.add("None of the above");
AutoComboBox comboBox = new AutoComboBox(listSomeString);
comboBox.setDataList(listSomeString);
comboBox.setMaximumRowCount(3);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
System.out.println(comboBox.getSelectedItem());
}
});
JTextComponent editor = ((JTextField) comboBox.getEditor().getEditorComponent());
editor.setCaretPosition(editor.getCaretPosition());
editor.moveCaretPosition(0);
editor.addFocusListener(focsListener);
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
private FocusListener focsListener = new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
dumpInfo(e);
}
#Override
public void focusLost(FocusEvent e) {
//dumpInfo(e);
}
private void dumpInfo(FocusEvent e) {
//System.out.println("Source : " + name(e.getComponent()));
//System.out.println("Opposite : " + name(e.getOppositeComponent()));
//System.out.println("Temporary: " + e.isTemporary());
final Component c = e.getComponent();
if (c instanceof JFormattedTextField) {
EventQueue.invokeLater(new Runnable() {
public void run() {
((JFormattedTextField) c).requestFocus();
((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
((JFormattedTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
EventQueue.invokeLater(new Runnable() {
public void run() {
((JTextField) c).requestFocus();
((JTextField) c).setText(((JTextField) c).getText());
((JTextField) c).selectAll();
}
});
}
}
private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
class ItemRenderer extends BasicComboBoxRenderer {
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
}
if (index == -1) {
}
return this;
}
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
private Object[][] data = {{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
{"John", "Doe", "Rowing", new Integer(3), true}, {"Sue", "Black", "Knitting", new Integer(2), false},
{"Jane", "White", "Speed reading", new Integer(20), true}, {"Joe", "Brown", "Pool", new Integer(10), false}};
public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println("New value of data: " + getValueAt(row, col));
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableRenderDemo newContentPane = new TableRenderDemo();
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
I know this is an old question but in case it can help someone out there, here is how to do what OP asked:
In Java2sAutoCombobox, replace the function setSelectedValue(Object obj) by this one instead:
void setSelectedValue(String str)
{
if(!isFired)
{
Object objectToSelect = null;
for(Object obj: getDataList())
{
if( obj.toString().equals(str) )
{
objectToSelect = obj;
break;
}
}
isFired = true;
setSelectedItem(objectToSelect);
fireItemStateChanged(new ItemEvent(this, 701, selectedItemReminder,
1));
isFired = false;
}
}
By doing this, JComboBox.setSelectedItem() will select the object from the datalist instead of its string representation.

How to reset the JComboBox in JTable to the first value of the JComboBox

I'm working on a code in Java Swing. I have created a JComboBox in JTable. It's working :). But once I select a value and click on the save or cancel button, it has to reset to the default value (1st value in the combo box). I tried a lot of ways like combobox.setSelectedIndex(0). This isn't working.
Code:
String[] manTimeHr = { "00","01", "02", "03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
String[] manTimeMin = {"00","01", "02", "03","04","05","06","07","08","09","10","11","12","13", "14", "15","16","17","18","19","20","21","22","23","24","25", "26", "27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
String[][] data = {{"-select-","-select-"}};
String[] cols = {"Hrs","Mins"};
String[][] data1 = {{"-select-","-select-"}};
String[] cols1 = {"Hrs","Mins"};
JLabel manTimeStart = new JLabel("Start From",JLabel.LEFT);
STimeTbl = new JTable(data,cols);
hrsColumn=STimeTbl.getColumnModel().getColumn(0);
minsColumn=STimeTbl.getColumnModel().getColumn(1);
manSiteStimeHrCBx = new JComboBox(manTimeHr);
manSiteStimeHrCBx.setSelectedIndex(0);
hrsColumn.setCellEditor(new DefaultCellEditor(manSiteStimeHrCBx));
manSiteStimeMinCBx = new JComboBox(manTimeMin);
manSiteStimeMinCBx.setSelectedIndex(0);
minsColumn.setCellEditor(new DefaultCellEditor(manSiteStimeMinCBx));
JLabel manTimeEnd = new JLabel("End To",JLabel.LEFT);
ETimeTbl = new JTable(data1,cols1);
hrsColumn1=ETimeTbl.getColumnModel().getColumn(0);
minsColumn1=ETimeTbl.getColumnModel().getColumn(1);
manSiteEtimeHrCBx = new JComboBox(manTimeHr);
manSiteEtimeHrCBx.setSelectedIndex(0);
hrsColumn1.setCellEditor(new DefaultCellEditor(manSiteEtimeHrCBx));
manSiteEtimeMinCBx = new JComboBox(manTimeMin);
manSiteEtimeMinCBx.setSelectedIndex(0);
minsColumn1.setCellEditor(new DefaultCellEditor(manSiteEtimeMinCBx));
.
.
.
.
if("Save".equals(e.getActionCommand())) {
try{
mSHr = Integer.parseInt((String)manSiteStimeHrCBx.getSelectedItem());
mEHr=Integer.parseInt((String)manSiteEtimeHrCBx.getSelectedItem());
mSMin=Integer.parseInt((String)manSiteStimeMinCBx.getSelectedItem());
mEMin=Integer.parseInt((String)manServEtimeMinCBx.getSelectedItem());
}catch (Exception en){
System.out.println("Main Exception : "+ en);
return;
}
if(validateBlockTime(mSHr,mEHr,mSMin,mEMin) != true)
{
System.out.println("Enter valid Time");
return;
}
manSiteStimeHrCBx.setSelectedIndex(0);
manSiteStimeMinCBx.setSelectedIndex(0);
manSiteEtimeHrCBx.setSelectedIndex(0);
manSiteEtimeMinCBx.setSelectedIndex(0);
.
.
.
private static boolean validateBlockTime(int val3, int val4, int val5, int val6){
if(val4 > val3)
return true;
else if((val4 == val3) && (val6 > val5))
return true;
else
return false;
}
JTable tutorial contains description about Combo Box as an Editor
all data are stored in the XxxTableModel
all changes in the JTable view are described in Concepts: Editors and Renderers
then you have to know that in the XxxTableModel is stored only String value (or Double or Long or Integer or Icon, depends of value stored in ComboBoxModel) for Combo Box as an Editor
result is
quick code (sorry I'm so lazy) based on tutorial code
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class TableRenderDemo extends JPanel {
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
public TableRenderDemo() {
super(new BorderLayout(5, 5));
final JTable table = new JTable(new MyTableModel());
table.setRowHeight(18);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
initColumnSizes(table);
setUpSportColumn(table, table.getColumnModel().getColumn(2));
add(scrollPane);
JButton resetButton = new JButton("Reset to default");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < table.getRowCount(); i++) {
table.getModel().setValueAt("None of the above", i, 2);
}
}
});
add(resetButton, BorderLayout.SOUTH);
}
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
if (DEBUG) {
System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth);
}
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
private void setUpSportColumn(JTable table, TableColumn sportColumn) {
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
private class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
private Object[][] data = {
{"Kathy", "Smith", "Snowboarding", new Integer(5), false},
{"John", "Doe", "Rowing", new Integer(3), true},
{"Sue", "Black", "Knitting", new Integer(2), false},
{"Jane", "White", "Speed reading", new Integer(20), true},
{"Joe", "Brown", "Pool", new Integer(10), false}
};
public final Object[] longValues = {"Jane", "Kathy", "None of the above", new Integer(20), Boolean.TRUE};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println(getValueAt(row, col));
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
output from AbstractTableModel, code line 135th.
run:
None of the above
None of the above
None of the above
None of the above
None of the above
BUILD SUCCESSFUL (total time: 37 seconds)

JTable frozen column alignment issue (Java 1.6)

I have implemented JTable frozen columns as below.When scrolling on a large dataset using vertical scrollbar, the columns in the frozen table are not aligned with the main table.Please help
JTable table,fixedTable
JScrollPane _scrl_table;
table = new JTable();
table.setName("MAIN_TABLE");
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setVisibleRowCount(8);
fixedTable = new JTable();
fixedTable.setName("FIXED_TABLE");
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
_scrl_table = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
fixedTable.setPreferredScrollableViewportSize(fixedTable.getPreferredSize());
_scrl_table.setRowHeaderView( fixedTable );
_scrl_table.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
maybe there no reason to use two JTables
use proper RowNumberTable by camickr or RowHeader
you can to share AdjustmentListener, with or without BoundedRangeModel
example scrolling horizontal directions, have to change to the vertical
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class FixedRowExample extends JFrame {
private static final long serialVersionUID = 1L;
private Object[][] data;
private Object[] column;
private JTable fixedTable, table;
private int FIXED_NUM = 2;
public FixedRowExample() {
super("Fixed Row Example");
data = new Object[][]{
{"a", "", "", "", "", ""},
{"", "b", "", "", "", ""},
{"", "", "c", "", "", ""},
{"", "", "", "d", "", ""},
{"", "", "", "", "e", ""},
{"", "", "", "", "", "f"},
{"fixed1", "", "", "", "", "", "", ""},
{"fixed2", "", "", "", "", "", "", ""}};
column = new Object[]{"A", "B", "C", "D", "E", "F"};
AbstractTableModel model = new AbstractTableModel() {
private static final long serialVersionUID = 1L;
#Override
public int getColumnCount() {
return column.length;
}
#Override
public int getRowCount() {
return data.length - FIXED_NUM;
}
#Override
public String getColumnName(int col) {
return (String) column[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public void setValueAt(Object obj, int row, int col) {
data[row][col] = obj;
}
public boolean CellEditable(int row, int col) {
return true;
}
};
AbstractTableModel fixedModel = new AbstractTableModel() {
private static final long serialVersionUID = 1L;
#Override
public int getColumnCount() {
return column.length;
}
#Override
public int getRowCount() {
return FIXED_NUM;
}
#Override
public Object getValueAt(int row, int col) {
return data[row + (data.length - FIXED_NUM)][col];
}
};
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable = new JTable(fixedModel);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane fixedScroll = new JScrollPane(fixedTable) {
private static final long serialVersionUID = 1L;
#Override
public void setColumnHeaderView(Component view) {
} // work around
}; // fixedScroll.setColumnHeader(null);
fixedScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollBar bar = fixedScroll.getVerticalScrollBar();
JScrollBar dummyBar = new JScrollBar() {
private static final long serialVersionUID = 1L;
#Override
public void paint(Graphics g) {
}
};
dummyBar.setPreferredSize(bar.getPreferredSize());
fixedScroll.setVerticalScrollBar(dummyBar);
final JScrollBar bar1 = scroll.getHorizontalScrollBar();
JScrollBar bar2 = fixedScroll.getHorizontalScrollBar();
bar2.addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(AdjustmentEvent e) {
bar1.setValue(e.getValue());
}
});
scroll.setPreferredSize(new Dimension(400, 100));
fixedScroll.setPreferredSize(new Dimension(400, 52));
getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(fixedScroll, BorderLayout.SOUTH);
}
public static void main(String[] args) {
FixedRowExample frame = new FixedRowExample();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}

Categories