Example:
I have problem with table header. How can i remove this white border or change color of it?
My code:
JTableHeader header = table.getTableHeader();
header.setBackground(new Color(21, 25, 28));
header.setForeground(new Color(255, 117, 0));
header.setPreferredSize(new Dimension(10,30));
header.setBorder(new LineBorder(new Color(21,25,28),2));
Well you were almost there...
For the header you can do:
final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setBorder(null);
header.setDefaultRenderer(renderer);
If you also want to remove the borders from the data cells also (practically removing the grid), then call:
table.setShowGrid(false);
Here is an assembled example of a JTable without any borders (except when the user selects a cell, then a border will appear around it temporarily):
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
public class Main {
private static void createAndShowGUI() {
final JFrame frame = new JFrame("Table without borders");
final JTable table = new JTable(new Object[][]{
new Object[]{"user", "user", "user"},
new Object[]{"user", "user", "user"},
new Object[]{"user", "user", "user"},
new Object[]{"user", "user", "user"},
new Object[]{"user", "user", "user"},
new Object[]{"user", "user", "user"}
}, new Object[]{"A", "B", "C"});
JTableHeader header = table.getTableHeader();
final Color dark = new Color(21, 25, 28),
orange = new Color(255, 117, 0);
table.setForeground(orange);
table.setBackground(dark);
header.setBackground(dark);
header.setForeground(orange);
final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setBorder(null); //Remove border from the renderer (which is just a JLabel)...
//Apply the renderer to all header cells and all data cells:
header.setDefaultRenderer(renderer);
table.setDefaultRenderer(Object.class, renderer);
/*The follwing lines simply remove the border from the cell editor
(ie the component which draws each cell when the user edits its value):*/
//final JTextField editor = new JTextField();
//editor.setBorder(null);
//table.setCellEditor(new DefaultCellEditor(editor));
//header.setBorder(null); //Not needed.
table.setShowGrid(false); //Hide the grid (which is some extra border between cells).
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
Related
I have several tabs and on one of the tabs I put a scroll pane. I would like to add another pane below it so that I can add some buttons. It just keeps readjusting to fill the whole tab though. I have tried setsize and setprefered size but had no luck. Can anyone see what I am doing wrong or point me in the right direction? Much appreciated!
JFrame frame = new JFrame();
JScrollPane pane = new JScrollPane(table);
pane.setBounds(0, 0, 415, 50); // < ---This seems to do nothing
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JTabbedPane jtp = new JTabbedPane();
jtp.setBounds(-11, -14, 436, 170); // <---- I used -ve to hide border
jtp.addTab("tab1", new JLabel("Tab1"));
jtp.addTab("tab2", new JLabel("Tab2"));
jtp.addTab("tab3", new JLabel("Tap3"));
jtp.addTab("tab4", new JLabel("Tab4"));
jtp.addTab("tab5", pane);
jtp.setTabPlacement(JTabbedPane.BOTTOM);
frame.add(jtp,BorderLayout.CENTER);
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
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.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
public class ScrollSizeRedone {
public static void main(final String[] args) {
final JFrame frame = new JFrame();
final JTable table = new JTable(new DefaultTableModel(new String[][] { { "a", "b" }, { "c", "d" } }, new String[] { "col1", "col2" }));
final JScrollPane pane = new JScrollPane(table);
// pane.setBounds(0, 0, 415, 50); // < ---This seems to do nothing // no use
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
final JPanel completePanel = new JPanel();
completePanel.setLayout(new BorderLayout());
completePanel.add(pane);
final JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(new JButton("LOL"));
buttonsPanel.add(Box.createHorizontalStrut(100));
buttonsPanel.add(new JButton("ROFL"));
buttonsPanel.add(Box.createHorizontalGlue());
buttonsPanel.add(new JButton("MUAHAHA"));
completePanel.add(buttonsPanel, BorderLayout.SOUTH);
final JTabbedPane jtp = new JTabbedPane();
// jtp.setBounds(-11, -14, 436, 170); // <---- I used -ve to hide border
jtp.addTab("tab1", new JLabel("Tab1"));
jtp.addTab("tab2", new JLabel("Tab2"));
jtp.addTab("tab3", new JLabel("Tap3"));
jtp.addTab("tab4", new JLabel("Tab4"));
jtp.addTab("tab5", completePanel);
jtp.setTabPlacement(JTabbedPane.BOTTOM);
frame.add(jtp, BorderLayout.CENTER);
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
i have a jtable where i can put some records but now i want to insert jbutton in place of that records
my code is given below
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class ScrollableJTable {
public static void main(String[] args) {
new ScrollableJTable();
}
public ScrollableJTable() {
JFrame frame = new JFrame("Creating a Scrollable JTable!");
JPanel panel = new JPanel();
String data[][] = {
{
"001", "vinod", "Bihar", "India", "Biology", "65", "First"
},
};
panel.setLayout(new BorderLayout());
String col[] = {
"Roll", "Name", "State", "country", "Math", "Marks", "Grade"
};
JTable table = new JTable(data, col);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(pane);
frame.setSize(xSize, ySize);
table.setSize(xSize, ySize);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
If you want to display a button is a column then you need to create a custom renderer and editor for the column. Read the section from the Swing tutorial on - Concepts: Editors and Renderers for some basic information.
Then check out Table Button Column for one approach.
i am working on a gui based project and i need to add a jtable in a jpanel.But i am not getting why the table is not being displayed when adding with a scrollpane.Also when adding without scrollpane,the table header is not displayed.
Thanks for any help...
Following is the code i am using..
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class FrmAddEditDN extends JDialog{
JButton JBUpdate = new JButton(new ImageIcon("images/save.png"));
JButton JBReset = new JButton("Reset",new ImageIcon("images/reset.png"));
JButton JBCancel = new JButton("Cancel",new ImageIcon("images/cancel.png"));
JLabel JLPic1 = new JLabel();
JLabel JLBanner = new JLabel("Please fill-up all the required fields.");
public FrmAddEditDN(boolean ADD_STATE,JFrame OwnerForm,Connection srcCN,String srcSQL){
super(OwnerForm,true);
cnAEDN = srcCN;
ADDING_STATE = ADD_STATE;
JPanel JPContainer = new JPanel();
JPContainer.setLayout(null);
String[] columnNames = {
"Sr No","Invoice No","Invoice Date","Consignee","Description","Basic Amount","Invoice Amount","Payment Received",
"EFT Date","Payment Earlier Received","Comm. # %","Comm.Claim # %","Comm. Received","Date","Bank","Remarks"};
String[][] data = {
{"","","","","","","","","","","","","","","",""}
};
DefaultTableModel DTModel = new DefaultTableModel(data,columnNames);
JTable table = new JTable(5,16);
table.setBounds(15,295,screen.width-40,150);
table.setRowHeight(30);
table.setForeground(Color.black);
table.setBackground(Color.white);
JTableHeader header = table.getTableHeader();
header.setForeground(Color.red);
header.setBackground(Color.green);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(2);
table.getColumnModel().getColumn(1).setPreferredWidth(50);
JScrollPane tableContainer = new JScrollPane();
tableContainer.setViewportView(table);
JPContainer.add(tableContainer);
getContentPane().add(JPContainer);
setSize(screen.width-5,screen.height-45);
setResizable(false);
setLocation(0,0);
}
}
You need to pass Component while creating a JScrollPane.
JScrollPane scrollPane = new JScrollPane(table);
JPContainer.add(scrollPane);
I am new to JTable.
I want to update the jtable data at runtime in button press event.
Here is my code.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SetEditableTableCell extends JPanel {
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JButton update = new JButton("Update");
JTable table;
Vector<String> rowOne;
Vector<String> rowTwo;
DefaultTableModel tablemodel;
public SetEditableTableCell() {
this.setLayout(new BorderLayout());
rowOne = new Vector<String>();
rowOne.addElement("Row1-1");
rowOne.addElement("Row1-2");
rowOne.addElement("Row1-3");
rowTwo = new Vector<String>();
rowTwo.addElement("Row2-2");
rowTwo.addElement("Row2-3");
rowTwo.addElement("Row2-4");
Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(rowOne);
rowData.addElement(rowTwo);
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Column One");
columnNames.addElement("Column Two");
columnNames.addElement("Column Three");
tablemodel = new DefaultTableModel(rowData, columnNames);
table = new JTable(tablemodel);
// table.setValueAt("aa", 0, 0);
update.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
updatedata();
}
private void updatedata() {
rowOne = new Vector<String>();
rowOne.addElement("updated Row1-1");
rowOne.addElement("updated Row1-2");
rowOne.addElement("updated Row1-3");
rowTwo = new Vector<String>();
rowTwo.addElement("updated Row2-2");
rowTwo.addElement("updated Row2-3");
rowTwo.addElement("updated Row2-4");
// tablemodel.addRow(rowTwo);
tablemodel.fireTableDataChanged();
table.setModel(tablemodel);
System.out.println("button pressed");
// table.setValueAt("aa", 0, 0);
}
});
JScrollPane scrollPane = new JScrollPane(table);
top.add(scrollPane);
bottom.add(update);
add(top, BorderLayout.NORTH);
add(bottom, BorderLayout.SOUTH);
}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SetEditableTableCell obj = new SetEditableTableCell();
frame.add(obj);
//frame.setSize(400, 300);
frame.pack();
frame.setVisible(true);
}
}
But it is not updating after pressing update button.
Can anybody solve my problem?
Thanks in advance..
It is not updated because you do not modify the values in your TableModel. By assigning a new Vector to rowOne and rowTwo in your updateData method, you are altering other Vector instances then the ones your TableModel knows about.
A possible solution is to reconstruct the data vector and use the setDataVector method
Construct your own table model using the AbstractTableModel and use it's "event" triggers to notify the JTable of changes to the underlying model
I have a set of panels across a layered pane. I need a separator between to separate the sideBar from the topBar and the tabbedPanel. I left a buffer of 10 pixels for it to be placed. unfortunately, possibly due to it being a JLayeredPane, I canot get it to view.
Is there a way to define the separator's X location? as this should solve it. Either way, here's a sample of the code, which I've removed most information from.
Alternatively, offer a different solution entirely, so long as I can get a defined split from the sideBar and other two panels. I've already tried to apply the BorderLayout.WEST to the sideBar, but due to it being a JLayeredPane, it gives me errors.
lPane = new JLayeredPane();
lPane.setBounds(0, 0, 1024, 768);
calendarFrame = new JFrame ("Calendar Frame");
calendarFrame.setPreferredSize(new Dimension(1024, 768));
calendarFrame.setLayout(null);
//Prepare side bar
sideBar = new JPanel ();
sideBar.setLayout(null);
sideBar.setBounds(0, 0, 210, 768);
//Prepare top bar
topBar = new JPanel ();
topBar.setLayout(null);
topBar.setBounds(220, 0, 774, 50);
//Create tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.setBounds(220, 50, 774, 700);
//Tab code here, but not needed for questuion
calendarFrame.add(lPane, BorderLayout.CENTER);
lPane.add(sideBar, new Integer(0), 0);
lPane.add(Box.createHorizontalStrut(5));
lPane.add(new JSeparator(SwingConstants.VERTICAL));
lPane.add(Box.createHorizontalStrut(5));
lPane.add(topBar, new Integer(1), 0);
lPane.add(tabbedPane, new Integer(2), 0);
EDIT:
if you want to create fixed 10pixels gap,
you can use
EmptyBorder (or CompoundBorder)
BorderLayout(int horizontalGap, int verticalGap) or GridLayout(int rows, int cols, int hgap, int vgap)
just my curiosity are you ...
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class EditableListExample extends JFrame {
private static final long serialVersionUID = 1L;
public EditableListExample() {
super("Editable List Example");
String[] data = {"a", "b", "c", "d", "e", "f", "g"};
JList list = new JList(data);
JScrollPane scrollList = new JScrollPane(list);
scrollList.setMinimumSize(new Dimension(100, 80));
Box listBox = new Box(BoxLayout.Y_AXIS);
listBox.add(scrollList);
listBox.add(new JLabel("JList"));
DefaultTableModel dm = new DefaultTableModel();
Vector<String> dummyHeader = new Vector<String>();
dummyHeader.addElement("");
dm.setDataVector(strArray2Vector(data), dummyHeader);
JTable table = new JTable(dm);
table.setShowGrid(false);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setColumnHeader(null);
scrollTable.setMinimumSize(new Dimension(100, 80));
Box tableBox = new Box(BoxLayout.Y_AXIS);
tableBox.add(scrollTable);
tableBox.add(new JLabel("JTable"));
Container c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
c.add(listBox);
c.add(new JSeparator(SwingConstants.VERTICAL));
//c.add(new JLabel("test"));
//c.add(new JSeparator(SwingConstants.HORIZONTAL));
c.add(tableBox);
pack();
setVisible(true);
}
private Vector<Object> strArray2Vector(String[] str) {
Vector<Object> vector = new Vector<Object>();
for (int i = 0; i < str.length; i++) {
Vector<Object> v = new Vector<Object>();
v.addElement(str[i]);
vector.addElement(v);
}
return vector;
}
public static void main(String[] args) {
final EditableListExample frame = new EditableListExample();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}