Forum » Programiranje » JTable preko JButtona (Java)
JTable preko JButtona (Java)
PentiumD ::
Prosim, če mi lahko nekdo pomaga glede odpiranja neke tabele (preko gumba) v Javi. Rad bi, da se mi preko gumba aktivira izpis neke preproste tabele na isti (osnovni) formi kot je ta gumb. Da ne odprira nove forme ipd. S spodnjo kodo se mi odpira nova forma in preko gumba se ne izpiše želena tabela. Kaj je narobe? Nekaj ne dela dobro. Prosim za pomoč.
Hvala za odgovore!
Tukaj je koda (jDeveloper):
package mypackage1;
import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.table.TableModel;
public class Frame1 extends JFrame
{
private JButton jButton1 = new JButton();
public Frame1()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
Object rows[][] = { { "11", "12", "13" },
{ "21", "22", "23" },
{ "31", "32", "33" },
{ "41", "42", "43" },
{ "51", "52", "53" },
{ "61", "62", "63" },
{ "71", "72", "73" },
{ "81", "82", "83" },
{ "91", "92", "93" } };
Object columns[] = { "Stolpec1", "Stolpec2", "Stolpec3" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
content.add(scrollPane, BorderLayout.CENTER);
this.setSize(350, 100);
this.setVisible(true);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jButton1.setText("Tabela");
jButton1.setBounds(new Rectangle(0, 205, 80, 20));
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jButton1, null);
}
private void jButton1_actionPerformed(ActionEvent e)
{
Frame1 frame1 = new Frame1();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
Object rows[][] = { { "11", "12", "13" },
{ "121", "122", "123" },
{ "131", "132", "133" },
{ "141", "142", "143" },
{ "151", "152", "153" },
{ "161", "162", "163" },
{ "171", "172", "173" },
{ "181", "182", "183" },
{ "191", "192", "193" } };
Object columns[] = { "Stolpec1", "Stolpec2", "Stolpec3" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
content.add(scrollPane, BorderLayout.CENTER);
this.setSize(350, 200);
this.setVisible(true);
}
}
Hvala za odgovore!
Tukaj je koda (jDeveloper):
package mypackage1;
import java.util.ArrayList;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.table.TableModel;
public class Frame1 extends JFrame
{
private JButton jButton1 = new JButton();
public Frame1()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
Object rows[][] = { { "11", "12", "13" },
{ "21", "22", "23" },
{ "31", "32", "33" },
{ "41", "42", "43" },
{ "51", "52", "53" },
{ "61", "62", "63" },
{ "71", "72", "73" },
{ "81", "82", "83" },
{ "91", "92", "93" } };
Object columns[] = { "Stolpec1", "Stolpec2", "Stolpec3" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
content.add(scrollPane, BorderLayout.CENTER);
this.setSize(350, 100);
this.setVisible(true);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jButton1.setText("Tabela");
jButton1.setBounds(new Rectangle(0, 205, 80, 20));
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jButton1, null);
}
private void jButton1_actionPerformed(ActionEvent e)
{
Frame1 frame1 = new Frame1();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = this.getContentPane();
Object rows[][] = { { "11", "12", "13" },
{ "121", "122", "123" },
{ "131", "132", "133" },
{ "141", "142", "143" },
{ "151", "152", "153" },
{ "161", "162", "163" },
{ "171", "172", "173" },
{ "181", "182", "183" },
{ "191", "192", "193" } };
Object columns[] = { "Stolpec1", "Stolpec2", "Stolpec3" };
JTable table = new JTable(rows, columns);
JScrollPane scrollPane = new JScrollPane(table);
content.add(scrollPane, BorderLayout.CENTER);
this.setSize(350, 200);
this.setVisible(true);
}
}
]Fusion[ ::
Logično če pod private void jButton1_actionPerformed(ActionEvent e) narediš novo okno, da boš to tudi dobil.
Lahko pa probaš z CardLyout-om naredit da imaš vse v originalnem oknu. Pač narediš več Panel-ov.
Pa kolko vidim ob kliku na gumb še enkrat inicializiraš tabelo po nepotrebnem, saj je že prej. Edino kaj moraš tam naredit je napolnit z podatki.
To pa ti ne morem pomagat, ker še nisem nič z JTable delal. Probaj google.
LP
Lahko pa probaš z CardLyout-om naredit da imaš vse v originalnem oknu. Pač narediš več Panel-ov.
Pa kolko vidim ob kliku na gumb še enkrat inicializiraš tabelo po nepotrebnem, saj je že prej. Edino kaj moraš tam naredit je napolnit z podatki.
To pa ti ne morem pomagat, ker še nisem nič z JTable delal. Probaj google.
LP
"I am not an animal! I am a human being! I... am... a man!" - John Merrick
l0g1t3ch ::
evo jest mam tkole razred tabela, ki kreira tabelo in jo napolni z podatki, extenda pa class JPanel
v glavnem oknu pa takole potem prikažem tabelo da je na isti formi in ne v svojem oknu. Tole se izvede ko kliknem na gumb.
import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import java.awt.Component; import java.awt.Dimension; import java.awt.GridLayout; import java.util.Vector; import javax.swing.*; import java.awt.*; import java.text.*; import java.awt.event.*; import javax.swing.event.*; public class Tabela extends JPanel { public static Vector<Vnos> seznam = new Vector<Vnos>(); private JTable table; private Vnos vn; ListSelectionModel listSelectionModel; private static int index = -1; public Tabela() { super(new GridLayout(1,0)); table = new JTable(new MyTableModel()); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setPreferredScrollableViewportSize(new Dimension(900, 150)); listSelectionModel = table.getSelectionModel(); //dodajanje poslusalca, da dobimo kateri element smo kliknili listSelectionModel.addListSelectionListener( new TableSelectionHandler() ); table.setSelectionModel(listSelectionModel); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Set up column sizes. initColumnSizes(table); //Add the scroll pane to this panel. add(scrollPane); } private void initColumnSizes(JTable table) { TableColumn column = null; int headerWidth = 0; int cellWidth = 0; for (int i = 0; i <= 54; i++) { column = table.getColumnModel().getColumn(i); headerWidth = 200; cellWidth = 200; column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } } class MyTableModel extends AbstractTableModel { //imena stolpcev private String[] columnNames = {"D.N.", "Naročnik", "Ovitek","Notranjost","Rok Izdelave"}; private Object[][] data = getVektor(); //podatki private Object[][] getVektor() //metoda vrne tabelo objektov, ki predstavlja podatke v nasi tabeli { int vrstica = seznam.size(); Object[][] temp = new Object[vrstica][58]; for(int i = seznam.size()-1; i>=0; i--) { vn = seznam.elementAt(i); temp[i][0] = new Integer(vn.getDN()); temp[i][1] = " " + vn.getNarocnik(); temp[i][2] = new Boolean(vn.getOvitek()); temp[i][3] = new Boolean(vn.getNotranjost()); temp[i][4] = " " + vn.getRok(); } return temp; } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } } class TableSelectionHandler implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel)e.getSource(); index = lsm.getMinSelectionIndex(); //index vrstice, ki jo kliknemo } } public static int getIndex() //vrnemo index vrstice na katero smo kliknili { return index; } }
v glavnem oknu pa takole potem prikažem tabelo da je na isti formi in ne v svojem oknu. Tole se izvede ko kliknem na gumb.
private Tabela tabela; //deklaracija to je na začetku razrede ne v kodi, ki se izvede ob kliku private Container kontainer; kontainer = getContentPane(); //kontajner, na katerega "lepimo" gumbe,... kontainer.setLayout(null); //sami bomo razporejevali stvari tabela = new Tabela(); //tele 4 vrstice se izvedejo ob kliku tabela.setBounds(20, 500, 900, 150); //velikost,pozicija kontainer.add(tabela); setVisible(true);
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | Vprašanje iz JaveOddelek: Programiranje | 1154 (894) | illion |
» | Vodni udar (java)Oddelek: Programiranje | 1400 (1098) | Nuke_H2 |
» | [Java] Zagon appleta v HTMLOddelek: Programiranje | 1549 (1371) | Bela01 |
» | [JAVA] branje iz datotekeOddelek: Programiranje | 2417 (2052) | Bela01 |
» | [Java]VprašanjaOddelek: Programiranje | 2114 (1582) | Bela01 |