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();
}
});
}
}
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.
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.
i have a gui in netbeans. 3 textfields animal id(i.e MAM001) , type of animal(LION) and nickname(SIMBA). when the three textfields are entered the user can click on the submit button which will store the values from the three textfields in a database.
what i want to do but don't know how to do it, is to use a combo box with three different fields in it (MAMMAL,BIRD REPTILE). when i select mammal from the combo box it will the change mysql statements in the background so that the data entered into the textfields will go into the mammal table in the database. if i click on the "bird" field in the combo box the same thing will happen except the data will go into the bird table in the database.
Any help would be greatly appreciated
Thank you.
I assume, that you want to react on an action event on your combo box. You can do this with the help of an ItemListener. For more details see the java api.
One approach could be the following:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class DemoFrame {
private JFrame frame;
private JComboBox comboBox;
private String[] items = {"IT1","IT2","IT3"};
public DemoFrame() {
frame = new JFrame("Demo Frame");
frame.setSize(300, 300);
comboBox = new JComboBox(items);
comboBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
//perform here your database querys for specific items.
if(e.getItem().equals(items[0]) && e.getStateChange() == ItemEvent.SELECTED) {
//db query for it "IT1"
}
}
});
frame.add(comboBox);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new DemoFrame();
}
});
}
}
I've noticed is that when an element is added to a JList and the element happens to fall within a range of values that are selected, it ends up being selected by default. In fact, if an element is added just above a selected value it is added to the selection.
I've tried looking at the JList code (in the open JDK6) and the BasicListUI code, but I'm having trouble teasing out the details of why this happens or what I need to do to fix it.
I'm considering supplying a custom SelectionModel, as that would be a sensible place to do some of the other work in my application, but I'm afraid that might make the problem worse, or harder to fix. Does anyone know why this happens? What I can do to fix it?
I've created this example that demonstrates the issue:
package jlistsscce;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class JListSSCCE extends JPanel
{
private final JList list;
private ScheduledExecutorService ses;
public JListSSCCE()
{
list = new JList();
list.setModel(new DefaultListModel());
ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new NewElement(), 100, 100, TimeUnit.MILLISECONDS);
add(list);
}
private static void createGui()
{
// create new JFrame
JFrame jf = new JFrame("JListSSCCE");
// this allows program to exit
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You add things to the contentPane in a JFrame
jf.getContentPane().add(new JListSSCCE());
// size frame
jf.pack();
// make frame visible
jf.setVisible(true);
}
public static void main(String[] args)
{
// threadsafe way to create a Swing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
createGui();
}
}
);
}
private class NewElement implements Runnable
{
int n = 0;
#Override
public void run()
{
((DefaultListModel)list.getModel()).add((int)Math.floor(Math.sqrt(n)), ("hey"+n));
n++;
}
}
}
This isn't the problem but I believe you should be using a Swing Timer instead of an Executor so the code get executed on the EDT.
Yes the problem is the selection model. The last time I looked at the code I found it rather confusing, so I'm not sure you want to play with it.
The only thing I can think of is to make sure you are using the multiple selection interval. Then after inserting an elemnt you check to see if the element is selected. If it is then you remove the selection for that element.