Unable to get column name in JTable in Swing - java

JTable table = new JTable(data,columnNames);
JScrollPane pane = new JScrollPane(table);
this.add(pane);
this.add(table);
My data is showing but column name is not showing on top.

A component can only have a single parent.
JScrollPane pane = new JScrollPane(table);
this.add(pane);
this.add(table);
First you add the table to the viewport of the scrollpane, which is good as this will cause the table header to automatically be displayed when the GUI is made visible.
But then you add the table directly to the frame, which is bad because it can no longer be displayed in the scrollpane.
Get rid of:
//this.add(table);
and then the scrollpane containing the table will be displayed properly on the frame.

Have a look at this example
import java.awt.Color;
import javax.swing.*;
public class table extends JFrame{
public table() {
setSize(600, 300);
String[] columnNames = {"A", "B", "C"};
Object[][] data = {
{"Moni", "adsad", 2},
{"Jhon", "ewrewr", 4},
{"Max", "zxczxc", 6}
};
JTable table = new JTable(data, columnNames);
JScrollPane tableSP = new JScrollPane(table);
JPanel tablePanel = new JPanel();
tablePanel.add(tableSP);
tablePanel.setBackground(Color.red);
add(tablePanel);
setTitle("Marks");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
table ex = new table();
ex.setVisible(true);
}
});
}
}

Related

populate DefaultTableModel after insert a row java

