I am working on a the GUI of a piece of code that I have been patching together. I am stuck at this part of the program where I would like a datafile the user chooses to be displayed in a JTable in a preview manner (i.e. the user should not be able to edit the data on the table).
With a button click from Experiment Parameters tab (see screenshot below), I create and run a "PreviewAction" which creates a new tab, and fills it up with the necessary components. Below is the code for DataPreviewAction. EDIT: I also posted a self-contained, minimal version of this that mimics the conditions in the real project, and exhibits the same behaviour.
import java.awt.BorderLayout;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class MyFrame extends JFrame {
private JPanel panel1;
private JTabbedPane tabs;
private JButton runButton;
public MyFrame() {
tabs = new JTabbedPane();
panel1 = new JPanel();
runButton = new JButton("go!");
runButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
runButtonActionPerformed(evt);
}
});
panel1.add(runButton);
tabs.addTab("first tab", panel1);
this.add(tabs);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MyFrame frame = new MyFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
/*
* Normally there is more stuff happening here but this much will do for
* the sake of example
*/
List<String[]> data = new LinkedList<String[]>();
for (int i = 1; i < 1000; i++)
data.add(new String[] { "entry1", "value1", "value2", "value3" });
SwingUtilities.invokeLater(new DataPreviewAction(data, tabs));
}
public class DataPreviewAction implements Runnable {
private JTabbedPane contentHolder;
private List<String[]> data;
public DataPreviewAction(List<String[]> data, JTabbedPane comp) {
this.contentHolder = comp;
this.data = data;
}
#Override
public void run() {
DefaultTableModel previewModel = new DefaultTableModel() {
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
for (String[] datarow : data) {
previewModel.addRow(Arrays.copyOf(datarow, datarow.length,
Object[].class));
}
JTable table = new JTable(previewModel);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("A button"));
buttonPanel.add(new JLabel(
"Some description for the awesome table below "));
buttonPanel.add(new JButton("another button"));
JScrollPane tablePanel = new JScrollPane(table);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(buttonPanel, BorderLayout.NORTH);
container.add(tablePanel, BorderLayout.CENTER);
contentHolder.addTab("Preview", container);
contentHolder.validate();
contentHolder.repaint();
}
}
}
There are at least two problems here:
The JTable (or the JScrollPane) does not render at all
The JScrollPane is not as wide as the frame itself, I have no idea why
I am not all that good in Swing so I might be missing something fundamental. I have checked that the datafile is read properly, and the data model contains the right amount of rows (1000+). SO the table should not be empty.
Suggestions?
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("A button"));
buttonPanel.add(new JLabel("Some description for the awesome table below "));
buttonPanel.add(new JButton("another button"));
JScrollPane tablePanel = new JScrollPane(table);
JPanel container = new JPanel();
container.add(buttonPanel,BorderLayout.NORTH);
container.add(tablePanel,BorderLayout.SOUTH);
contentHolder.addTab("Preview", container);
//contentHolder.validate(); <- NO good
//contentHolder.repaint(); <- --"---
}
JPanel uses FlowLayout (implemented in API, acceptiong only PreferredSize, by default isn't resizable), correct output as is demonstrated in attn image, you have to change default LayoutManager for JPanel to BorderLayout, then code lines
.
container.add(buttonPanel,BorderLayout.NORTH);
container.add(tablePanel,BorderLayout.SOUTH);
will expands JComponents and can be works as you expecting, but I think tablePanel should be placed in CENTER area
EDIT:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MyFrame extends JFrame {
private JPanel panel1;
private JTabbedPane tabs;
private JButton runButton;
private JFrame frame = new JFrame();
private String[] columnNames = {"Nama", "Nim", "IP", "Hapus Baris ke"};
private Object[][] data = {
{"igor", "B01_125-358", "1.124.01.125", true},
{"lenka", "B21_002-242", "21.124.01.002", true},
{"peter", "B99_001-358", "99.124.01.001", false},
{"zuza", "B12_100-242", "12.124.01.100", true},
{"jozo", "BUS_011-358", "99.124.01.011", false},
{"nora", "B09_154-358", "9.124.01.154", false},
{"xantipa", "B01_001-358", "1.124.01.001", false},};
private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
switch (column) {
case 3:
return true;
default:
return false;
}
}
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public MyFrame() {
tabs = new JTabbedPane();
panel1 = new JPanel();
runButton = new JButton("go!");
runButton.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
//
}
});
panel1.add(runButton);
tabs.addTab("first tab", panel1);
JTable table = new JTable(model);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("A button"));
buttonPanel.add(new JLabel("Some description for the awesome table below "));
buttonPanel.add(new JButton("another button"));
JScrollPane tablePanel = new JScrollPane(table);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(buttonPanel, BorderLayout.NORTH);
container.add(tablePanel, BorderLayout.CENTER);
tabs.addTab("Preview", container);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tabs);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
}
});
}
}
EDIT 2nd. e.g.
from code (included your idea about to fill data to model)
import java.awt.BorderLayout;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MyFrame extends JFrame {
private JPanel panel1;
private JTabbedPane tabs;
private JButton runButton;
private JFrame frame = new JFrame();
private String[] columnNames = {"Nama", "Nim", "IP", "Hapus Baris ke"};
private Object[][] data = {
{"igor", "B01_125-358", "1.124.01.125", "true"},
{"lenka", "B21_002-242", "21.124.01.002", "true"},
{"peter", "B99_001-358", "99.124.01.001", "false"},
{"zuza", "B12_100-242", "12.124.01.100", "true"},
{"jozo", "BUS_011-358", "99.124.01.011", "false"},
{"nora", "B09_154-358", "9.124.01.154", "false"},
{"xantipa", "B01_001-358", "1.124.01.001", "false"},};
private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
switch (column) {
case 3:
return true;
default:
return false;
}
}
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
public MyFrame() {
tabs = new JTabbedPane();
panel1 = new JPanel();
runButton = new JButton("go!");
runButton.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
List<String[]> data = new LinkedList<String[]>();
for (int i = 1; i < 10; i++) {
data.add(new String[]{"entry1", "value1", "value2", "value3"});
}
for (String[] datarow : data) {
model.addRow(Arrays.copyOf(datarow, datarow.length, Object[].class));
}
}
});
panel1.add(runButton);
tabs.addTab("first tab", panel1);
JTable table = new JTable(model);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("A button"));
buttonPanel.add(new JLabel("Some description for the awesome table below "));
buttonPanel.add(new JButton("another button"));
JScrollPane tablePanel = new JScrollPane(table);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(buttonPanel, BorderLayout.NORTH);
container.add(tablePanel, BorderLayout.CENTER);
tabs.addTab("Preview", container);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tabs);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
}
});
}
}
Following the footsteps of mKorbel I ended up doing some debugging. I am providing it here in case others run into the same problem.
It felt quite odd that the table looked OK when the underlying DataModel was supplied a data matrix upon initialisation
private DefaultTableModel model = new DefaultTableModel(data, columnNames)
but it would not show up properly when created with the empty constructor
private DefaultTableModel model = new DefaultTableModel()
and adding rows later with model.addRow(Object[] row);
I started look through the source code, and it turns out with the empty constructor the number of rows and columns for the model (private fields) is initiated to 0 and not updated properly afterwards. I noticed this while debugging since my tables had the dimension of 1370 x 0, which of course does not display properly.
Since I do not want to hardcode the number of rows/cols in advance the best course of action was to convert my "rows" to a matrix and provide the data to the model via constructor (much like mKorbel did). Here comes the fun part, if you want to supply the data then you need to supply the column names as well. THe fact that you have to have column names is counter-intuitive (IMHO), what happens if you dont have/need headers? The data is already in a table form, so I dont understand why column names is so important.
At any rate the following code renders the table at least:
String[] colNames = new String[data[1].length];
for(int i=0; i<colNames.length; i++)
colNames[i] = "C" + i;
DefaultTableModel model = new DefaultTableModel(data,colNames){
#Override
public boolean isCellEditable(int row, int column){
return false;
}};
I am accepting this because it points to the origin of the problem, but I would not be able to pinpoint the problem without mKorbel's answer, so give the upvote to his/her answer :)
Related
I am trying to switch between different panels using JComboBox and CardLayout but any switching doesn't occur. Printing the ItemSelected was revealed that ItemListener is working correctly and also the accuracy of card layout was confirmed by examining other methods like next(), previous(), ...
I would really appreciate if anybody can help me out with this issue.
public class MyPanel exends JPanel {
public MyPanel() {
setBodyPanel();
}
private void setBodyPanel() {
card = new JPanel(new CardLayout());
cards.add(noBodyPanel);
cards.add(formPanel);
cards.add(jsonPanel);
cards.add(binaryFilePanel);
String comboBoxItems[] = {"No Body", "Form Data", "JSON", "Binary Data"};
JComboBox cbBodyType = new JComboBox(comboBoxItems);
cbBodyType.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
CardLayout cl = (CardLayout) cards.getLayout();
if (e.getStateChange() == ItemEvent.SELECTED) {
cl.show(cards,e.getItem().toString());
System.out.println(e.getItem().toString());
}
}
});
JPanel cbPanel = new JPanel();
cbPanel.add(cbBodyType);
add(cbPanel,BorderLayout.SOUTH);
add(cards,BorderLayout.CENTER);
}}
I deleted the unnecessary codes
You need to add the JPanel with the same name you use to retrieve it.
Here is an mre1 demonstrating it :
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
}
class MyPanel extends JPanel {
public MyPanel() {
setLayout(new BorderLayout(10,10));
setBodyPanel();
}
private void setBodyPanel() {
String comboBoxItems[] = {"No Body", "Form Data", "JSON", "Binary Data"};
JPanel cards = new JPanel(new CardLayout());
for(String name : comboBoxItems){
cards.add(name, getPanel(name));
}
JComboBox<String> cbBodyType = new JComboBox<>(comboBoxItems);
cbBodyType.addItemListener(e -> {
CardLayout cl = (CardLayout) cards.getLayout();
if (e.getStateChange() == ItemEvent.SELECTED) {
cl.show(cards,(String)e.getItem());
}
});
add(cards,BorderLayout.CENTER);
JPanel cbPanel = new JPanel();
cbPanel.add(cbBodyType);
add(cbPanel,BorderLayout.PAGE_END);
}
private Component getPanel(String name) {
JPanel p = new JPanel();
p.add(new JLabel(name +" panel"));
return p;
}
}
1 It is highly recommended to post mre or SSCCE when asking and answering
I add full code Here. Check the following code which I am using to create the table.
While executing the program, Values are displaying clearly, but the header is not displaying.
public class Table extends JFrame implements ActionListener {
JFrame f;
JPanel p1;
JPanel jPanel1;
JTable jTable1;
Table() {
f=new JFrame("Home123");
f.setSize(getMaximumSize());
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
p1=new JPanel();
p1.setBackground(Color.red);
p1.setVisible(true);
p1.setLayout(null);
f.add(p1);
String[] headers = {"First Name","Last Name","Age"};
Object[][] data = {
{"Kathy", "Smith",new Integer(25)},
};
jTable1 =new JTable(data, headers);
JScrollPane scrollPane = new JScrollPane(jTable1);
p1.add(jTable1);
p1.setBounds(200, 100, 500, 500);
jTable1.setBounds(70,250, 375,80);
}
public static void main(String[] args) {
new Table();
}
}
public class TableExample extends JFrame implements ActionListener {
JFrame f;
JPanel p1;
JPanel jPanel1;
JTable jTable1;
TableExample() {
JFrame f;
JPanel p1;
JPanel jPanel1;
JTable jTable1;
f = new JFrame("Home123");
f.setSize(getMaximumSize());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
p1.setBackground(Color.red);
p1.setLayout(new BorderLayout());
f.add(p1);
String[] headers = {"First Name", "Last Name", "Age"};
Object[][] data = {
{"Kathy", "Smith", new Integer(25)},};
jTable1 = new JTable(data, headers);
JScrollPane scrollPane = new JScrollPane(jTable1);
scrollPane.setSize(500,500);
jTable1.setSize(500,500);
p1.add(scrollPane);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new TableExample();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
try this its working and showing all thing that u need.
to #Sergiy Medvynskyy and #Krishna about 1. f.setVisible(true); 2. Initial Thread 3. p1.setVisible(true);
e.g.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Table /*extends JFrame implements ActionListener*/ {
private JFrame f;
private JPanel p1;
private JPanel jPanel1;
private JTable jTable1;
public Table() {
p1 = new JPanel();
p1.setBackground(Color.red);
p1.setLayout(new BorderLayout(5, 5));
String[] headers = {"First Name", "Last Name", "Age"};
Object[][] data = {
{"Kathy", "Smith", new Integer(25)},};
jTable1 = new JTable(data, headers);
JScrollPane scrollPane = new JScrollPane(jTable1);
jTable1.setPreferredScrollableViewportSize(jTable1.getPreferredSize());
p1.add(scrollPane);
f = new JFrame("Home123");
//f.setSize(getMaximumSize());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p1);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Table();
}
});
}
}
You're adding the table directly to the frame's panel, instead of adding the scroll pane wrapping the table, and displaying the headers.
Change
p1.add(jTable1);
to
p1.add(scrollPane);
And definitely stop using a null layout and setting bounds. Learn using layout managers.
You should add scroll pane to panel
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Table extends JFrame {
JFrame f;
JPanel p1;
JPanel jPanel1;
JTable jTable1;
Table() {
p1=new JPanel();
p1.setBackground(Color.red);
p1.setVisible(true);
p1.setLayout(new BorderLayout());
final String[] headers = {"First Name","Last Name","Age"};
final Object[][] data = {
{"Kathy", "Smith",new Integer(25)},
};
jTable1 =new JTable(data, headers);
final JScrollPane scrollPane = new JScrollPane(jTable1);
p1.add(scrollPane);
f=new JFrame("Home123");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p1);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new Table();
}
}
P.S. Never use null-layout.
I have looked and found that many people weren't putting their table into a scrollPane. Even though I nest my table into a scrollPane then into the frame it still fails to show the header. Is there something else I'm missing ? Thanks
public class Gui extends JFrame {
AbstractTableModel model;
JTable table;
public void start(AbstractTableModel model) {
this.model = model;
table=new JTable(model){
#Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = null;
for (int i = 0; i < model.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(120);
column.setMaxWidth(300);
column.setMinWidth(50);
}
JScrollPane pane = new JScrollPane(table);
pane.setPreferredSize(new Dimension(900,900));
add(pane);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
Based on your code and then having to add the missing functionality, you code works...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Gui extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Gui frame = new Gui();
frame.start(new DefaultTableModel(new Object[]{"A", "B", "C"}, 10));
}
});
}
AbstractTableModel model;
JTable table;
public void start(AbstractTableModel model) {
this.model = model;
table = new JTable(model) {
#Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = null;
for (int i = 0; i < model.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(120);
column.setMaxWidth(300);
column.setMinWidth(50);
}
JScrollPane pane = new JScrollPane(table);
pane.setPreferredSize(new Dimension(900, 900));
add(pane);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
}
This then leads to two broad assumptions...
You're TableModel doesn't have any columns
You're TableModel doesn't have any column names...
For example, a TableModel with no column names...
Personally...
I wouldn't use FlowLayout for this, BorderLayout will give better results
pack the frame before you make it visible
Set the layout before you add components as sometimes, things can get messed up...
For example...
setLayout(new FlowLayout()); // But I'd use a `BorderLayout`
add(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
I am making a GUI in which i tried to indent the column name.I am also pasting my source code. I am using the GridBagConstraints. This my code where i create a GridBagConstraints but nothing seems to happen
private final static Dimension VOLUME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension NAME_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension IMAGE_FIELD_DIMENSION = new Dimension(200, 25);
private final static Dimension DATE_FIELD_DIMENSION = new Dimension(150, 25);
private final static Dimension STATUS_FIELD_DIMENSION = new Dimension(100, 25);
private final static Dimension IMAGE_PANEL_DIMENSION = new Dimension(1000,250);
private static void ImagePanel(ArrayList<ResourceListObject> arr, JPanel imagelist)
{
for(int i=0 ; i < arr.size();i++)
{
try {
// column 1: name
JTextField t1 = createTextField(arr.get(i).getName(), NAME_FIELD_DIMENSION);
// column 2: Created At
JTextField t2 = createTextField(arr.get(i).getCreatedDate(), DATE_FIELD_DIMENSION);
// column 3: Status
JTextField t3 = createTextField(arr.get(i).getStatus(), STATUS_FIELD_DIMENSION);
// column 4: Image Id
JTextField t4 = createTextField(arr.get(i).getID(), IMAGE_FIELD_DIMENSION);
//column 5:Volume Id
JTextField t5 = createTextField(arr.get(i).getVolume_id(), VOLUME_FIELD_DIMENSION);
// construct the row (panel), and then add it to the list panel
JPanel rowPanel = new JPanel();
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
rowPanel.add(t1);
rowPanel.add(t2);
rowPanel.add(t3);
rowPanel.add(t4);
rowPanel.add(t5);
imagelist.add(rowPanel);
} catch (Exception e) {
}
}
}
private static JTextField createTextField(String text, Dimension d) {
JTextField tf = new JTextField(text);
tf.setPreferredSize(d);
tf.setBackground(Color.WHITE);
tf.setEditable(false);
return tf;
}
private static JPanel getColumnHeadings() {
GridBagConstraints regularFieldConstraints = new GridBagConstraints();
regularFieldConstraints.fill = GridBagConstraints.BOTH;
String nameColumn = "Image Name";
String secondColumn = "Created At";
String thirdColumn = "Status";
String fourthColumn = "Image Id";
String fifthColumn = "Volume Id";
JLabel name = new JLabel(nameColumn);
JLabel created_at = new JLabel(secondColumn);
JLabel status = new JLabel(thirdColumn);
JLabel id = new JLabel(fourthColumn);
JLabel volume_id = new JLabel(fifthColumn);
name.setPreferredSize(NAME_FIELD_DIMENSION);
created_at.setPreferredSize(DATE_FIELD_DIMENSION);
status.setPreferredSize(STATUS_FIELD_DIMENSION);
id.setPreferredSize(IMAGE_FIELD_DIMENSION);
volume_id.setPreferredSize(VOLUME_FIELD_DIMENSION);
JPanel headingsPanel = new JPanel(new GridBagLayout());
headingsPanel.add(name, regularFieldConstraints);
headingsPanel.add(created_at, regularFieldConstraints);
headingsPanel.add(status, regularFieldConstraints);
headingsPanel.add(id, regularFieldConstraints);
headingsPanel.add(volume_id, regularFieldConstraints);
return headingsPanel;
}
The class sun.swing.table.DefaultTableCellHeaderRenderer is not part of the published API. If you are reluctant to depend on it, consider these alternative options:
Use the existing renderer, as shown here and below.
Use Darryl Burke's Default Table Header Cell Renderer.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class Demo {
/** #see https://stackoverflow.com/a/6644956/230513 */
private static class HeaderRenderer implements TableCellRenderer {
TableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = table.getTableHeader().getDefaultRenderer();
}
#Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
// assumes JLabel
JLabel label = (JLabel) renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
label.setHorizontalAlignment(JLabel.LEADING);
return label;
}
}
private void initGUI() {
Object[] columnNames = new Object[]{
"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
dtm.addRow(new Object[]{"Some picture's name", "2013-10-05",
"Status unknown", "0123456789", "9876543210"});
JTable table = new JTable(dtm);
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));
JScrollPane scrollPane = new JScrollPane(table);
JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(600, 250));
content.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().initGUI();
}
});
}
}
I want that all the column name are indents like Image Id
If I'm understanding right, you want column header labels left-aligned just as Image ID. If so, you can use a TableCellRenderer to achive that using JTable.getHeader() method. (if not so, sorry for misunderstand your question)
For instance you can use a sun.swing.table.DefaultTableCellHeaderRenderer which extends from DefaultTableCellRenderer.
Sample Code
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import sun.swing.table.DefaultTableCellHeaderRenderer;
public class Demo{
private void initGUI(){
Object[] columnNames = new Object[]{"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
dtm.addRow(new Object[]{"Some picture's name", "2013-10-05", "Status unknown", "0123456789", "9876543210"});
/*
* Create your own TableCellRender
*/
TableCellRenderer headerRenderer = new DefaultTableCellHeaderRenderer(){
#Override
public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
super.getTableCellRendererComponent(jtable, o, bln, bln1, i, i1);
setHorizontalAlignment(SwingConstants.LEADING);
return this;
}
};
JTable table = new JTable(dtm);
table.getTableHeader().setDefaultRenderer(headerRenderer);
JScrollPane scrollPane = new JScrollPane(table);
JPanel content = new JPanel(new BorderLayout());
content.setPreferredSize(new Dimension(600,250));
content.add(scrollPane);
JFrame frame = new JFrame("Demo");
frame.setContentPane(content);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().initGUI();
}
});
}
}
I would like the JTable to autoscroll to the bottom whenever I add a new column and show the last 10 rows. However, I have the option of scrolling to anywhere I want (mouse listener?). Do you know how to do that? Here's the code I have so far. It builds a JTable and adds a new row for every mouse click on the JButton.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class sampleGUI extends JFrame implements ActionListener {
private JButton incrementButton;
private JTable table;
private DefaultTableModel model;
private int count;
private JScrollPane scroll;
public sampleGUI() {
JFrame frame = new JFrame("sample frame");
frame.setLayout(new BorderLayout());
incrementButton = new JButton("Increase the count!");
model = new DefaultTableModel();
model.addColumn("column 1");
table = new JTable(model);
frame.add(incrementButton, BorderLayout.NORTH);
scroll = new JScrollPane(table)
frame.add(scroll, BorderLayout.CENTER);
count = 0;
incrementButton.addActionListener(this);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#Override
public synchronized void actionPerformed(ActionEvent e) {
if (e.getSource() == incrementButton) {
count++;
model.addRow(new Object[] { count });
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sampleGUI gui = new sampleGUI();
}
});
}
}
Thanks!
Required to change selection in JTable, add code line
table.changeSelection(table.getRowCount() - 1, 0, false, false);
to
public (synchronized) void actionPerformed(ActionEvent e) {
I would like the JTable to autoscroll to the bottom whenever I add a new column
I assume you mean scroll to the bottom when you add a new row?
model.addRow(new Object[] { count });
table.scrollRectToVisible(...);
You forget add JScrollPane to the table :
//...
frame.add(new JScrollPane(table), BorderLayout.CENTER);
//...
and don't forget
import javax.swing.JScrollPane;