I have used a JTable only for the alignement of the values I want to display.
So far I my table looks like this:
What I want is to delete the margin of the table, so it can't be noticed that it is a table. Is there a way to make this possible?
My code:
displayNames = new ArrayList<String>();
displayClasses = new ArrayList<String>();
for (int i=0; i<displayName.size(); i++) {
displayNames.add(displayName.get(i));
displayClasses.add(classes.get(i));
}
Object rowData[][] = { displayNames.toArray(), displayClasses.toArray() };
Object columnNames[] = displayNames.toArray();
JTable table = new JTable(rowData, columnNames);
table.setTableHeader(null);
table.setShowGrid(false);
table.setBackground(Color.LIGHT_GRAY);
tablePane = new JScrollPane(table);
tablePane.setPreferredSize(new Dimension(765,40));
tablePane.setBackground(Color.LIGHT_GRAY);
rightPanel.removeAll();
rightPanel.updateUI();
rightPanel.add(tablePane);
public void showGUI() {
JFrame frame = new JFrame();
frame.add(leftPanel,BorderLayout.EAST);
frame.add(listScrollPane,BorderLayout.WEST);
frame.add(rightPanel);
frame.setSize(1000,500);
frame.setLocation(200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
It sounds like you want to use setBorder on the JScrollPane
tablePane.setBorder(BorderFactory.createEmptyBorder());
Answered in more detail here.
tablepane.setBorder(BorderFactory.createEmptyBorder());
The margin (and border) are showing because you place your table in a JScrollPane.
If you use rightPanel.add(**table**), the margin is gone.
If still want the border, you can manually add a border to the table using table.setBorder(...).
By not using the JScrollPane, you obviously will also lose the ability to scroll... I'm not sure if this is a problem for you.
I also agree with Andrew: you shouldn't use a table for this, use GridBagLayout instead.
Related
I have a tablePanel which is a JScrollPane,and initialized with a JTable, the JTable initialized with a defaultTableModel.When I trying to add some rows to the table, but didn't see the scroll bar, appriciated for any reply.
JFrame jFrame = new JFrame();
//rows will be added dynamically.
DefaultTableModel defautTableModel = new DefaultTableModel(null,columnNames){
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable jTable = new JTable(defautTableModel);
jTable.setLocation(20,60);
jTable.setSize(950,450);
jTable.setRowHeight(25);
jTable.getColumn("No.").setMaxWidth(45);
jTable.getColumn("position").setMaxWidth(45);
...
JTableHeader jTableHeader = jTable.getTableHeader();
jTableHeader.setLocation(20,30);
jTableHeader.setSize(950,30);
jTableHeader.setFont(new Font(null, Font.BOLD, 16));
jTableHeader.setResizingAllowed(true);
jTableHeader.setReorderingAllowed(true);
JScrollPane tablePanel = new JScrollPane(jTable);
tablePanel.setLayout(null);
tablePanel.add(jTableHeader);
tablePanel.add(jTable);
jFrame.setContentPane(tablePanel);
tablePanel.setLayout(null); is the primary cause of your problem. A JScrollPane has its own layout manager which is used to manage the scrollbars, view port and headers.
tablePanel.add is your next problem, as you shouldn't be adding components to the JScrollPane. Instead, you should be setting the JScrollPane's JViewPort.
But, since you're using JScrollPane tablePanel = new JScrollPane(jTable);, there's actually no need for the three lines which follow it.
I would highly recommend that you take a closer look at:
How to us tables
How to use scroll panes
Laying Out Components Within a Container
Now, before you tell me how nothing I've suggested actually works, go and re-read Laying Out Components Within a Container - this is the corner stone concept you will need to understand and master before Swing really begins to work for you
JScrollPane tablePanel = new JScrollPane(jTable);
// No need for the below code
/*tablePanel.setLayout(null);
tablePanel.add(jTableHeader);
tablePanel.add(jTable);*/
jFrame.setContentPane(tablePanel);
I created a JFrame with Java Swing using a BorderLayout. Both east and west layout are fixed with some components, while BorderLayout at the center can change depending on what the user does. At the beginning there is a picture,
panelDinamycCenter = new JPanel ();
JScrollPane = new JScrollPane (panelDinamycCenter);
panelDinamycCenter.add (new JLabel ( "", new Imagelcon ( "......."), JLabel.CENTER));
jFrame.getContentPane (). add (JScrollPane, BorderLayout.CENTER);
Then a data entry screen, for which I used a GridBagLayout. In order to remove the picture and to insert the new screen I used this:
panelDinamycCenter.removeAll (),
then I add the various components. After
setVisible (true);
I have now created a JTable, to be included in panelDinamycCenter,
panelDinamycCenter.removeAll ();
String [] [] matrixValori = new String [arrayOggCreaJTableDeroghe.length] [arrayJTableNomeColumnDeroghe.length];
for (int i = 0; i <arrayOggCreaJTableDeroghe.length; i ++) {
matrixValori [i] = arrayOggCreaJTableDeroghe [i] .creaArrayString ();
}
DefaultTableModel model = new DefaultTableModel (matrixValori, arrayJTableNomeColumnDeroghe);
JTable JTable = new JTable (model);
jTable.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
panelDinamycCenter.add (JTable);
jFrame.setVisible (true);
All right, but I can not see the names of the columns. Why?
As shown in How to Use Tables: Adding a Table to a Container, "The scroll pane automatically places the table header at the top of the viewport." At a minimum, you need to replace the JScrollPane removed by removeAll():
JTable jTable = new JTable(model);
panelDinamycCenter.add(new JScrollPane(jTable));
Among alternatives, consider these:
Add the table header to PAGE_START, as suggested here.
Instead of removing and restoring, use CardLayout to switch between views.
EDIT: Sorry for many edits. I forgot what I wrote by myself.
I use JPanel that has BoxLayout as root Panel for JFrame. I'm adding to this root Panel two other Panels: buttonPanel with FlowLayou and tabbedPane. Each tabbed Pane is created dynamically by pressing second button at the top. In tabbedPane there is a templatePanel with BoxLayout that contains three other general JPanels: Checkboxes Panel with FlowLayout, tablePanel with BorderLayout and another one with BoxLayout.
I'm adding a JTable to tablePanel with BoderLayout.CENTER and after running program JTable is way too big vertically and I need to resize frame. I need to add rows dynamically so I create an empty JTable with my custom DefaultTableModel (I overloaded isCellEditable method, nothing more) and then by checking checkboxes I fill it with data.
JTable is also way too big than maximum rows number it is designed to hold.
What I mean:
How can I shrink it?
I create templatePanel with class's constructor (extends JPanel) by just add.(templatePanel)
code:
public TemplatePanel()
{
model = new DefaultTableModel(new Object[][] {}, new String[]
{"<html>...</html>", "<html>...</html>",
"...", "...", "<html>...</html>",
"<html>...</html>", "...", "...",
"<html>...</html>"})
{
#Override
public boolean isCellEditable(int row, int column)
{
return column == 1 || column == 3;
}
};
templatePanel = new JPanel();
tablePanel = new JPanel();
templatePanel.setLayout(new BoxLayout(templatePanel, BoxLayout.PAGE_AXIS));
tablePanel.setLayout(new BorderLayout());
checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new FlowLayout());
1 = new JCheckBox("...");
2 = new JCheckBox("...");
3 = new JCheckBox("...");
4 = new JCheckBox("...");
5 = new JCheckBox("...");
6 = new JCheckBox("...");
checkBoxPanel.add(1);
checkBoxPanel.add(2);
checkBoxPanel.add(3);
checkBoxPanel.add(4);
checkBoxPanel.add(5);
checkBoxPanel.add(6);
1.addItemListener(this);
2.addItemListener(this);
3.addItemListener(this);
4.addItemListener(this);
5.addItemListener(this);
6.addItemListener(this);
table = new JTable(model);
scrollPane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setTableHeader(new JTableHeader(table.getColumnModel())
{
#Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.height = 50;
return d;
}
});
TableColumn firstColumn = table.getColumnModel().getColumn(0);
TableColumn secondColumn = table.getColumnModel().getColumn(1);
TableColumn thirdColumn = table.getColumnModel().getColumn(2);
TableColumn ninthColumn = table.getColumnModel().getColumn(8);
firstColumn.setPreferredWidth(170);
secondColumn.setPreferredWidth(50);
thirdColumn.setPreferredWidth(30);
ninthColumn.setPreferredWidth(100);
table.setRowHeight(30);
tablePanel.add(scrollPane, BorderLayout.NORTH);
templatePanel.add(checkBoxPanel);
templatePanel.add(tablePanel);
add(templatePanel);
}
The basic logic should be:
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
This will get rid of the extra vertical space.
If I understand correctly, you want the frame of the table to be smaller? Is that correct? If you're using BorderLayout, center will cover the entire frame unless you add something in the cardinal directions. Create a box and add it SOUTH to create a cushion between the bottom and the table.
Again, If I'm not understanding this properly I apologize.
Edit:
Have you tried using setPreferredSize(new Dimension(800, 500)) on the scrollpane?
I'm pretty new to Javas swings, so sorry if this is something trivial. This is the constructor, I excluded unnecessary form items. (I tried running the code as short as this, but the problem still appears)
//This just opens a connection to MySQL server, this doesn't create any problems.
bp = BazaPodataka.getBaza();
//Forming the main frame..
JFrame frame = new JFrame();
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(d.width/2 - sirina/2, d.height/2 - visina/2, sirina, visina);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
//Adding a layered pane so I can place items inside the form more 'freely'
JLayeredPane layeredPane = new JLayeredPane();
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);
//Adding a table
JTable table = new JTable();
String[] rowData = {"Name:", "Price:", "Cathegory:", "Sum:"};
DefaultTableModel model = new DefaultTableModel(rowData, 0);
JScrollPane skrol = new JScrollPane(table);
table.setModel(model);
//The 2 lines below work as intended
ResultSet rs = (ResultSet) bp.query("SELECT * FROM table"); //This calls a query
popuniTabelu(rs, model); //This populates the table.
table.setBounds(10, 110, 500, 350);
table.setEnabled(false);
table.setShowHorizontalLines(false);
layeredPane.add(table);
Populating the table and displaying it isn't the problem, there's enough information inside the table 'table' that the user even needs to scroll down.
But that's where the problem begins, the scroll doesn't show up. How do I implement it from here. I tried following solutions I found on google, but they pretty much sum up on:
JScrollPane scroll = new JScrollPane(table);
Which simply doesn't work in my case.
The problem may be trivial, if it is, I'm sorry, I'm still learning swing. Also, sorry for my bad english, it's not my native language. :)
Also, if there's something I forgot to include, please let me know.
Thank you!
You add your table to 2 components :
JScrollPane skrol = new JScrollPane(table);
and
layeredPane.add(table);
Because of Swing component can have just one parent component second statment override first, so your JScrollPane is empty. Seems you need to remove layeredPane.add(table);
As mentioned here
Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.
I'm trying to create a JTable with the column headers defined in colName using a DefaultTableModel, then adding the table to a JScrollPane then to a JPanel. However, when I add the panel to my JFrame, only the panel shows up, not the table. I am using similar code in another table, and that one shows up fine, only difference being the number of columns and variable names.
What am I missing?
My code :
//Column Names
final String[] colNames = {"Item", "Count"};
DefaultTableModel dtm = new DefaultTableModel(0, colNames.length);
//Panel to hold Table
JPanel j = new JPanel(new BorderLayout());
j.setBounds(9, 78, 267, 254);
//Colored to see if the panel has been added
j.setBackground(Color.RED);
//Set Column Headers
dtm.setColumnIdentifiers(colNames);
//Jtable with model
JTable t = new JTable(dtm);
t.setBackground(Color.GREEN);
t.getTableHeader().setReorderingAllowed(false);
t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
t.getColumnModel().getColumn(0).setPreferredWidth(113);
t.doLayout();
j.add(new JScrollPane(t), BorderLayout.CENTER);
I would suggest that the column's are being overridden by those reported back by the table model. You could instead use...
String[] colNames = {"Item", "Count"};
DefaultTableModel dtm = new DefaultTableModel(colNames, 0);
JPanel j = new JPanel(new BorderLayout());
JTable t = new JTable(dtm);
t.setBackground(Color.GREEN);
t.getTableHeader().setReorderingAllowed(false);
t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
t.getColumnModel().getColumn(0).setPreferredWidth(113);
j.add(new JScrollPane(t), BorderLayout.CENTER);
Instead...
Without seeing the code you're using to put the table on the frame, it's difficult to comment further, however...
Avoid using setBounds, it's pointless in this context any way.
The background color will actually be defined more by the view port then the table or panel until the table is either configured to fill the empty space or has enough rows to fill the empty space