Parsed data display in JTable - java

I am new to Java. I have to display xml Parsed data parent nodes on JTabbedPane and child nodes in jtable into respective JTabbedPane. For parsing i have used sax parser and all the data was previously displayed in three JTextarea, I have created TabbedPane and displayed hardcoded string as title but i am not able to set Jtable into it with values.
public class JTableDisplay {
public JTableDisplay() {
JFrame frame = new JFrame("JTable Test Display");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTable table = new JTable();
JScrollPane tableContainer = new JScrollPane(table);
panel.add(tableContainer, BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new JTableDisplay();
}
}
This is sample to create JTable after which i have to arrange the parsed data into in.

It is there. It is just not showing any data.
To make your table visible, try defining columns and rows for your table via the constructor :
JTable table = new JTable(3,4);
Get it?
Now you just need to define the tablemodel and you are set.

Related

Java scrollpane not adding to panel instead displaying white screen

So I have a table that is displaying data from a database but since there's a lot of data being outputted into the table it goes way beyond the bounds I have set for the JFrame.
I've tried adding a ScrollPane below but instead of allowing me to scroll down the GUI it will display white over the whole GUI! All I need to do is make it so I can scroll down to view the whole table!
Here is my code:
public class ViewAll extends JFrame{
//Jtextfields, buttons, labels
private static JButton one = new JButton("Back");
private static JLabel lblMembTitle = new JLabel("<html><h1>All Members</h1></html>");
private static JLabel lblPlayTitle = new JLabel("<html><h1>All Playlists</h1><br /></html>");
//Containers, Panels, Scrollpanes
private Container mainCon = this.getContentPane();
private static JPanel pnlTable = new JPanel();
//Tables
private static JTable tblShowAllMemb = new JTable();
private static JTable tblShowAllPlay = new JTable();
JScrollPane scrollPane = new JScrollPane(pnlTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public ViewAll(){
super("Search/Edit/Delete Members");
this.setBounds(400, 800, 854,400);
//Add panel and create table model
mainCon.add(pnlTable);
MemTableModel tblMembers = new MemTableModel();
PlayTableModel tblPlaylist = new PlayTableModel();
this.add(scrollPane);
//Add table and set tableModel
pnlTable.add(lblMembTitle);
pnlTable.add(tblShowAllMemb);
tblShowAllMemb.setModel(tblMembers);
pnlTable.add(lblPlayTitle);
pnlTable.add(tblShowAllPlay);
tblShowAllPlay.setModel(tblPlaylist);
}
}
It seems that you add your JPanel pnlTable to multiple containers, first to scrollPane, then to Container mainCon (so it is not in scrollPane, as a component can be only in one container). So when you add scrollPane separetly to ViewAll it display nothing, because it is empty.
So use:
mainCon.add(scrollPane);
or delete mainCon.add(pnlTable); and use only:
this.add(scrollPane);

Dynamic Panel Content from function call

What I am trying to do is add two X by 2 grid panels in the first row of a 2x2 grid content panel, leaving the bottom row of the content panel blank.
To populate the cells on the top row I want to use a function which uses a loop to generate a text field and a slider. the text field calling it's input from textList[n].
So this breaks down into two primary questions.
If I have a function:
public static void makeTop(String textName) {
JTextField textBox = new JTextField(textName);
textBox.setPreferredSize(new Dimension(100,50));
textBox.setHorizontalAlignment(JTextField.CENTER);
textBox.setEditable(false);
SpinnerNumberModel numSpinner = new SpinnerNumberModel(10,0,100,1);
JSpinner spinner = new JSpinner(numSpinner);
spinner.setPreferredSize(new Dimension(100,50));
}
And a frame w/ panel:
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cPane = new JPanel((new GridLayout(2,2)));
frame.add(cPane, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
How could I add the text field and spinner created in makeTop to cPane?
cPane.add() doesn't like function calls, and making cPane public didn't seem to help when trying to add the content in makeTop().
Secondly, let's say makeTop is called as follows, with N being arbitrary and textList[] being populated with Strings:
for(i=N;i>0;i--){
makeTop(textList[i]);
}
How could I get the text fields and sliders to be unique instances when creating them in such a way?
cPane.add doesn't like function calls, and making cPane public didn't
seem to help when trying to add the content in makeTop()
It won't work, indeed, because by contract makeTop(String textName) is returning void. But if you make this change:
public static JPanel makeTop(String textName){
JTextField textBox = new JTextField(textName);
textBox.setPreferredSize(new Dimension(100,50));
textBox.setHorizontalAlignment(JTextField.CENTER);
textBox.setEditable(false);
SpinnerNumberModel numSpinner = new SpinnerNumberModel(10,0,100,1);
JSpinner spinner = new JSpinner(numSpinner);
spinner.setPreferredSize(new Dimension(100,50));
JPanel panel = new JPanel(new FlowLayout());
panel.add(textBox);
panel.add(spinner);
return panel;
}
Then cPane.add(makeTop("Whatever")); will work like a charm.

Why will my JTable display on a new JFrame but not on my existing JPanel?

I am working with JDBC.
My Class has a JFrame with a JTabbedPane to display my JPanels with the UI's for my different methods.
On this panel I want to display a result set in a JTable with to additional columns of buttons.
This is all currently working when I display it on a new JFrame but not when I try to display it on the existing one. Could somebody please explain why.
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("Main", null, panel, null);
panel.setLayout(null);
JTable table = new JTable(model);
DisplayButtonColumn testWithButtons1 = new DisplayButtonColumn(table,
displayHandler.getColCount());
DisplayButtonColumn testWithButtons2 = new DisplayButtonColumn(table,
displayHandler.getColCount() + 1);
panel.add(new JScrollPane(table));
// JFrame f = new JFrame();
// f.setSize(1000, 500);
// f.getContentPane().add(new JScrollPane(table));
// f.setVisible(true);
panel.setLayout(null);
The problem is the null layout. Don't do that. Swing is designed to be used with layout managers and not doing that will lead to trouble almost without exception. Using absolute layout would require you to manually manage the bounds to the components, and you're not doing that. If you need something else than the default FlowLayout, just create one that matches your needs:
panel.setLayout(new BorderLayout());
(It would be also possible to add to JTabbedPane without the helper panel, but I suppose you want more components in the tab).

How to get the Jtable or get jTable model of the current tab?

Is there anyway i can get Jtable from the tabbed panel selected.
I creat tables dynamically for every tab selected using this code:
JScrollPane panel2 = new JScrollPane();
panel2.setName(tabidis);
chattable = new JTable();
chattable.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {},new String [] {"Messages"}));
panel2.setViewportView(chattable);
chattable.setShowGrid(false);
jTabbedPane1.add(username4, panel2);
//I am using GUI environment in netbeans.
I think the hierarchy as follows:
JTabbedPanel1 >> JscrollPAnel1 >> Jtable1
By using the following code:
((JTable)jTabbedPane1.getSelectedComponent().getComponentAt(1,1).getComponentAt(0,0)).getName();
I get the error that "javax.swing.table.JTableHeader cannot be cast to javax.swing.JTable"
That means that i am getting jTable Header as the component. But what i need is to get JTable as the component outcome so to get the model from it.
Or more simply "Is there anyway to get the model of a JTable present in the selected tab"
This seems to work:
#Test
public void test() {
// original components
JTable jTable = new JTable();
JScrollPane scrollPane = new JScrollPane(jTable);
// add them to tab
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("tab1", scrollPane);
jTabbedPane.setSelectedComponent(scrollPane);
// get them out of selected tab
JScrollPane scrollRef = (JScrollPane) jTabbedPane.getSelectedComponent();
JTable tableRef = (JTable) scrollRef.getViewport().getComponents()[0];
assertTrue(tableRef == jTable);
}

Scrollpane gets disappeared when frame is maximized

My prob is this....
JFrame ActualFrame = new JFrame("Actual frame");
JScrollPane pane = new JScrollPane(new JTable(data, columns));
ActualFrame.add(pane);
ActualFrame.add(PrintPreviewBtn);
PrintPreviewBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame PreviewFrame = new JFrame("Preview");
PreviewFrame.add(pane, BorderLayout.CENTER);
}
When i run the program everything seems to b fine but when i press print preview button the preview frame shows off and when i maximize or resize the ActualFrame the table gets disappeared....
NOTE:
I m adding the pane to the preview frame to show as if it is a preview of the table displayed.....is ter any other method for print preview
A component can only belong to a single parent. When you add it to the PreviewFrame, it is been removed, automatically, from the ActualFrame.
Instead of using the previous panel, create a new JTable, using the model from the previous one.
Update
Printing tables is a little more complicated, as includes the headers, and the columns need to be resized to meet the requirements of the available space.
Take a look at the Printing Tables tutorial for some examples
For printing there is the java.awt.PrinterJob class. To show the standart print preview you should call:
PrinterJob job = PrinterJob.getPrinterJob();
job.printDialog();
The Reason for disappearance of JScrollPane from the ActualFrame is that: You are adding the same instance of pane in PreviewFrame. So the actual container of the pane is now PreviewFrame instead of ActualFrame. When you maximize or resize the ActualFrame,it repaint its child components. Since the pane now no longer belongs to the ActualFrame it does not show the pane now.
The best way to avoid this situation is to create a seperate JTabel instance first, instead of passing anonymous JTable class object within the constructor of JScrollPane while creating the object pane . Get the TableModel and TableColumnModel of that JTable instance. In previewFrame add a new instance of JScrollPane that will contain the new instance of JTable with same TableModel and TableColumnModel objects.
The Code would look something like this:
final JTable table = new JTable(data,columns);
JScrollPane pane = new JScrollPane(table);
ActualFrame.add(pane);
ActualFrame.add(PrintPreviewBtn);
PrintPreviewBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame previewFrame = new JFrame("Preview");
javax.swing.table.TableModel tabModel = table.getModel();
javax.swing.table.TableColumnModel colModel = table.getColumnModel();
JScrollPane previewPane = new JScrollPane(new JTable(tabModel,colModel));
previewFrame.getContentPane().add(previewPane, BorderLayout.CENTER);
previewFrame.pack();previewFrame.setVisible(true);
}

Categories