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);
Related
The following program segment generates the output within the red box as shown in the figure:
st = c.con.createStatement();
rs = st.executeQuery("SELECT itemName, itemMaxQty, itemImage FROM item");
while(rs.next()){
JPanel itemPanel = new JPanel();
itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.PAGE_AXIS));
JPanel componentPanel = new JPanel();
componentPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel itemName = new JLabel(rs.getString(1));
ImageIcon itemImage = new ImageIcon(rs.getString(3));
Image scaleImage = itemImage.getImage().getScaledInstance(120, 120, Image.SCALE_SMOOTH);
JLabel imageHolder = new JLabel(new ImageIcon(scaleImage));
JSpinner quantityField = new JSpinner();
Dimension d = quantityField.getPreferredSize();
d.width = 60;
d.height = 30;
quantityField.setPreferredSize(d);
componentPanel.add(imageHolder);
componentPanel.add(quantityField);
itemPanel.add(componentPanel);
itemPanel.add(itemName);
add(itemPanel);
}
I know that probably some parts of the code are not the best way to do it, but that is what I can do for now in line with the knowledge that I have.
I actually have two problems here.
First, I don’t understand why the itemName's value (Cupcake Stand, chafers, etc) are centred.
1. How do I make it left centred such that it is aligned with its corresponding picture on top?
Second, as you can see, there is a large space in between the componentPanel (the picture and the JSpinner) and the itemName. I think it’s because the height allotted for itemName is the same as that for the componentPanel. So
2. How do I make itemName immediately below the componentPanel?
You can do that by changing the LayoutManager of itemPanel to BorderLayout (with that line itemPanel.setLayout(new BorderLayout());), and add the component to itemPanel in that way:
itemPanel.add(componentPanel, BorderLayout.CENTER);
itemPanel.add(itemName, BorderLayout.SOUTH);
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.
I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.
My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.
Here's my code, the second image is created by not adding headerPanel:
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
this.add(headerPanel);
this.add(descLabel);
This class extends JPanel, and is added to a JFrame, which is simply pack()'d
Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
descPanel.add(descLabel);// added
this.add(headerPanel);
this.add(descPanel);// modified
My problem is the margin to the left of the large lipsum box, how can I get rid of this?
You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.
I'm guessing the JLabel is center aligned so you need to use:
descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.
I wrote a little code to see how the scroll Pane functions but my code never worked.
here's the code,
public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work??
Any help explaining how scrollpane works?
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.
content.add(textArea);
content.add(imad);
A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.
Try something like:
JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.
I've written a code which displays search results which are images in JTable..
The main problem is that even after adding JScrollbar, the scorll bar does not appear even when its needed...
Here is a snapshot of my code..
this.queryName = qname;
JFrame frame = new JFrame(title);
frame.setLayout(new GridLayout(2, 1)); //.. UPPER FOR QUERY AND LOWER FOR RESULT
this.ijtable = new ImageJTable(result, Setting.getColumnCount());
ijtable.setVisible(true);
JScrollPane pane = new JScrollPane(ijtable);
JLabel query = new JLabel(new ImageIcon(ImageResize.resize(queryName, Setting.getQueryWidth(), Setting.getQueryWidth())));
query.setVisible(true);
frame.add(query);
frame.add(ijtable);
frame.add(pane);
frame.setSize(Setting.getFrameWidth(), Setting.getFrameHeight());
frame.setVisible(true);
It's probably because you are adding both ijtable and pane. Only add pane because that control has ijtable inside it.
frame.add(ijtable);
Try removing this line. By adding the table to the frame, you're removing it from the scroll pane.