Unable to get hidden JTable column - java

I use a JTable with two buttons(Active & Inactive Buttons) . When I click on the Inactive button I hide the first column of the table. Also I try to show the first column when I click on the Active button. But the problem is , when I click on the Active button I couldn't display the first column with its preferred size .
This is the code for hide the first column,
expTable.getColumnModel().getColumn(0).setMinWidth(0);
expTable.getColumnModel().getColumn(0).setMaxWidth(0);
expTable.getColumnModel().getColumn(0).setWidth(0);
This is the code for get back fist column,
expTable.getColumnModel().getColumn(0).setMinWidth(0);
expTable.getColumnModel().getColumn(0).setMaxWidth(300);
expTable.getColumnModel().getColumn(0).setWidth(100);
First I click on the Inactive button and then I click on the Active button . But this is not working .
Have any idea to how to do above .
private void inactiveButtonActionPerformed(java.awt.event.ActionEvent evt){
expTable.getColumnModel().getColumn(0).setMinWidth(0);
expTable.getColumnModel().getColumn(0).setMaxWidth(0);
expTable.getColumnModel().getColumn(0).setWidth(0);
}
private void activeButtonActionPerformed(java.awt.event.ActionEvent evt)
{
expTable.getColumnModel().getColumn(0).setMinWidth(0);
expTable.getColumnModel().getColumn(0).setMaxWidth(300);
expTable.getColumnModel().getColumn(0).setWidth(100);
}

Don't attempt to "hide" a TableColumn by playing with the width. The TableColumn is still part of the table so when the user tabs from column to column, focus will go to the hidden column and disappear from the users view which is very confusing.
Instead you should remove the TableColumn from the TableColumnModel. Then you can add the TableColumn back to the TableColumnModel when needed.
Check out the Table Column Manager which manages this concept for you. If you don't use the full functionality of the class you can use the hideColumn(...) and showColumn(...) to do a simple toggle on the first column.

In the past when I've need to do something like, I've simply removed and re-added the TableColumn
This is a pretty crude example of the concept...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Column 1");
model.addColumn("Column 2");
model.addColumn("Column 3");
model.addColumn("Column 4");
model.setRowCount(100);
JTable table = new JTable(model);
add(new JScrollPane(table));
JToggleButton toggle = new JToggleButton("Toggle");
toggle.addActionListener(new ActionListener() {
private TableColumn column;
#Override
public void actionPerformed(ActionEvent e) {
TableColumnModel columnModel = table.getColumnModel();
if (toggle.isSelected()) {
column = columnModel.getColumn(0);
table.getColumnModel().removeColumn(column);
} else if (column != null) {
columnModel.addColumn(column);
columnModel.moveColumn(columnModel.getColumnCount() - 1, 0);
}
}
});
add(toggle, BorderLayout.SOUTH);
}
}
}
I actually spent sometime putting together a custom TableModel which provide the ability to hide and show columns, which basically wrapped this functionality up in a nice reusable package

Related

JPopupMenu Menu Not Appearing

This is the code for my JPopupMenu and how I added it, it is supposed to respond when I right click the table:
JMenuItem deleteRows = new JMenuItem("Delete Row");
popup.add(deleteRows);
personTable.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3) {
popup.show(personTable, e.getX(), e.getY());
}
}
});
I am not sure why the popup menu is not appearing when I right-click the table in the application. I would appreciate it if somebody told me what I am doing wrong.
The trigger for a popup is different for different OS's, you can't simply use mousePressed and your certainly shouldn't be using e.getButton() == MouseEvent.BUTTON3
From How to use Menus, Bringing up a PopupMenu
The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component that is popup-enabled. In the Java look and feel, the customary trigger is either pressing the right mouse button (for a popup that goes away when the button is released) or clicking it (for a popup that stays up).
Instead, you should be checking for each of the mouse events, pressed, released and clicked. You should also be using MouseEvent#isPopupTrigger to determine if the event is a popup trigger for the OS.
Having said all that, it would be simpler to just us JComponent#setComponentPopupMenu and let it decide instead
personTable.setComponentPopupMenu(popup);
Runnable example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(10, 10);
JTable table = new JTable(model);
JMenuItem mi = new JMenuItem("I'll be your menu for today");
JPopupMenu popup = new JPopupMenu();
popup.add(mi);
table.setComponentPopupMenu(popup);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Here is the simple way to create a popupMenu without using mouse listener:
JpopupMenu popMenu = new JpopupMenu() ;
JMenuItem item = new JMenuItem("my item") ;
popMenu.add(item);
myTable.setComponentPopupMenu(popMenu);

