Java scrollbar option
If you can I would like someone to help me make my java interface with a开发者_如何学C scrollbar option,so I can go up and down my window.
I need the code to add inside my current code and the specific place where to add it
Here is the code:( I cannot add the scroll pane:(
package sms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
public class SMS4 extends JFrame implements ActionListener {
JRadioButton entp = new JRadioButton("Enterprise");
JRadioButton linux = new JRadioButton("Linux");
JRadioButton tkodimi = new JRadioButton("TeoriKodimi");
JRadioButton ings = new JRadioButton("InxhinieriSofti");
JRadioButton cmp = new JRadioButton("Kompilator");
JButton save= new JButton("Save");
ButtonGroup btnG = new ButtonGroup();
JPanel pane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
private DefaultTableModel students;
private JTable table;
private DefaultTableModel students1;
private JTable table1;
private DefaultTableModel students2;
private JTable table2;
private DefaultTableModel students3;
private JTable table3;
private DefaultTableModel students4;
private JTable table4;
public SMS4() {
super("Lendet");
btnG.add(entp);
btnG.add(linux);
btnG.add(tkodimi);
btnG.add(ings);
btnG.add(cmp);
pane.add(entp);
pane.add(linux);
pane.add(tkodimi);
pane.add(ings);
pane.add(cmp);
pane.setLayout(new GridLayout(3, 3));
pane2.add(save, "Center");
save.addActionListener(this);
entp.addActionListener(this);
linux.addActionListener(this);
tkodimi.addActionListener(this);
ings.addActionListener(this);
cmp.addActionListener(this);
pane.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Zgjidh lenden:"));
pane1.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Tabela e notave:"));
pane2.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Ruaj:"));
// tabela e notave:
//tab 1:
students = new DefaultTableModel();
students.addColumn( "Emrat" );
students.addColumn( "Notat" );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
Statement st= cn.createStatement();
ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Arkitekture Aplikimi'");
if(rs!=null){
while(rs.next()){
String[] student= new String[2];
student[0]=rs.getString("Name");
student[1]=rs.getString("Nota");
students.addRow(student);
}
}
else System.out.println("Gabim.");
table = new JTable(students);
pane1.add( new JScrollPane(table),BorderLayout.CENTER);
table.setVisible(false);
}
catch(ClassNotFoundException e) {
System.err.println(e.getMessage());
}catch(SQLException e){
System.err.println(e.getMessage());
}
// end tabela
//tab 1:
students1 = new DefaultTableModel();
students1.addColumn( "Emrat" );
students1.addColumn( "Notat" );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn1 = DriverManager.getConnection("Jdbc:odbc:db");
Statement st1= cn1.createStatement();
ResultSet rs1= st1.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Linux'");
if(rs1!=null){
while(rs1.next()){
String[] student1= new String[2];
student1[0]=rs1.getString("Name");
student1[1]=rs1.getString("Nota");
students1.addRow(student1);
}
}
else System.out.println("Gabim.");
table1 = new JTable(students1);
pane1.add( new JScrollPane(table1),BorderLayout.CENTER);
table1.setVisible(false);
}
catch(ClassNotFoundException e) {
System.err.println(e.getMessage());
}catch(SQLException e){
System.err.println(e.getMessage());
}
// end tab1
//tab 2:
students2 = new DefaultTableModel();
students2.addColumn( "Emrat" );
students2.addColumn( "Notat" );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
Statement st= cn.createStatement();
ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Teori Kodimi'");
if(rs!=null){
while(rs.next()){
String[] student2= new String[2];
student2[0]=rs.getString("Name");
student2[1]=rs.getString("Nota");
students2.addRow(student2);
}
}
else System.out.println("Gabim.");
table2 = new JTable(students2);
pane1.add( new JScrollPane(table2),BorderLayout.CENTER);
table2.setVisible(false);
}
catch(ClassNotFoundException e) {
System.err.println(e.getMessage());
}catch(SQLException e){
System.err.println(e.getMessage());
}
// end tab2
//tab 3:
students3 = new DefaultTableModel();
students3.addColumn( "Emrat" );
students3.addColumn( "Notat" );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
Statement st= cn.createStatement();
ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Inxhinieri Softi'");
if(rs!=null){
while(rs.next()){
String[] student3= new String[2];
student3[0]=rs.getString("Name");
student3[1]=rs.getString("Nota");
students3.addRow(student3);
}
}
else System.out.println("Gabim.");
table3 = new JTable(students3);
pane1.add( new JScrollPane(table3),BorderLayout.CENTER);
table3.setVisible(false);
}
catch(ClassNotFoundException e) {
System.err.println(e.getMessage());
}catch(SQLException e){
System.err.println(e.getMessage());
}
// end tab3
//tab 4:
students4 = new DefaultTableModel();
students4.addColumn( "Emrat" );
students4.addColumn( "Notat" );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
Statement st= cn.createStatement();
ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Kompilatore'");
if(rs!=null){
while(rs.next()){
String[] student4= new String[2];
student4[0]=rs.getString("Name");
student4[1]=rs.getString("Nota");
students4.addRow(student4);
}
}
else System.out.println("Gabim.");
table4 = new JTable(students4);
pane1.add( new JScrollPane(table4),BorderLayout.CENTER);
table4.setVisible(false);
}
catch(ClassNotFoundException e) {
System.err.println(e.getMessage());
}catch(SQLException e){
System.err.println(e.getMessage());
}
// end tab4
Container paneg= getContentPane();
paneg.add(pane, "North");
paneg.add(pane1, "Center");
paneg.add(pane2, "South");
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(entp.isSelected() ){
students = new DefaultTableModel();
table.setVisible(true);
}
else if (linux.isSelected()) {
students1 = new DefaultTableModel();
table1.setVisible(true);
}
else if (tkodimi.isSelected()) {
students2 = new DefaultTableModel();
table2.setVisible(true);
}
else if (ings.isSelected())
{
students3 = new DefaultTableModel();
table3.setVisible(true);
}
else if (cmp.isSelected()) {
students4 = new DefaultTableModel();
table4.setVisible(true);
}
}
public void frame(){
SMS4 s4 = new SMS4();
s4.setSize(700,700);
s4.setVisible(true);
s4.setResizable(false);
s4.setLocation(320,400);
}
}
The JScrollPane class is what you are looking for. The JavaDocs tell you everything you need to know and even link to a simple to use tutorial.
Put your component inside a JScrollPane
.
Whatever item you wish to scroll, be in a JPanel
or a JTextArea
, you should add it to a JScrollPane
and then add the JScrollPane
to the panel or other container.
So find wherever you're doing this:
container.add(item);
Instead, do this:
JScrollPane pane = new JScrollPane(item); container.add(pane);
精彩评论