Can someone tell me, where is my mistake?
I'm trying to fill up table with data but I can't, always get empty table.
Here is my code with frame:
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Rinktis extends JFrame {
private final JScrollPane scrollPane = new JScrollPane();
private JTable table;
public Rinktis()
{
super();
setBounds(100, 100, 861, 375);
try {
jbInit();
} catch (Throwable e) {
e.printStackTrace();
}
}
Vector<Vector<String>> data1 =new Vector<Vector<String>>();
Vector<String> heading1 = new Vector<String>();
public void tab(Vector heading, Vector data)
{ System.out.println(data);
System.out.println(heading);
data1=data;
heading1=heading;
System.out.println(data1);
System.out.println(heading1);
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
getContentPane().add(scrollPane);
scrollPane.setBounds(10, 10, 825, 176);
table = new JTable(data1,heading1);
scrollPane.setViewportView(table);
}
}
Here's how I call it from another frame:
protected void rinktisButton_1_actionPerformed(ActionEvent e)
{ Rinktis frame = new Rinktis();
frame.setVisible(true);
try {
db.getClients();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
and from db.getClients(); I call rinktis.tab(columnHeads,v);
System.out.println gives me all data correctly, but table is empty.
change your tab function to be like this:
public void tab(Vector heading, Vector data)
{
table.setModel(new DefaultTableModel(data,heading) );
}
For a serius answer you need to post an SSCCE. Anyway it seems that you are passing a wrong type to JTable constructor.
You need to pass a 2d array of object (Object [][]) or Vector<Vector<Object>> not plain 1D Vector, same as for TableHeader
I think here's the problem :
table = new JTable(data1, heading1);
//data1 need to of type Object[][] or Vector<Vector<Object>>
//heading1 need to of type String[] or Vector<String>
Here's a tutorial.
The problem is that the table does not know that you changed the data. You can either load your data on creation of the table. Then it should show up just fine (maybe try this out first). Then in a second step you should work directly with the tablemodel and change data there. Whenever you do so call the you can notify the table to update.
Alternatively, if you already know your header, you can set it and only add the data later. This also should work. However I do not recommend this.
Related
I am learning Java Swing. I am trying to develop as simple app for learning purpose. There is are multiple issues in following code. I try to read a csv file and populate JComboBox on button click.
public class MyForm {
private JButton btnRead;
private JButton btnRead2;
private JComboBox cbCodes;
private JPanel mainPanel;
private DefaultComboBoxModel comboBoxModel;
public MyForm(){
// issue 1: I always get null pointer exception in this line
comboBoxModel = new DefaultComboBoxModel();
cbCodes = new JComboBox(comboBoxModel);
btnRead.addActionListener( e -> {
List<String[]> data = readData();
comboBoxModel.removeAllElements();
data.forEach(item -> comboBoxModel.addElement(item));
});
// issue 2: Since DefaultComboBoxModel was not working. I tried without it. As this I get correct data in the array. But when I make JComboBox with array. Nothing is filled. It is empty.
btnRead2.addActionListener( e -> {
List<String[]> data = readData();
String[] array = new String[data.size()];
data.toArray(array);
cbCodes = new JComboBox(array);
});
}
// issue 3: I can't complie the code without this empty method. Why do I need it?
// error: Form contains components with Custom Create option but no createUIComponents() method
void createUIComponents(){
}
public List<String[]> readData() {
String file = "data.csv";
List<String[]> content = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = "";
while ((line = br.readLine()) != null) {
if(line.contains("\"")){
content.add(line.split(" "));
}
content.add(line.split(","));
}
} catch (FileNotFoundException e) {
//Some error logging
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
I make my question in the source code with the comment to show exactly here those issues are involved.
You don't get the NullPointerException in the lines you indicated, but in the line btnRead.addActionListener( e -> { because btnRead has not been initialized yet!
When you create a new JComboBox, you have to add it to the panel, too. Just creating it with new will not display it. But the real problem behind it is: you're using the model wrong. Write
comboBoxModel.removeAllElements();
for (final String string : array) {
comboBoxModel.addElement(string);
}
to solve that.
The problem you have here does not lie within the code you provided, but from another component. At some point, someone used a UI designer. Those designers usually create initialization methods, just like createUIComponents. See where that method gets called.
Synopsis:
All in all, your code is really chaotic. Restructure from new, this will clean up a lot of problems.
And initialize UI components as soon as possible, best do it in the declaration line: private final JButton btnRead = new JButton("Read!");
I strongly recommend using an IDE like Eclipse or IntelliJ that will help you write clean code and see and correct problems easier.
I try to update my JTable with the fireTableChanged() Method after i imported the names of spreadsheets from excel in my datastructure but the Method is not executed. I confirmed with a test that the data is correctly imported and that the jtable should have the necessary informationen.
What do i have to do that the JTable is correctly updated?
I found several other links to this topic but none of them worked for me:
Refresh Jtable
How to make JTable show refreshed data after updating database?
JTable How to refresh table model after insert delete or update the data.
AbstractDataTable fireTableDataChanged() does not refresh jtable
Can't refresh my JTable with new data
Model:
public class Model extends Observable {
String[][] data;
List<Arbeitsmappe> AMList = new LinkedList<>();
.....
public void setAMList(List<Arbeitsmappe> aMList) {
AMList = aMList; //new List replace the old
this.getData(); //The 2dimensional Array is filled with the names from the list
setChanged();
notifyObservers(Controller.Command_Excel_Eingelesen);
}
}
View:
JTextField cellEditorTF = new JTextField();
cellEditorTF.setEditable(false);
DefaultCellEditor cellEditor = new DefaultCellEditor(cellEditorTF);
ContentTable = new JTable(model.getData(), Model.columnNames);
//Cell Editable FALSE
ContentTable.getColumnModel().getColumn(0).setCellEditor(cellEditor);
//Single Interval Selection
ContentTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
//Cell Listener - When Cell is edited the new informationen is safed in AMLISt
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener tcl = (TableCellListener)e.getSource();
Model.AMList.get(tcl.getRow()).Speichername = String.valueOf(tcl.getNewValue());
// System.out.println("Row: " + tcl.getRow() + " " + Model.data[tcl.getRow()][1]);
}
};
TableCellListener tcl = new TableCellListener(ContentTable, action);
JScrollPane scrollPane = new JScrollPane(ContentTable);
ContentTable.setFillsViewportHeight(true);
ContentTable.getTableHeader().setReorderingAllowed(false);
this.add(BorderLayout.NORTH,ButtonPanel);
this.add(BorderLayout.SOUTH,scrollPane);
}
#Override
public void update(Observable arg0, Object arg1) {
if(arg0 instanceof Model){
Model model = (Model) arg0;
String cmd = (String) arg1;
if(cmd.equals(Controller.Command_Excel_Eingelesen)){
((AbstractTableModel)ContentTable.getModel()).fireTableDataChanged();
ContentTable.repaint();
this.repaint();
}
}
((AbstractTableModel)ContentTable.getModel()).fireTableDataChanged(); is called out of models definitions, it should't be, must be part of code, class void that override AbstractTableModel and its methods
as aside this method reseting all custom properties for model, and important part of methods for JTable (e.g. override for XxxTableCellRenderer/Editor)
read API in part methods for fireTableXxxXxx, there are notifiers for all JTable/AbstractTableModel's lifecycle, be sure that youare used the correct notifier for every actions/event
I'm a bit new to programming so sorry if there is a few things that could have been done better
My combobox is successfully filled with my string array and the auto-complete works fine. I just cant get the text in the combobox.
returns java.lang.NullPointerException
private ArrayList<String> arrRekening;
private ArrayList<String> arrEienaar;
private String[] sarrRekening;
private String[] sarrEienaar;
public NewConnectionPoint() {
arrAccount = new ArrayList<String>();
arrOwner = new ArrayList<String>();
FillCombo(arrAccount , "Owners", "OwnerName");
FillCombo(arrOwner , "Accounts", "AccountName");
sarrOwner= arrOwner.toArray(new String[arrOwner .size()]);
sarrAccount= arrAccount.toArray(new String[arrAccount.size()]);
JComboBox<String> comboAccount = new JComboBox<String>();
AutoCompleteSupport<String> supAccount = AutoCompleteSupport.install(comboRekening, GlazedLists.eventList(Arrays.asList(sarrAccount)));
supAccount.setStrict(true);
JComboBox<String> comboOwner = new JComboBox<String>();
AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,GlazedLists.eventList(Arrays.asList(sarrOwner)));
supOwner.setStrict(true);
JButton btnShow = new JButton("ShowSelectedr");
btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Error occurs at this line
JOptionPane.showMessageDialog(null, comboOwner.getSelectedItem().toString());
});}
}
//Data loaded into arraylists from a Database with sql
private void FillCombo(ArrayList<String> ComboElements, String sTable, String sColumn){
try{
Data.changeQuery(sTable);// database connection fine returns and fills combobox
while(MyData.rs.next()){
String sReturn= MyData.rs.getString(sColumn);
ComboElements.add(sReturn);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
The fundamental difficulty you're experiencing here is that you're trying to leverage the GlazedLists package without properly embracing it's core utility: EventLists.
You can easily side-step your difficulties if you use EventLists rather than ArrayLists.
If you really want to you can keep your FillCombo method returning an ArrayList (perhaps better name as getElements()) but straight-away initiate an EventList, use the GlazedLists EventComboBoxModel to link the EventList to the JComboBox and then you'll find your combobox getSelectedItem() should work fine.
The modified code to hook a list up to a combobox with autocomplete support will look something like this:
...
FillCombo(arrOwner , "Owners", "OwnerName");
EventList<String> ownerEventList = GlazedLists.eventList(arrOwner);
EventComboBoxModel<String> ownerModel = new EventComboBoxModel<String>(ownerEventList);
JComboBox comboOwner = new JComboBox(ownerModel);
AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,ownerEventList);
supOwner.setStrict(true);
...
I am refactoring my code. I have a JPanel:
public JPanel createLayout() {
log.info("create Layout");
JPanel panel = new JPanel();
panel.add(TestTable());
return panel;
}
I want to add TestTable to this panel. My TestTable looks like that:
public void TestTable() {
JLabel lFxRates = new JLabel("FXRates");
//get data
try {
stm = new SettingsTableModel(settingsService.getTestByParameter("DebtService"));
stm.setColumnNames(fxColumnNames);
} catch (Exception e) {
new HelperFunctions().showError("Error loading data!" + e);
}
fxTable = new JTable(stm);
fxTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
return null;
}
The problem I am facing is that nothing gets shown, because of return null. To which thing can I add my table to add it later to my JPanel?
I really appreciate your answer!
You have the problem that you are adding nothing, like null.
Everything you add to the panel must inherit from the parent- class called Component.
So, your Class TestTable, i hope it is a Class, must inherit from JTable or so.
instead of "return null", try adding "return fxTable".
In panel.add(TestTable()), TestTable() is expecting the component.
I read here , but if the xml file changes the jtree does not reload /refreshes
how to create a function for refresh / reload Jtree
I try to write code :
refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
XMLTree xmlClass = null;
((DefaultTreeModel) xmlClass.getModel()).reload();
System.out.println("Refresh");
}};
but i got the error : java.lang.NullPointerException
I added a new Action to popup in getJPopupForExplorerTree(). You'll probably want to re-factor xmlFile out of the XMLTree constructor; I've hard coded it for expedience below:
popup.add(new AbstractAction("Reload") {
public void actionPerformed(ActionEvent e) {
System.out.println("Reload");
try {
root = getRoot("xml.xml");
setModel(new XMLTreeModel(root));
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
});
this is most complex code, probably
read tutorial about JTables DefaultTableModel (good described concept and logics for DefaultXxxModel is similair / the same)
read tutorial about JTree
read tutorial about Concurency in Swing,
especially description about SwingWorker
in your case better (sorry for that) would be create an new instance for DefaultTreeModel, fills data by using SwingWorker, add new model to the visible JTree,
by replacing model you'll lost all changes in the current JTree
I dont know the spesific code but you can try this
refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
DefaultTreeModel myTreeModel = (DefaultTreeModel) xmlClass.getModel();
myTreeModel.reload();
revalidate();
repaint();
}};