I am running a program with two frames. First one has a table, the second one has a form which allows adding a new user to the table. I think the problem is I didn't add a reference from the mainframe. I was trying different methods to refresh the mainframe programmatically, but it did not help so much. I read many articles on how to to it but I could find a solution. My table usually changes when I close my app and open it again. But I don't think is the right way to do it. I tried to delete elements from DefaultTableModel and populated jtable again, but did not get any results. Here is my code:
public Vector vector_jtable = new Vector();
public MainApp() {
initComponents();
Database b = new Database();
b.getAmountOfRows(getCount);
this.setLocationRelativeTo(null);
printResultDB();
}
//add function that is responsible for addding data to the table
public void postDataJtable() {
System.out.println("The vector is: " + vector_jtable);
Vector<String> header = new Vector<String>();
header.add("Number");
header.add("Name");
header.add("First Payment");
header.add("Next Payment");
header.add("Picture");
header.add("Phone");
header.add("Amount");
header.add("Age");
model = (DefaultTableModel)jTable2.getModel();
model.setDataVector(vector_jtable,header);
}
I created a vector that allows putting data from the second frame.
MainApp app;
public AddStudents(MainApp a) {
initComponents();
app = a;
this.setLocationRelativeTo(null);
jDateChooser1.setDateFormatString("yyyy-MM-dd");
jDateChooser2.setDateFormatString("yyyy-MM-dd");
}
After that, I push the button to send it out and update the mainframe, but nothing happened:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
app.vector_jtable.add(name);
app.vector_jtable.add(first_p);
app.vector_jtable.add(next_p);
app.vector_jtable.add(picture);
app.vector_jtable.add(phone);
app.vector_jtable.add(amount);
app.vector_jtable.add(age);
app.postDataJtable();
My question. How to add a row in jtable and refresh it. I really stuck in this topic. I need your help.
Don't update the Vector.
When you want to change the data in the table you need to change the data in the TableModel.
You can use the addRow(...) method of the DefaultTableModel to add a new row of data.
So the basic logic is:
Vector<Object> row = new Vector<Object>();
row.add( someVariable1 );
row.add( someVariable2 );
...
modal.addRow( row ):
The model will then tell the table to repaint itself.
Edit:
There is no trick all you need is a reference to the model. Then you update the model.
Here is a simple example to prove the concept works:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class SSCCE extends JPanel
{
private DefaultTableModel model;
SSCCE()
{
setLayout( new BorderLayout() );
model = new DefaultTableModel(0, 2);
JTable table = new JTable( model );
add(new JScrollPane( table ));
JButton button = new JButton( "Add Row" );
add(button, BorderLayout.PAGE_END);
button.addActionListener( new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Vector<Object> row = new Vector<Object>();
row.add( "" + model.getRowCount() );
row.add( new Date().toString() );
model.addRow( row );
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
If it doesn't work for you then you need to debug your code. Maybe you have two "model" variables? Maybe you have to "table" variables. Maybe your code isn't even executed. Did you add any debug statements to the code to make sure it is executed.
We can't solve your problem only point you in the right direction.
You can try some aspects from this example below. The example has two JFrame's - one with a JTable and the other the data entry fields. When the data is entered and the "UpdateTable" button is pressed (in the data entry class) the table is updated.
The example uses java.util.Observer and Observable to achieve this functionality.
The class with table:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Observer;
import java.util.Observable;
public class TableUpdateTester implements Observer {
private JTable table;
private static final Object[] TABLE_COLUMNS = {"Book", "Author"};
private static final Object [][] TABLE_DATA = {
{"Book 1", "author 1"}, {"Book 2", "author 1"}
};
public static void main(String [] args) {
TableUpdateTester tester = new TableUpdateTester();
new DataEntryClass(tester);
}
public TableUpdateTester() {
JFrame frame = new JFrame("Table Update Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getTablePanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel getTablePanel() {
table = new JTable(new DefaultTableModel(TABLE_DATA, TABLE_COLUMNS));
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(400, 150));
scrollpane.setViewportView(table);
JPanel panel = new JPanel();
panel.add(scrollpane);
return panel;
}
// This is Observer's override method.
#Override public void update(Observable o, Object arg) {
String [] data = (String []) arg;
System.out.println("Data recieving: " + java.util.Arrays.toString(data));
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(data);
}
}
The data entry class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
public class DataEntryClass {
public DataEntryClass(TableUpdateTester observer) {
final DataObservable observable = new DataObservable();
observable.addObserver(observer);
JLabel label = new JLabel("Book: ");
final JTextField text = new JTextField(15);
JLabel label2 = new JLabel("Author: ");
final JTextField text2 = new JTextField(15);
JButton button = new JButton("Update Table");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data1 = text.getText().isEmpty() ? "empty" : text.getText();
String data2 = text2.getText().isEmpty() ? "empty" : text2.getText();
String [] data = {data1, data2};
System.out.println("Data sent: " + java.util.Arrays.toString(data));
observable.changeData(data);
}
});
JPanel panel = new JPanel();
GridLayout grid = new GridLayout(3, 2);
panel.setLayout(grid);
panel.add(label);
panel.add(text);
panel.add(label2);
panel.add(text2);
panel.add(new JLabel(""));
panel.add(button);
JFrame frame = new JFrame();
frame.setTitle("Data Entry");
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
}
}
class DataObservable extends Observable {
DataObservable() {
super();
}
void changeData(Object data) {
// the two methods of Observable class
setChanged();
notifyObservers(data);
}
}
Finally, I found a solution to my problem. I will post my code here.
The Mainframe. I know the app with two frames is not a good option, because it's hard to fix the problem and it usually takes a lot of time to debug it.
/**
* Create the application.
*/
public MainApp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 689, 345);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//add table in DefaultTableModel
model = new DefaultTableModel(0,2);
table = new JTable(model);
table.setBounds(58, 38, 524, 197);
frame.getContentPane().add(table);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 4, 4);
frame.getContentPane().add(scrollPane);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Call a second frame
//add reference for DefaultTableModel and send it to another frame
AddData frame = new AddData(model);
frame.setVisible(true);
}
});
btnNewButton.setBounds(239, 269, 117, 29);
frame.getContentPane().add(btnNewButton);
}
The second frame, that is responsible for adding a new row in a table.
/**
* Create the frame.
*/
public AddData(DefaultTableModel model) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(115, 71, 188, 41);
contentPane.add(dateChooser);
JButton btnNewButton = new JButton("Send");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//MainApp app = new MainApp();
Vector<Object> row = new Vector<Object>();
row.add(""+model.getRowCount());
row.add(dateChooser.getDate().toString());
model.addRow(row);
}
});
btnNewButton.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
btnNewButton.setBounds(165, 192, 117, 41);
contentPane.add(btnNewButton);
}
I found my mistake. I did not send a link of DefaultTableModel into the second frame. That's why it was null every time. It was a really painful experience, but I learned from my mistakes. Thanks, everyone for your help. I really appriciate.

