How to set Jtable name of columns - java

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 );

Related

Scrollbar for JTable

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);

Aligning JLabel to Left or Right inside BoxLayout with Y_AXIS Constraint of JPanel

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.

Inserting a JList into an existing JFrame

I have a JFrame that looks like:
The user enters some command into JTextArea2 and the result is shown into JTextArea1.
Now, for a specific command entered I want to create and "insert" the JList somewhere inside the JTextArea1.
It would look like:
Here is a little bit of code to show how I initially place them. How could I do that?
setLayout(new FlowLayout());
textArea_1 = new JTextArea(25,112);
textArea_1.setEditable(false);
scroll_1 = new JScrollPane(textArea_1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
textArea_2 = new JTextArea(1,111);
scroll_2 = new JScrollPane(textArea_2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll_2);

Adding JTable to a JScrollPane

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);

Multiple JTables in JPanel using Miglayout

I'm beeginer in MigLayout so, I need to add multiples JTables in one JPanel, but when I try to add more than one table, just the last table appears, and the others is marked just the JScrollPane border. My code is in below.
Test() {
//Panels
JPanel globalPanel = new JPanel(new MigLayout("fillx","[]","[]50[]"));
JPanel topPanel = new JPanel (new MigLayout("fillx","40px[]15[grow]","40px[]"));
JPanel tablePanel = new JPanel (new MigLayout("fillx","[center]","[]"));
//Components
JComboBox boxProj;
JTable table;
JScrollPane scroll;
//Top Panel
topPanel.add(new JLabel("Project Name:"));
String listString[] = {"test"};
boxProj= new JComboBox(listString);
topPanel.add(boxProj);
//Table Panel
//Tables
table = new JTable();
createTable(table); //my table
//Adding Multiples Tables
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
//Scroll to TablePanel
scroll = new JScrollPane(tablePanel);
scroll.setBorder(BorderFactory.createTitledBorder(null, "Project", TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLACK));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Global Panel
globalPanel.add(topPanel, "dock north");
JSeparator separator = new JSeparator();
globalPanel.add(separator,"growx");
globalPanel.add(scroll,"dock south, growx");
getContentPane().add(globalPanel);
pack();
setSize(1024,768);
}
If I made some mistake, correct me please.
Thank you!!
Any Swing component can only have one parent. Here you are adding the same JTable to 2 different JScrollPane containers. The result is that only the last one will be displayed. For 2 JTable components to appear you have to create 2 separate components.
table2 = new JTable();
...
tablePanel.add(new JScrollPane(table2), "growx,wrap,hmax 300");
It would seem that you are trying to add the same component twice. You can only have a component visible in one container:
table = new JTable();
createTable(table); //my table
//Adding Multiples Tables
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
Try with:
JTable table1 = new JTable();
JTable table2 = new JTable();
createTable(table1); //my table
createTable(table2);
//Adding Multiples Tables
tablePanel.add( new JScrollPane(table1),"growx,wrap,hmax 300");
tablePanel.add( new JScrollPane(table2),"growx,wrap,hmax 300");

Categories