I am trying to add a JTable to a JScrollPane. But I can't see the Table after doing this.
scrollpane.setBounds(100,50,800,400);
JTable table = new JTable(myTableModel(res));
scrollpane.add(table);
What's wrong with me?
scrollpane.add(table);
JScrollPane isn't designated to nest any JComponent, you have to add JTable to its JViewport, see also Constructor Sumary in JScrollPane API as the proper way
Try this:
JTable table = new JTable(myTableModel(res));
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBounds(100,50,800,400);
Try this
scrollpane.setBounds(100,50,800,400);
JTable table = new JTable(myTableModel(res));
scrollpane.setViewporView(table);
Related
so I have actually couple of issues where I could need some help.
1.) Actually I can display the SQL Result into the jList, that works so far.
What I am actually missing are the names of the Columns. It just shows me the MySQL Entries.
2.) The second issue is, that my Database actually includes more Information. I want to show that in the jList also but to prevent having a huge window I want to add a horizontal and vertical scrollbar.
I tried adding one in but whenever I did that the list suddenly disappeared. how would adding a scrollbar to the list actually look like ?
The next steps of my Java program would be MySQL Operations. I want to change Values of the received dataset and send it back. I think I will do that with some kind of getSelectedRow right ?
Thank you very much for your help
public class aPanel extends JFrame{
private JTable jt;
public aPanel() {
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 780, 450);
JPanel contentPane = new JPanel();
contentPane.setBackground(new Color (51,51,51));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel leftTitle = new JPanel();
leftTitle.setBackground(new Color (51,153,255));
leftTitle.setBounds(0, 0, 230, 450);
contentPane.add(leftTitle);
leftTitle.setLayout(null);
String[] columnNames = {"ID","First_Name","Last_Name"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
try {
// Establishment of JDBC Connection
String url = "jdbc:mysql://localhost:3306/eHealthDB?serverTimezone=UTC";
Connection con = DriverManager.getConnection(url,"root","root");
String sql = "SELECT ID,First_Name,Last_Name FROM patient;";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next() )
{
String pID = rs.getString("ID");
String vName = rs.getString("First_Name");
String nName = rs.getString("Last_Name");
String[] data = {pID,vName,nName};
tableModel.addRow(data);
}
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
jt.setBounds(370, 52, 251,167);
contentPane.add(jt);
jt.setModel(tableModel);
} catch (Exception e1) {
System.out.println(e1);
}
}
}
Don't use a null layout (get rid of that code)
Don't use setBounds(...) (get rid of that code)
Swing was designed to be used with layout managers. The layout manager will give the component a size/location based on the rules of the layout manager.
What I am actually missing are the names of the Columns.
In order for the columns names to appear the table must be added to the viewport of a JScrollPane and the scroll pane added to the frame.
So the basic change would be:
//contentPane.setLayout(null);
contentPane.setLayout(new BorderLayout());
and
//contentPane.add(jt);
contentPane.add(new JScrollPane(jt), BorderLayout.CENTER);
This will allow the table to fill the available space in frame. Scrollbars will appear as required. Read the section from the Swing tutorial on Layout Managers for more information and working examples.
Your question states, "JList", but your code shows a JTable -- this is a bit confusing
Having said that, you've a major problem here:
jt.setBounds(370, 52, 251,167);
Never restrict the size of an expanding component such as a JTable or a JTextArea since this prevents it from displaying within the JScrollPane as it should since it artificially restricts its size. If you are going to restrict the size of anything, it should be the JScrollPane's viewport, and only do this very carefully.
Next, you're adding the JTable itself to the GUI and should be adding JTable to a JScrollPane, actually to the JScrollPane's viewport, and then add the containing JScrollPane to the GUI.
e.g.,
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
// jt.setBounds(370, 52, 251,167);
JScrollPane scrollPane = new JScrollPane(jt);
// contentPane.add(jt);
jt.setModel(tableModel);
contentPane.add(scrollPane);
I have this piece of code
String [] titles = {"حذف", "شماره درس","گروه","واحد","نام","تعداد ثبت نامی","نام استاد","زمان امتحان","زمان کلاس","وضعیت پیشنیازی ها","محدودیت"};
String[][] value = new String[1][11];
JTable table = new JTable(value, titles);
table.setBounds(800,450,1100,80);
table.setBorder(BorderFactory.createLineBorder(Color.black, 2, true));
table.setRowHeight(40);
table.setForeground(Color.BLACK);
table.setBackground(Color.lightGray);
mainPanel.add(table);
but it doesn't work :/
It displays this while columns should have name :/
How can I fix this?
mainPanel.add(table);
The table header is only displayed automatically when you add the table to a scroll pane.
The code should be:
//mainPanel.add(table);
JScrollPane scrollPane = new JScrollPane( table );
mainPanel.add( scrollPane );
I have a JPanel with Constraint's of Y_Axis so that whenever I add a new Component it will automatically be Added on a new Line.But the Problem is that the Label inside is not Aligned to Left or Right. It is displayed at some distance above the JTable. How can JLabel be displayed at desired Alginment.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
Then I added a JLabel inside panel.
JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
panel.add(labelSemester);
After label, I added a new JTable inside panel,
// Column Names for the Table
Object[] col_names = {"ID", "Name", "CH", "Marks", "Grade"};
// row data for the table
Object[][] table_rows = {{"CS123","Introduction to Computing",3,80,"A-"}};// One row only
JTable table = new JTable(table_rows, col_names);
panel.add(new JScrollPane(table));
Then I added a JFrame and added the Panel to show in the frame
JFrame frame = new JFrame();
// frame Title
frame.setTitle("DMC");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// adding panel inside frame
frame.add(panel);
// displaying frame
frame.show()
Note:
I have added code for auto Adjustment of column width of JTable.
Output can be seen from attached Image
All components added to the BoxLayout need the same alignmentX, otherwise you can get some weird layouts:
//JLabel labelSemester = new JLabel("Semester 1: ",SwingConstants.LEFT);
JLabel labelSemester = new JLabel("Semester 1: ");
label.semester.setAlignmentX(JLabel.LEFT_ALIGNMENT);
panel.add(labelSemester);
...
JTable table = new JTable(table_rows, col_names);
//panel.add(new JScrollPane(table));
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
panel.add( scrollPane );
Read the section from the Swing BoxLayout tutorial on Fixing Alignment Problems for more information. Keep a link to the tutorial handy for all Swing basics.
Is there any way to change the size dimensions of a JOptionPane window in java? I tried looking around online and didn't see anything. It would be for a simple showInputDialog(); window.
I believe that a better approach is to consider to use JDialog instead JOptinionPane.
You could try something like this:
JScrollPane scrollpane = new JScrollPane();
String categories[] = {"Test", "Test1", "Test2", "Test3"};
JList list = new JList(categories);
scrollpane = new JScrollPane(list);
JPanel panel = new JPanel();
panel.add(scrollpane);
scrollpane.getViewport().add(list);
JOptionPane.showMessageDialog(null, scrollpane, "Test List", JOptionPane.PLAIN_MESSAGE);
I'm trying to populate a JTable from an ArrayList, The ArrayList is filled with data from my database.
this is the code I tried :
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new String[]{"Numéro d'ordre", "Article", "Quantité", "Remarque"});
for (gestionstock.LigneBonInterne o : listLigneBonInterne) {
model.addRow(new String[]{o.getNumOrder().toString(), o.getdesgArt(), o.getQte().toString(), o.getRemarque()});
System.out.println(o.toString());
}
jTable1.setModel(model);
But I get this error message :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at magasinier.BonInterneDetails.(BonInterneDetails.java:63)
the ligne 63 is : jTable1.setModel(model);
I did a test to see if the ArrayList is filled, and I found that the ArrayList is filled with records which means that there is no problem with filling the ArrayList
How can I solve this problem ?
EDIT :
I tried to create the JTable using code and assign it to ScrollPane :
JTable jTable1 = new JTable(model);
jTable1.setModel(model);
jScrollPane1.setViewportView(jTable1);
But I still get the same error this time is the line : jScrollPane1.setViewportView(jTable1);
Initialize the JTable jTable1 prior to setting the TableModel
jTable1 = new JTable(model);
jTable1.setModel(model);
In Netbeans palette you can specify some custom post-initialization code or use a custom constructor.