JTable width layout

I'm creating a GUI interface to interact with the database of a warehouse. The application needs to add items to the database, update them and show them. I have some tables in the database and for each table, I want to create a JPanel, and put them in a cardlayout, so I can navigate between them with JMenu items. Each JPanel has the same form. In the top, there is a box with textfields, comboboxes etc. to add an item in the table. Under the box, I have a JTable with 1 row and under that, I have a JTable in a JScrollPane to show the content of the table. Each column needs to have a width of 150, except the last one (width=100, it will contain a JButton for modifications). I use the first JTable as a filter (for example if the first column contains '1', then the second JTable will only show items with an ID starting by '1'). I don't know how to choose correct layouts for different JPanels. For the moment, each JPanel has a BorderLayout and each component is placed in the center. But the problem is that I can't choose the width of each column.
Well, finally I've solved my own problem. Actually, I need to put the JTable in a JPanel, and the JPanel in a JScrollPane. Here is the code :
public class Brouillon extends JFrame {
public Brouillon() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpContent = new JPanel();
JPanel jpCards = new JPanel();
jpContent.setLayout(new BoxLayout(jpContent, BoxLayout.PAGE_AXIS));
CardLayout clSelect = new CardLayout();
jpCards.setLayout(clSelect);
JButton jbTest = new JButton();
jbTest.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
clSelect.next(jpCards);
}
});
jpContent.add(jbTest);
JPanel jpContainer1 = new JPanel();
JTable jtData1 = new JTable(new Object[2][3], new String[] {"1", "2", "3"});
jtData1.getColumnModel().getColumn(0).setPreferredWidth(150);
jtData1.getColumnModel().getColumn(1).setPreferredWidth(150);
jtData1.getColumnModel().getColumn(2).setPreferredWidth(50);
JPanel jpTemp1 = new JPanel();
jpTemp1.setLayout(new BorderLayout());
jpTemp1.add(jtData1.getTableHeader(), BorderLayout.NORTH);
jpTemp1.add(jtData1, BorderLayout.CENTER);
JScrollPane jspData1 = new JScrollPane(jpTemp1);
jpContainer1.add(jspData1);
JPanel jpContainer2 = new JPanel();
JTable jtData2 = new JTable(new Object[2][7], new String[] {"1", "2", "3", "4", "5", "6", "7"});
jtData2.getColumnModel().getColumn(0).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(1).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(2).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(3).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(4).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(5).setPreferredWidth(150);
jtData2.getColumnModel().getColumn(6).setPreferredWidth(50);
JPanel jpTemp2 = new JPanel();
jpTemp2.setLayout(new BorderLayout());
jpTemp2.add(jtData2, BorderLayout.CENTER);
jpTemp2.add(jtData2.getTableHeader(), BorderLayout.NORTH);
JScrollPane jspData2 = new JScrollPane(jpTemp2);
jpContainer2.add(jspData2);
jpCards.add(jpContainer2);
jpCards.add(jpContainer1);
jpContent.add(jpCards);
this.getContentPane().add(jpContent);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new Brouillon();
}
}
For jTable, you can set the column size.
Possibly the below example may help. Please let me know if this is what you are looking for!
TableColumn column = null;
for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 2) {
column.setPreferredWidth(100); //sport column is bigger
} else {
column.setPreferredWidth(50);
}
}

JTables ,How to insert data in Jtable from a text file

Hey I don't have enough knowledge on JTables, How to insert data in JTables by the way I entered the data in my table but I don't know why the column names not appear in my table.
Here's my code:
public class tab {
public tab() {
initComponents();
}
public void initComponents() {
JFrame fr = new JFrame("Score Card");
JScrollPane pane = new JScrollPane();
fr.setSize(500, 350);
JTable scoreTab = new JTable(5, 4);
DefaultTableModel model = new DefaultTableModel(1, 4);
// model.setColumnIdentifiers(new Object[]{"No","Name","Score","Date"});
scoreTab.setModel(new javax.swing.table.DefaultTableModel(
new Object[][] { { 1, 2, 2 },
}, new String[] { "Name", "Score", "Date" }));
pane.setViewportView(scoreTab);
fr.add(scoreTab);
fr.setLocationRelativeTo(null);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
}
public static void main(String[] args) {
new tab();
}
}
add JScrollPane to the jframe
fr.add(pane);
you are directly adding jtable to the frame fr.add(scoreTab);
if you add a table without jscrollpane you have to add headers separately.

