Bilal Ekrem Bilal Ekrem - 7 months ago 16
Java Question

Can't set to TabbedPane size in Java

I'm trying to create a

tabPane
and add tabs from my database. But I have a problem while setting size and location of
tabbedPane
. Here is my code ;

JPanel pnlTemp;
JTabbedPane tabPaneTemp;
JButton btnTemp;

int categoryCount= site.getCategoryCount();

try {

//sql. is a class which I created for connection to database.

ResultSet resultCategory= sql.select("SELECT * FROM CATEGORY");

for (int i = 0; i < categoryCount; i++) {
resultCategory.next();

pnlTemp = new JPanel();
tabPaneAnasayfa.addTab(resultCategory.getString("AD"), pnlTemp);

tabPaneTemp = new JTabbedPane();
pnlTemp.add(tabPaneTemp);

tabPaneTemp.setSize(950, 460);
tabPaneTemp.setBounds(35,22,950, 460);

tabPaneTemp.setTabPlacement(JTabbedPane.BOTTOM);

createButtonToTab(tabPaneTemp);


}

} catch (SQLException ex) {
Logger.getLogger(Anasayfa.class.getName()).log(Level.SEVERE, null, ex);
}



private void createButtonToTab(JTabbedPane tabPane) {
JPanel pnlTemp;
JButton btnTemp;

ResultSet resultHaber = sql.select("SELECT * FROM HABER");
ArrayList<Haber> haberList = new ArrayList();
try {
while (resultHaber.next()) {
int haberID;
haberID = resultHaber.getInt("ID");
haberList.add(site.getHaber(haberID));

}
} catch (SQLException ex) {
Logger.getLogger(Anasayfa.class
.getName()).log(Level.SEVERE, null, ex);
}

int listIndexi = 0;
int haberSirasi = 1;

int topHaberSayisi = haberList.size();
for (int i = 0; i < topHaberSayisi; i++) {

pnlTemp = new JPanel();
btnTemp = new JButton();

btnTemp.setSize(pnlTemp.getSize());

pnlTemp.setLayout(new GridLayout());

if (!haberList.get(listIndexi).haberSonDakikaMi) {
tabPane.addTab(String.valueOf(haberSirasi), pnlTemp);
Haber tempHaber;
tempHaber = haberList.get(listIndexi);

pnlTemp.add(btnTemp);
btnTemp.setText(haberList.get(listIndexi).haberBasligi);
haberSirasi++;

btnTemp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
haberOkuCagir(tempHaber.haberID);
}
});
}
listIndexi++;

}
}


Here is the result:

enter image description here

Answer

pnlTemp = new JPanel(); default layout of JPanel is FlowLayout. So in FlowLayout you can not set positions by setBounds(). You can learn how to use layout managers here. Pick one of them to implement your desired design.