JComboBox in JTable cell. Selection change affects JComboBox in other rows

I have been lurking around here for a couple years or so, never needed to ask a question before because I have always found my answer in someone else's question. Thank you!
I guess the lurking has come to an end. I have seen similar questions but not exactly this situation on here:
I have a 2 column JTable with a JComboBox in the first column, and an integer in the second column.
JComboBox has ItemListener set up so that when the selection in the JComboBox is changed the value in the Integer column is set to the comboBox selected index. Right click on the table renders JPopupMenu with addRow() as MouseEvent.
It works fine as long as I add all the rows I want when setting up the DefaultTableModel. But, if I start the model with only one row and use the MouseEvent (or any other method of adding rows other than adding them in parameters of DefaultTableModel) to add rows, when we start changing the selections in the combo boxes it will change the integer values in other rows.
For example: If i run the program and immediately add two more rows via MouseEvent, I then select Zero from combo in row 0, One from combo in row 1, and Two from combo in row 2. All fine so far...
Then I go back to row 0 and as soon as I activate the combobox (I haven't selected an item yet...) it changes the integer in row 2 to 0.
Can anyone tell me how to stop the integers from changing in this manner?
here is a trial code:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class JComboBoxInJTable {
public static void main(String[] args) {
new JComboBoxInJTable();
}
public JComboBoxInJTable() {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"ComboBox", "Index"}, 1);
JTable table = new JTable(model);
//popup menu to add row
JPopupMenu popup = new JPopupMenu();
JMenuItem newRow;
newRow = new JMenuItem("New Row");
newRow.setToolTipText("Add new row.");
newRow.addActionListener((ActionEvent nr) -> {
model.addRow(new Object[]{"", ""});
});
popup.add(newRow);
//set up right-click to open popup menu
table.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent rc) {
if (SwingUtilities.isRightMouseButton(rc)) {
if (table.getSelectedRow() >= 0) {
popup.show(table, rc.getX(), rc.getY());
}
}
}
});
JComboBox combo = new JComboBox(new Object[]{"Zero", "One", "Two", "Three"});
combo.addItemListener((ItemEvent e) -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
//sets value of cell to left of combobox to comboboxe's selected index
table.setValueAt(combo.getSelectedIndex(), table.getSelectedRow(), 1);
} else {
//do nothing...
}
});
DefaultCellEditor comboEditor = new DefaultCellEditor(combo);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setCellEditor(comboEditor);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
As usual I am sure this is something simple that I am missing. Thanks Again for the help in the past and in the future!
Don't use MouseListeners to show popup menus, different OSs have different triggers for the popups, and not all are triggered by mousePressed. Instead use JComponent#setComponentPopupMenu and let the API deal with it
Don't modify the state of the model from an editor. This can place the model into an invalidate state and cause other side effects, like you have now. Once seems to be happening is when a new row is selected, the editor is been updated with the rows cell value, but the row selection hasn't been set, so the table still thinks the previous row is still selected. Instead, use the models setValueAt method to make decisions about what to do once the first columns value is changed.
For example...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class JComboBoxInJTable {
public static void main(String[] args) {
new JComboBoxInJTable();
}
private List<String> comboData;
public JComboBoxInJTable() {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"ComboBox", "Index"}, 1) {
#Override
public void setValueAt(Object aValue, int row, int column) {
super.setValueAt(aValue, row, column);
if (column == 0) {
String value = aValue == null ? null : aValue.toString();
if (aValue == null) {
super.setValueAt(null, row, 1);
} else {
super.setValueAt(comboData.indexOf(aValue), row, 1);
}
}
}
};
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
table.setGridColor(Color.GRAY);
//popup menu to add row
JPopupMenu popup = new JPopupMenu();
JMenuItem newRow;
newRow = new JMenuItem("New Row");
newRow.setToolTipText("Add new row.");
newRow.addActionListener((ActionEvent nr) -> {
model.addRow(new Object[]{"", ""});
});
popup.add(newRow);
table.setComponentPopupMenu(popup);
comboData = new ArrayList<>(Arrays.asList(new String[]{"Zero", "One", "Two", "Three"}));
JComboBox combo = new JComboBox(comboData.toArray(new String[comboData.size()]));
// combo.addItemListener((ItemEvent e) -> {
// if (e.getStateChange() == ItemEvent.SELECTED) {
// //sets value of cell to left of combobox to comboboxe's selected index
// System.out.println("Selected row = " + table.getSelectedRow());
// table.setValueAt(combo.getSelectedIndex(), table.getSelectedRow(), 1);
// } else {
// //do nothing...
// }
// });
DefaultCellEditor comboEditor = new DefaultCellEditor(combo);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setCellEditor(comboEditor);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}