JTable, JComboBox - problems in showing JComboBox in second column

I have written this simple program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class JcomboboxJtableDemo extends JPanel
implements ActionListener {
private DefaultTableModel tableModel;
JTable table = new JTable (tableModel);
private JScrollPane scrollpaneTable = new JScrollPane( table );
private JPanel PaneBottoniTabella = new JPanel( );
public JcomboboxJtableDemo() {
super(new BorderLayout());
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox comboBox = new JComboBox(petStrings);
comboBox.setSelectedIndex(4);
tableModel = CreateTableModel();
tableModel.insertRow( 0, new Object[] {"Header col1", ""} );
tableModel.insertRow( 0, new Object[] {petStrings[0], ""} );
tableModel.insertRow( 0, new Object[] {petStrings[1], ""} );
tableModel.insertRow( 0, new Object[] {petStrings[2], ""} );
tableModel.insertRow( 0, new Object[] {petStrings[3], ""} );
tableModel.setValueAt("Header col2", 0, 1);
DefaultCellEditor editor = new DefaultCellEditor(comboBox);
table.getColumnModel().getColumn(0).setCellEditor(editor);
table.getColumnModel().getColumn(1).setCellEditor(editor);
//Lay out the demo.
add(comboBox, BorderLayout.PAGE_START);
add(table, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
private final DefaultTableModel CreateTableModel () {
DefaultTableModel modello = new DefaultTableModel( new Object[] { "Col1","Col2" }, 0 ) {
#Override
public boolean isCellEditable(int row, int column) {
return true;
}
};
table.setModel(modello);
return modello;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new JcomboboxJtableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I you try to run it you will see that there is a problem in showing correctly the JComboBox components in the second column, in the first column the are correctly shown and you can see each selected item as set in the code, instead in the second column there are some problems: none on the relative cell.
Could you tell me why? How can I solve the problem?
Thanks
You're using the same JComboBox component for both ColumnModel columns which in turn share the same ComboBoxModel. Any change in the selected item from one column will be reflected in the other column. Create a second combobox
JComboBox comboBox2 = new JComboBox(petStrings);
...
table.getColumnModel().getColumn(1).setCellEditor(editor2);
so that any changes can occur independently in either column.

Adding a component to jPanel java

i'm trying to add a jtable component to my jPanel but i am unable to see it. What am i doing wrong?.
table gui = new table(data,colum);
mainPanel.add(gui.table);
class table extends JFrame
{
public JTable table;
public table(Vector data, Vector colum)
{
setLayout(new FlowLayout());
table = new JTable(data,colum);
table.setPreferredScrollableViewportSize(new Dimension(900,10));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
}
Extending JFrame seems odd; you don't use any of the top level container capabilities. Here's an example that extends JPanel, with a main() that drops the panel into a JFrame.
--Edited to accept an existing JPanel
public class TablePanel
{
public static void addTableToPanel(JPanel jPanel, Vector rowData, Vector columnNames)
{
JTable jTable = new JTable(rowData, columnNames);
jTable.setFillsViewportHeight(true);
JScrollPane jScrollPane = new JScrollPane(jTable);
jScrollPane.setPreferredSize(new Dimension(300, 50));
jPanel.add(jScrollPane);
}
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
Vector cols = new Vector();
Vector rows = new Vector();
Vector row1 = new Vector();
cols.add("A");
cols.add("B");
cols.add("C");
row1.add("1");
row1.add("2");
row1.add("3");
rows.add(row1);
rows.add(row1.clone());
rows.add(row1.clone());
rows.add(row1.clone());
JFrame frame = new JFrame("TableTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout(0, 0));
TablePanel.addTableToPanel(jPanel, rows, cols);
frame.getContentPane().add(jPanel);
frame.pack();
frame.setVisible(true);
}
});
}
}

Categories

Resources