Skip to main content

How to use JComboBox, JList, JTree, JTable with Model class

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
nttaivn
Offline
Joined: 2013-08-17

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
arnavkumartechno
Offline
Joined: 2013-10-25

Hope these examples will be helpfull for you.
jTabel example:

package swingdemo;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JtableDemo extends JFrame {
private JScrollPane jScrollPane1 = new JScrollPane();
private JTable jTable1 = new JTable();
private JButton jButton1 = new JButton();
DefaultTableModel model = null;
private JButton jButton2 = new JButton();

public JtableDemo() {

Vector rowOne = new Vector();
rowOne.addElement("Ram");
rowOne.addElement("21");
rowOne.addElement("Delhi");
Vector rowData = new Vector();
rowData.addElement(rowOne);
Vector rowTwo = null;
for(int i = 2;i<=20; i++) {
rowTwo = new Vector();
rowTwo.addElement("Ram"+i);
rowTwo.addElement(""+(21+i));
rowTwo.addElement("Delhi"+i);
rowData.add(rowTwo);
}

rowData.addElement(rowTwo);

Vector columnNames = new Vector();
columnNames.addElement("Name");
columnNames.addElement("Age");
columnNames.addElement("Address");
model = new DefaultTableModel(rowData,columnNames);
jTable1 = new JTable(model);
this.getContentPane().setLayout( null );
this.setSize(new Dimension(616, 392));
jScrollPane1.setBounds(new Rectangle(15, 30, 360, 205));
jButton1.setText("Add");
jButton1.setBounds(new Rectangle(75, 300, 160, 35));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("Remove");
jButton2.setBounds(new Rectangle(290, 295, 125, 40));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
}
public static void main(String[] args) {
JtableDemo obj =new JtableDemo();
obj.setVisible(true);
}

private void jButton1_actionPerformed(ActionEvent e) {
Vector rowOne = new Vector();
rowOne.addElement("Shyam");
rowOne.addElement("22");
rowOne.addElement("Roop Nagar");
model.addRow(rowOne);
}

private void jButton2_actionPerformed(ActionEvent e) {
model.removeRow(0);
}
}

MultipleControls example:

package swingdemo;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;

public class MultipleControls extends JFrame {
ButtonGroup bg = new ButtonGroup();
private JRadioButton jRadioButton1 = new JRadioButton();
private JRadioButton jRadioButton2 = new JRadioButton();
private JLabel jLabel1 = new JLabel();
private JRadioButton jRadioButton3 = new JRadioButton();
private JRadioButton jRadioButton4 = new JRadioButton();
private JLabel jLabel2 = new JLabel();
private JButton jButton1 = new JButton();

public MultipleControls() {

this.getContentPane().setLayout( null );
this.setSize(new Dimension(585, 370));
this.setBackground(Color.white);
this.setTitle("Quiz-2013");
this.setResizable(false);
bg.add(jRadioButton1);
bg.add(jRadioButton2);
bg.add(jRadioButton3);
bg.add(jRadioButton4);
jRadioButton1.setText("Java Framework Classes");
jRadioButton1.setBounds(new Rectangle(30, 120, 240, 20));
jRadioButton1.setFont(new Font("Tahoma", 1, 16));
jRadioButton2.setText("Java Framework Components");
jRadioButton2.setBounds(new Rectangle(30, 165, 300, 20));
jRadioButton2.setFont(new Font("Tahoma", 1, 16));
jLabel1.setText(" JFC Stands For");
jLabel1.setBounds(new Rectangle(15, 40, 475, 55));
jLabel1.setFont(new Font("Tahoma", 0, 18));
jLabel1.setBorder(BorderFactory.createLineBorder(Color.black, 5));
jRadioButton3.setText("Java Foundation Classes");
jRadioButton3.setBounds(new Rectangle(30, 205, 260, 20));
jRadioButton3.setFont(new Font("Tahoma", 1, 16));
jRadioButton4.setText("None of These");
jRadioButton4.setBounds(new Rectangle(30, 240, 245, 30));
jRadioButton4.setFont(new Font("Tahoma", 1, 16));
jLabel2.setBounds(new Rectangle(15, 110, 485, 175));
jLabel2.setBorder(BorderFactory.createLineBorder(Color.black, 6));
jButton1.setText("Submit");
jButton1.setBounds(new Rectangle(335, 290, 165, 40));
jButton1.setFont(new Font("Tahoma", 1, 16));
jButton1.setToolTipText("Submit Your Answer");
jButton1.setMnemonic('S');
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jRadioButton4, null);
this.getContentPane().add(jRadioButton3, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jRadioButton2, null);
this.getContentPane().add(jRadioButton1, null);
}
public static void main(String[] args) {
MultipleControls mc = new MultipleControls();
mc.setVisible(true);
mc.setLocation(300,300);
}

private void jButton1_actionPerformed(ActionEvent e) {
if(jRadioButton3.isSelected()) {
JOptionPane.showMessageDialog(this,"Yes, This is Correct Answer","Quiz-2013",JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(this,"No, This is InCorrect Answer","Quiz-2013",JOptionPane.ERROR_MESSAGE);
}
}
}