I use a JTable inside a JScrollPane with a custom TableModel, cell-/column-selection disabled and only row-selection enabled (single selection). If I select a row, from time to time, the value in the cell on which I perform the click, appears in the neighbour columns too (and overwrites the values in there).
Could anybody give me a hint, what I'm doing wrong or has anybody else faced this problem?
Thanks for your help in advance!
EDIT: I added a SSCCE. After a few selections, the issue should occur. You can accelerate the occurence of it by keeping the mouse pressed while hovering over the rows. If it occured once, it occurs during every selection.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class TableIssueSSCCE {
public TableIssueSSCCE() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JTable table = new JTable(new DefaultTableModel());
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addColumn("Test1");
model.addColumn("Test2");
model.addColumn("Test3");
model.addColumn("Test4");
for (int i = 0; i < 1000; i++) {
model.addRow(new String[]{
"Column1" + i, "Column2" + i, "Column3" + i, "Column4" + i});
}
table.setFillsViewportHeight(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.setAutoCreateRowSorter(true);
JScrollPane tableContainer = new JScrollPane(table);
tableContainer.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL));
tableContainer.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
frame.add(tableContainer, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
new TableIssueSSCCE();
}
}
I was able to avoid the problem by changing to Oracle's JRE. So this issue seems to be related to OpenJDK.
Some observations:
Whenever a Swing program misbehaves intermittently, I start looking for incorrect synchronization, sometimes resorting to the approach shown here. See also Initial Threads. Because such errors may be difficult to reproduce, it's better to be safe:
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new TableIssueSSCCE();
});
}
Instead of setSize(), override getPreferredScrollableViewportSize(), as suggested here.
Related
I am new to Java and need to make a blank table with column header only, but I am stuck at the column header, it won't appear, I have tried the answer in this link JTable won't show column headers (adding JScrollPane) but it won't work. This is my code:
void panelTabel(){
JPanel panelTabel = new JPanel();
panelTabel.setBackground(Color.white);
panelTabel.setLayout(null);
panelTabel.setBounds(0, 260, 1000, 455);
JScrollPane scrollTabel = new JScrollPane();
scrollTabel.setBackground(Color.white);
scrollTabel.setLayout(null);
scrollTabel.setBounds(5,5,990,340);
Vector headerTabel = new Vector(2);
headerTabel.addElement(new String("No."));
headerTabel.addElement(new String("Kode Barang"));
DefaultTableModel modelTabel = new DefaultTableModel(1, headerTabel.size());
modelTabel.setColumnIdentifiers(headerTabel);
JTable tabelBarang = new JTable();
tabelBarang.setModel(modelTabel);
tabelBarang.setBackground(Color.gray);
tabelBarang.setBounds(5,5, 980, 330);
scrollTabel.add(tabelBarang);
panelTabel.add(scrollTabel);
halaman.add(panelTabel);
}
And this is the output :
Blank table with no column header
I know my question may be duplicate, but I am really new to java and don't know what I did wrong, can someone please tell me what am I missing ? Thank you so much.
Here's a simple JTable GUI I created using your method.
Here are the changes I made.
I used a border layout on the JPanel that holds the JTable.
I got rid of all null layouts and positioning statements. I did ask for a preferred size for the JPanel. After you actually add some data to the JTable, you can define the size of the JTable and remove the preferred size hint.
I defined the JTable first, then the JScrollPane. Thanks to Andrew Thompson for his comment.
Here's the minimal, runnable example #38,593,729 of a JTable in a JPanel in a JFrame. I hope this example helps you, unlike the first 38,593,728 examples on the Internet.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JTableSimpleExample implements Runnable {
private JFrame frame;
#Override
public void run() {
frame = new JFrame("JTable Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panelTabel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel panelTabel() {
JPanel panelTabel = new JPanel();
panelTabel.setLayout(new BorderLayout());
panelTabel.setPreferredSize(new Dimension(400, 100));
Vector<String> headerTabel = new Vector<>(2);
headerTabel.addElement(new String("No."));
headerTabel.addElement(new String("Kode Barang"));
DefaultTableModel modelTabel = new DefaultTableModel(1, headerTabel.size());
modelTabel.setColumnIdentifiers(headerTabel);
JTable tabelBarang = new JTable(modelTabel);
JScrollPane scrollTabel = new JScrollPane(tabelBarang);
panelTabel.add(scrollTabel, BorderLayout.CENTER);
return panelTabel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTableSimpleExample());
}
}
I am using JComboBox in Jtable cell. When I click on the JComboBox and select a value from it, it calls the ActionPerformed fuction. Till here it is working fine but as soon as I click on the JComboBox again, it calls the ActionPerformed function, which it should not. What I want is, to call the ActionPerformed function when the item is selected in the JComboBox. In other words it should work as it worked for the first time when the item was selected from the JComboBox and then the ActionPerformed function was called. I cannot figure out why this problem is occurring. Here are the links that I have looked into and I did some other searches also but still could not find any relative answer to the above mentioned problem.
Adding JComboBox to a JTable cell
How to use ActionListener on a ComboBox to give a variable a value
https://coderanch.com/t/339842/java/ComboBox-ItemListener-calling
Here is the code, you can copy paste it and check it.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class TableExample implements ActionListener{
JFrame frame;
JComboBox skuNameComboBoxTable;
TableExample() {
frame = new JFrame();
String data[][] = {{"101", "Amit", "Choose"},
{"102", "Jai", "Choose"},
{"101", "Sachin", "Choose"}};
String column[] = {"ID", "Name", "Degree"};
JTable table = new JTable(data, column);
table.setBounds(30, 40, 200, 300);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);
frame.setSize(300, 400);
frame.setVisible(true);
String[] array = {"BS(SE)", "BS(CS)", "BS(IT)"};
skuNameComboBoxTable = new JComboBox(array);
skuNameComboBoxTable.addActionListener(this);
TableColumn col = table.getColumnModel().getColumn(2);
col.setCellEditor(new DefaultCellEditor(skuNameComboBoxTable));
}
public static void main(String[] args) {
new TableExample();
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "actionPerformed called");
}
}
Kindly tell me why this problem is occurring and how should I solve it.
Unfortunately, you can't do much when using the DefaultCellEditor - that is how it behaves. Within your code you can add a check to ensure that a change in value occured before handling the event. Something like below:
public void actionPerformed(ActionEvent e) {
if (skuNameSelected == null || skuNameComboBoxTable.getSelectedItem() != skuNameSelected)
JOptionPane.showMessageDialog(null, "actionPerformed called: ");
skuNameSelected = (String) skuNameComboBoxTable.getSelectedItem();
}
You can try using ItemListener and filter your action according to the ItemEvent.
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class JComboBoxTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] items = {"One", "Two", "Three"};
JComboBox cb = new JComboBox(items);
cb.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Selected " + e.getItem());
} else {
System.out.println("Deselected " + e.getItem());
}
}
});
frame.add(cb);
frame.pack();
frame.setVisible(true);
}
}
This is happening because you are using the same JComboBox as the DefaultCellEditor for column 2.
Whenever you click a cell from column 2 the ComboBox will change to the value that is on the cell at the moment and that triggers the DESELECT (from the old value) and the SELECT (for the new value). This will only happen if the old value and the new value are not the same.
One way to avoid this is to add a CellEditorListener on the DefaultCellEditor that you are using, like below:
TableColumn col = table.getColumnModel().getColumn(2);
DefaultCellEditor cellEditor = new DefaultCellEditor(skuNameComboBoxTable);
col.setCellEditor(cellEditor);
cellEditor.addCellEditorListener(new CellEditorListener() {
#Override
public void editingStopped(ChangeEvent e) {
System.out.println("Value of combo box defined!");
}
#Override
public void editingCanceled(ChangeEvent e) {
System.out.println("Edition canceled, set the old value");
}
});
This way you will be able to only act when the value has been defined by the ComboBox.
I hope this helps.
You should not be using an ActionListener for this. The combo box uses its own ActionListener to update the TableModel when an item is selected.
So instead you should be listening for changes in the TableModel in order to do your custom code. So you should be using a TableModelListener to listen for changes in the data. However, a TableModelListener can fire an event even if just start and stop editing of the cell which might be a problem for you.
In this case you can use the Table Cell Listener. It will only generate an event when the value in the TableModel has changed.
Trying to list the nodes in a JList so I can choose one from the list.
I have this code (and a lot more)...
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel row1 = new JPanel();
JPanel row2 = new JPanel();
JLabel flabel = new JLabel("Förbindelse från " + n1 +" till " + n2, JLabel.CENTER);
JScrollPane scroll = new JScrollPane(JLista);
DefaultListModel<Edge<Node>> Jlistan = new DefaultListModel<Edge<Node>>();
List<Edge<Node>> listan = listGraph.getEdgesBetween(n1,n2);
for (Edge<Node> listEdge : listan) {
Jlistan.addElement(listEdge);
}
JLista = new JList<Edge<Node>> (Jlistan);
JLista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JLista.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JLista.setVisibleRowCount(5);
JLista.setSize(100, 100);
getJlistVal();
row2.add(scroll);
row1.add(flabel);
add(row1);
add(row2);
}
...
public Edge<Node> getJlistVal(){
return JLista.getSelectedValue();
}
But when listing, i just get a little spot on the Jpanel, i think its a graphical dot, or a very very litte Jlist. :( cant publish a picture yet...
I thought it had to do with pixelsize but dont think so??
Do i have to specify size of the list??
The list to be displayed is a generic LIST as type Node. I have a method that i call, is the return type ok?
//thank you
-Help me StackOverflow. You are my only hope...
You create scroll instance with null list.
JScrollPane scroll = new JScrollPane(JLista);
Just move the line after
JLista = new JList<Edge<Node>> (Jlistan);
Not sure what you're doing wrong because your example won't compile without your other classes, But When I use the above code, adding in a bit of my own, it works. The only difference is I used a Box instead of BoxLayout, which is pretty much the same thing, just a Box uses a JPanel under the hood. I had to do this because it wasn't allowing be to use this from the JFrame subclass. You can test it
import javax.swing.Box;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
/**
*
* #author Paul SamSotha
*/
public class TestList extends JFrame {
public TestList() {
String[] list = {"Hello", "Jello", "Wello"};
JList JLista = new JList(list);
JPanel row1 = new JPanel();
JPanel row2 = new JPanel();
JLabel flabel = new JLabel("Förbindelse från ", JLabel.CENTER);
JScrollPane scroll = new JScrollPane(JLista);
JLista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JLista.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JLista.setVisibleRowCount(5);
JLista.setSize(100, 100);
row2.add(scroll);
row1.add(flabel);
Box box = Box.createVerticalBox();
box.add(row1);
box.add(row2);
add(box);
}
public static void createAndShowGui() {
JFrame frame = new TestList();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
"Do i have to specify size of the list??"
No, you can just call .pack() on the frame and let the pack do its magic
What you could do is, get the value in the getJlistVal() and return the value as a String.
String selected = jList1.getSelectedItem().toString();
and try to return that value. Tell me if it prints. I can't test it myself right now.
Just a suggestion to try, let me know how it goes.
Currently what your method getJListVal() is doing is, returning the value as an Element. Which may be your problem. Try returning it as a String, int or generic variable type, then try displaying it.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SideNotes {
public static JPanel panel = new JPanel();
private List<String> notes = new ArrayList<String>();
private static JButton add = new JButton("Add note");
public SideNotes() {
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(add);
loadNotes();
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNote();
}
});
}
public void addNote() {
String note = JOptionPane.showInputDialog("Enter note: ", null);
notes.add(note);
JLabel label = new JLabel(note);
panel.add(label);
panel.revalidate();
panel.repaint();
}
private void loadNotes() {
for (int i = 0; i < notes.size(); i++) {
JCheckBox jcb = new JCheckBox(notes.get(i), false);
panel.add(jcb);
panel.revalidate();
panel.repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200, 400);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(add);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new SideNotes();
}
}
Why isn't my JCheckBox showing up? The text shows up but not the actual box. What's the deal?
I have edited my post to contain all of my code in case that helps solve the issue.
needmoretextneedmoretextneedmoretextneedmoretextneedmoretextneedmoretextneedmoretext
Possible reasons:
panel has not been added to GUI
panel has been added but for some reason is not visible.
panel is too small to show the child component. This can happen for instance if you set a component's size or preferredSize or if you place it in a FlowLayout-using container without thought.
panel uses null layout.
panel's layout manager is not one that easily accepts a new component -- think GroupLayout for this one.
There are other unspecified layout manager problems going on. Do you call pack() on your GUI? Do you use null layout or absolute positioning anywhere? Do you need to put panel in a JScrollPane?
Consider creating and posting an sscce for better help.
Edit
Your posted code doesn't ever add any JCheckBoxes to the JPanel, just JLabels. To prove this is so, click on the labels and you'll see that they don't respond to clicks.
Your code grossly over-uses static fields. Get rid of all static modifiers on all variables. They should all be instance variables. The only static anything in your code above should be the main method, and that's it. If this causes errors, then fix the errors, but not by making fields static.
Give your SideNotes class a method, getPanel() that returns the panel field.
Create a SideNotes instance in the beginning of your main method. Then call the above method on the instance to get the JPanel for the JFrame. i.e., frame.add(sideNotes.getPanel());.
Don't add JLabels to your GUI (I've no idea why you're doing this). Add JCheckBoxes in the actionPerformed method.
Every time you press the button, a new Note (JLabel) is added to the panel. But you never call loadNotes() after adding a new Note. So the JLabel is added but not its respective JCheckBox as intended.
Besides of this I'd suggest you make this change:
public void addNote() {
String note = JOptionPane.showInputDialog("Enter note: ", null);
if(notes != null) {
notes.add(note);
JLabel label = new JLabel(note);
panel.add(label);
panel.add(new JCheckBox(note, false));
panel.revalidate();
panel.repaint();
}
}
So you don't need to call loadNotes() and update the GUI just once.
I have a JTable with a list of information. I highlighted some of the rows using:
myTable.addRowSelectionInterval(number,number);
myTable.setSelectionBackground(Color.yellow);
number is just the number of the row to be highlighted.
My program reads from a file which contains a list of numbers and then highlights the according rows. This is so when I close the program and open it back up, the highlighted rows will remain highlighted.
The problem is that I can't seem to update my JTable if I use
addRowSelectionInterval(number,number);
Is there any way to "refresh" a JTable?
I've seen some code used to refresh the values on a table but that doesn't help.
I don't know if it's because I'm just highlighting the rows.
I'm using the default JTable.
I don't know anything about "models". This is my first time making a GUI.
Can anyone please tell me how to do this? I would gladly appreciate any help.
Thank you.
The sscce below produces the expected result. As #P. Lalonde comments, verify that you are updating the selection on the event dispatch thread. Updating the TableModel will update the view automatically, but the current selection is a view property.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/** #see http://stackoverflow.com/a/16765450/230513 */
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(
new String[][]{{"One"},{"Two"}, {"Three"}},
new String[]{"Ordinal"});
table.addRowSelectionInterval(1, 1);
f.add(new JScrollPane(table));
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();
}
});
}
}