How To Create And Add JMenu To JMenuBar Using MySQL And Java NetBeans
In this Java Tutorial we will see How To Create A JMenu Dynamically From MySQL Database By Geting Image And Text From Database And Set Them Into JMenu In Java NetBeans .
Project Source Code:
// get connection to the database
public Connection getConnection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/s_t_d", "root", "");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
return con;
}
public void _addMenu(){
Connection con = getConnection();
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM mypics");
while(rs.next()){
// get the image from database
// resize the image
// create the jmenu and add the image to it
ImageIcon img1 = new ImageIcon(rs.getBytes("pic"));
Image img2 = img1.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
ImageIcon img3 = new ImageIcon(img2);
JMenu jm = new JMenu(rs.getString("name"));
jm.setIcon(img3);
// add the jmenu to the jmenubar
jMenuBar1.add(jm);
}
} catch (SQLException ex) {
Logger.getLogger(JMenu_From_MySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
1 comments:
commentsin this video, only menu and sub menu have displayed, how to perform and open particular frame on menu click
Reply