Display JCheckBox in JTable

I have a Jtable in which I want to add a JCheckbox in one column. However, when I create a JCheckbox object, javax.swing.JCheckBox is being displayed in the column.Please refer to the image. Can you tell me how to amend that please? I have searched everywhere but cannot seem to find any solution for it. Thank you.
don't add components to your TableModel, that's not the responsibility of the TableModel
You will need to specify the class type of your column. Assuming you're using a DefaultTableModel, you can simply fill the column with a bunch of booleans and this should work - After testing, you will need to override the getColumnClass method of the DefaultTableModel (or what ever TableModel implementation) and make sure that for the "check box" column, it returns Boolean.class
See How to use tables for more details
For example...
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TestCardLayout {
public static void main(String[] args) {
new TestCardLayout();
}
public TestCardLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Random rnd = new Random();
DefaultTableModel model = new DefaultTableModel(new Object[]{"Check boxes"}, 0) {
#Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
};
for (int index = 0; index < 10; index++) {
model.addRow(new Object[]{rnd.nextBoolean()});
}
JTable table = new JTable(model);
final JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

Select text in 2 JTextarea at the same time

I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ?
Should I share events or listeners ?
Thank you
This would be so much easier if JTextComponent supported a selection model...
Basically, what you can do is attach a ChangeListener to the JTextArea's Caret and monitor for changes to the Caret, changing the selection of the other JTextArea in response...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class MirrorTextSelection {
public static void main(String[] args) {
new MirrorTextSelection();
}
public MirrorTextSelection() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextArea left;
private JTextArea right;
private DefaultHighlighter.DefaultHighlightPainter highlightPainter;
public TestPane() {
highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(UIManager.getColor("TextArea.selectionBackground"));
left = new JTextArea(20, 20);
left.setWrapStyleWord(true);
left.setLineWrap(true);
right = new JTextArea(20, 20);
right.setWrapStyleWord(true);
right.setLineWrap(true);
left.setText("I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ? Should I share events or listeners ? Thank you");
right.setText("I am trying to do a small app that compares two similar texts contained in 2 JTextarea. I am wondering if it's possible to select text from the first JTextarea and automatically select the text on the second JTeaxtarea (lets consider that it's guarantee that the 2 JTextarea will have the same text for now) ? Should I share events or listeners ? Thank you");
setLayout(new GridLayout(0, 2));
add(new JScrollPane(left));
add(new JScrollPane(right));
left.getCaret().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
int dot = left.getCaret().getDot();
int mark = left.getCaret().getMark();
right.setCaretPosition(mark);
right.moveCaretPosition(dot);
}
});
}
}
}
Now, when you run this, you will find that the right side doesn't seem to get highlighted...what?!
The selection is changing, it's just not been rendered because the component doesn't have focus...
Instead, you could use a Highlighter to highlight the text...
private DefaultHighlighter.DefaultHighlightPainter highlightPainter;
//...
highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(UIManager.getColor("TextArea.selectionBackground"));
left.getCaret().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
int dot = left.getCaret().getDot();
int mark = left.getCaret().getMark();
right.getHighlighter().removeAllHighlights();
try {
int start = Math.min(dot, mark);
int end = Math.max(dot, mark);
right.getHighlighter().addHighlight(start, end, highlightPainter);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
Okay, this is now working and you can control the background color of the highlight...
There is another alternative...We can replace the Caret of the right JTextArea with one that doesn't hide the selection when focus is lost...
public class HighlightCaret extends DefaultCaret {
#Override
public void install(JTextComponent c) {
super.install(c);
setSelectionVisible(true);
}
#Override
public void focusGained(FocusEvent e) {
JTextComponent component = getComponent();
if (component.isEnabled()) {
if (component.isEditable()) {
setVisible(true);
}
setSelectionVisible(true);
}
}
#Override
public void focusLost(FocusEvent e) {
setVisible(false);
}
}
Then we set the Caret to right...
right.setCaret(nwe HighlightCaret());
This means we don't need the Highlighter code, we can stick with the original and we get control over not only the background selection color but also the foreground selection color as well...

Refresh a Jlist from another JFrame

i want to refresh a JList when i push a button in another JFrame.
So i have a JFrame GuiBoss that manages employees (add,delete,update).When i press the button add, another Jframe opens, in wich i create a new employee.
//Open the "add_form" where i give details about a new employee.
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
GuiBoss gb = new GuiBoss(contrb,boss);
Add_form af = new Add_form(gb,contrb,boss);
af.setVisible(true);
}
//refresh the list with the new employee added.
public void refresh(Employee e){
System.out.println("I reach this point!");
//if i print e.getName() it works, printing the right name that i give in the "add_form"
listModel.addElement(e);
//listModel.clear(); //don't work either.
}
My problem is that when i submit the details about the new employee i call the function refresh(Employee e) from the GuiBoss frame , the message ("I reach this point!") shows up on the console, the size of the listModel changes, but the list it doesen't refresh.
Also i must say that i set the model properly for the list.
//take data from form and call refresh(Employee e) from the main frame("GuiBoss")
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
//String Id = txtID.getText();
String UserName = txtName.getText();
txtHour.setVisible(false);
boolean b = false;
if(rbtnYes.isSelected() == true){
b = true;
}
if(rbtnNo.isSelected() == true){
b = false;
}
if(rbtnYes.isSelected()==false && rbtnNo.isSelected() == false){
System.out.println("Select the presence!");
}
else{
txtOra.setVisible(true);
String Hour = txtHour.getText();
e = new Employee(UserName,b,Hour,boss); //boss i get from main frame when i start this add new employee form
contrb.addEmployee(e);
gb.refresh(e); //gb is of type GuiBoss were i have the function that does
// the refresh
}
}
Please let me know if u have any ideeas.Thanks.
Instead of popping up another frame, why not use a modal JDialog to collect the information about the new employee. When the dialog is closed, you can then extract the details from the dialog and refresh the list from within the current frame.
This prevents the need to expose portions of your API unnecessarily.
Check out How to use Dialogs for details.
Updated
Assuming you've set the model correctly, then your code should work...as per this example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestList03 {
public static void main(String[] args) {
new TestList03();
}
public TestList03() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private DefaultListModel model;
public TestPane() {
setLayout(new BorderLayout());
model = new DefaultListModel();
JList list = new JList(model);
add(new JScrollPane(list));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
model.addElement("New Element");
}
});
add(btn, BorderLayout.SOUTH);
}
}
}
That would suggest that there is something else wrong that you're not showing us...
Updated with possible fix for reference issues
This basically demonstrates passing a reference of the main panel to a sub factory that is responsible for actually adding the value back into the main panel. Normally I'd use a interface of some kind instead of exposing the entire panel to simply provide access to a single method, but this was a quick example.
It uses both a normal implements and inner class as ActionListener to demonstrate the two most common means for passing a reference of "self" to another class.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestList03 {
public static void main(String[] args) {
new TestList03();
}
public TestList03() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements ActionListener {
private DefaultListModel model;
public TestPane() {
setLayout(new BorderLayout());
model = new DefaultListModel();
JList list = new JList(model);
add(new JScrollPane(list));
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton btn1 = new JButton("Add 1");
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Factory(TestPane.this, "Added by Button 1");
}
});
buttons.add(btn1);
JButton btn2 = new JButton("Add 2");
btn2.addActionListener(this);
buttons.add(btn2);
add(buttons, BorderLayout.SOUTH);
}
public void addItem(String text) {
model.addElement(text);
}
#Override
public void actionPerformed(ActionEvent e) {
new Factory(TestPane.this, "Added by Button 2");
}
}
public class Factory {
public Factory(TestPane testPane, String text) {
testPane.addItem(text);
}
}
}

Categories

Resources