How can i display the column name of a table - java

I use the following code to display a table.
final Vector<Vector<String>> vct = refreshDatas();
final Vector<String> Cols = new Vector<String>();
Cols.add("OID");
Cols.add("Name");
this.tmodel = new DefaultTableModel(vct,Cols);
this.table.setModel(this.tmodel);
this.table.setBounds(50, 200, 300, 250);
this.table.setSize(200, 200);
this.table.setVisible(true);
but only the contents is displayed. The header OID and Name are not displayed.

See the 'Adding a table to a container' section in the table tutorial.
If you add the table yourself, you must make the headers visible as well. If you add your table to a scrollpane, the scrollpane will take care of this for you.
Copy-paste from that tutorial:
If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);
Sidenote: it should not be needed to call setBounds nor setSize. Just make sure your parent Container has a decent LayoutManager and it will take care of the size

just add the table in to the JScrollPane, It automatically display the table headers.

Related

How to make java message dialog bigger?

Im currently using the following code
tableModel.setDataVector(data, columnNames);
jTable2 = new JTable(tableModel);
JOptionPane.showMessageDialog(null, new JScrollPane(jTable2));
and when I execute it, the resulting size looks like below
How can I make message dialog bigger, so that we can see the column headings? I want to remove those '...'
Thanks!
Set a preferredSize to the JScrollPane ?

How to refresh a JTable on JScrollPane

as many other persons, I want my JTable being actualized after adding rows.
Here you can see the fragments of my code.
ArrayList<> foundR = new ArrayList<FoundResult>();
JTable resultsTable = new JTable();
AbstractTableModel resultsModel = new FoundResultTableModel(foundR);//<- model which contains FoundResults as rows
JScrollPane scroller = new JScrollPane();
resultsTable.setModel(resultsModel);
scroller.add(resultsTable);
this.add(scroller, BorderLayout.CENTER);// a big panel which contains scroller and other components
Then a add ActionListener that uses following method:
foundR.clear();
foundR.addAll( ...);//<- so the foundR ArrayList is refreshed
resultsModel.fireTableDataChanged();
resultsModel.fireTableStructureChanged();
rowCount of results Model shows that the model is refreshed (the number of rows differes from the previeous variant), but the table still doesn't appear. I tried to insert resultsTable.repaint() but it also dind't help.
UDP(NB) I discovered, that the problem is concentrated in this scroller Panel.
If I add the table directly to the big panel it is refreshed (but I cannot see all the results, since I cannot scroll down)
this.add(resultsTable, BorderLayout.CENTER);
If I use a scroller, nothing is shown. Do you know why?
Just recreate the model and set it to your table.
foundR.clear();
foundR.addAll( ...);//<- so the foundR ArrayList is refreshed
AbstractTableModel resultsModel = new FoundResultTableModel(foundR);
resultsTable.setModel(resultsModel);
Add this to your code,
resultsTable.revalidate();
Since your model has been changed but the change in model should be notified to the table also.

Java BorderLayout, south never displays itself

Im' working on a computer science project, and I'm having some issues with borderlayout:
that is my configuration (simplified)
public class MainPanel extends JPanel{
JSplitPane Pcenter;
JPanel PEnd;
public MainPanel(Container dad){
JTable myTable=new JTable(), anotherTable=new JTable();
JSplitPane left=new JSplitPane();
left.setTopComponent(new JScrollPane (myTable));
left.setBottomComponeny(new JScrollPane(anotherTable));
left.setOrientation(JSplitPane.VERTICAL_SPLIT);
this.setLayout(new BorderLayout());
Pcenter=new JSplitPane();
PEnd=new JPanel();
pend.add(new JButton("Store"));
Pcenter.setLeftComponent(left);
Pcenter.setRightComponent(new JScrollpane(new JLabel("right")));
this.add(Pcenter,BorderLayout.CENTER);
this.add(PPnd,BorderLayout.PAGE_END);
//EDIT:
PEnd.add(new JLabel("goofy"));
}
}
Now, my project is different, but this is my configuration
as I run the main (a jframe whith a JTabbed with this attached as a tab) it shows me only the the center and not the end one. But if I attach PEnd at the beginning it's sown as it have to.
EDIT 14-3-14
I dug into my code and I've seen that te problem is generated by the myTable's scrollpane so removing it will make PEnd shown but mytable represents itself in an horrible way
PEnd, the panel you are adding at the PAGE_END has nothing added to it so there is nothing to show, and has size 0.
Maybe with the line
pend.add(new JButton("Store"));
you meant
PEnd.add(new JButton("Store"));
As a side note, your variable names should start in lower case to follow standard Java style, to differenciate them from classes.

No Scroller For JList

In my swing application, I set a Vector as data to JList. JList is resizable (vertical and horizontal). Number of viewable items is 8. When data is set morethan 8 items to the JList, the Scroller is not visible and to see the rest of items, JList should be pull down. Is there any way to get this JScroller ?
I use Netbeans IDE 7.2 I checked the JScrollerPane's properties. It has default values for horizontal and vertical scroll bars.
Here is JScrollPane and JList codes which NetBeans has created.
JScrollPane
jScrollPane1 = new javax.swing.JScrollPane();
This is JList
jList1 = new javax.swing.JList();
jList1.setBackground(new java.awt.Color(##, ##, ##));
jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(##, ##, ##), 1, true));
jList1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
jList1.setForeground(new java.awt.Color(##, ##, ##));
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jList1.setCellRenderer(new CellRendererManager());
jList1.setMaximumSize(new java.awt.Dimension(30, 80));
jList1.setMinimumSize(new java.awt.Dimension(30, 80));
jList1.setPreferredSize(new java.awt.Dimension(30, 80));
jList1.setValueIsAdjusting(true);
jScrollPane1.setViewportView(jList1);
`
You state:
JScrollPane lays on FreeDesign layout which shows in NetBeans IDE. JScrollPane has not been set any size.But the JList has set a dimension(30,80)
There's your problem -- you're setting the Dimension of the JList which prevents it's view from expanding, even if it is held by a JScrollPane.
Solution: don't do this. Read up on and use the various layout managers so that they and your component's innate preferred sizes do all the sizing and heavy lifting for you.

Stretching out JScrollPane - Java

I have this structure:
<JFrame>
<JPanel backgroundcolor = "pink">
<JScrollPane>
<JTable>!!!Data here !!!</JTable>
</JScrollPane>
</JPanel>
</JFrame>
How do i stretch the ScrollPane it to cover the full window without using setSize?
This is how it looks like now:
alt text http://img22.imageshack.us/img22/8491/17747996.png
Thanks!
Mmmph! Nobody offered a simple solution such as using BorderLayout as layout manager for my JScrollpane container!
I am not familiar with the XML file format.
If it is coded, you may need to code something like this:
JScrollPane1 = new JScrollPane();
JPanel1.add(JscrollPane1);
JScrollPane1.setBounds(5,29,636,122);
JTable1 = new JTable();
JPanel1.add(JTable1);
JScrollPane1.setBounds(5,434,553,3097);
JScrollPane1.setViewportView(JTable1);
Use setPreferredScrollableViewportSize() and a suitable layout.
Edit: You'll also need setFillsViewportHeight(), as discussed in Adding a Table to a Container.

Categories