Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can anyone help me?
I try to use key released for searching in the table with check box in java swing, but when I type a character the check box disappears
I use this code for the key released:
Jtable.setModel(new DefaultTableModel(null, new Object[]{"Name","code","section","credit","type","hours","semestre","null"}));
How can I keep the check box when I search for a value in this table?????
Don't use a KeyListener on a text component, it's just an inappropriate usage.
Instead, use DocumentListener on the underlying Document, for example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private JTextField searchField;
private SubjectTableModel tableModel;
public TestPane() {
setLayout(new BorderLayout());
JPanel searchPanel = new JPanel(new GridBagLayout());
searchField = new JTextField(20);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(8, 8, 8, 8);
searchPanel.add(new JLabel("Enter Value to Search:"), gbc);
gbc.fill = gbc.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx++;
searchPanel.add(searchField, gbc);
add(searchPanel, BorderLayout.NORTH);
List<SubjectTabelItem> items = new ArrayList<>(25);
items.add(new SubjectTabelItem("Chemistry", "CH3305", "English", 6, "Mandatory", 20, "S3"));
items.add(new SubjectTabelItem("Data", "I2206", "French", 5, "Mandatory", 50, "S4"));
items.add(new SubjectTabelItem("Java", "I2211", "French", 5, "Mandatory", 45, "S4"));
items.add(new SubjectTabelItem("Theorie", "Th3306", "French", 6, "Optional", 20, "S3"));
items.add(new SubjectTabelItem("Arabic", "A1265", "French", 3, "Optional", 20, "S4"));
items.add(new SubjectTabelItem("Html", "H102", "English", 4, "Mandatory", 12, "S1"));
items.add(new SubjectTabelItem("Php", "PI3301", "English", 3, "Mandatory", 25, "S3"));
items.add(new SubjectTabelItem("Assembly", "I3564", "English", 3, "Optional", 20, "S5"));
items.add(new SubjectTabelItem("Go", "I1234", "English", 3, "Mandatory", 20, "S5"));
tableModel = new SubjectTableModel(items);
table = new JTable(tableModel);
add(new JScrollPane(table));
searchField.getDocument().addDocumentListener(new DocumentListener() {
protected void process(DocumentEvent e) {
try {
Document document = e.getDocument();
String text = document.getText(0, document.getLength());
find(text);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
#Override
public void insertUpdate(DocumentEvent e) {
process(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
process(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
process(e);
}
});
}
protected void find(String text) {
tableModel.highlight(text);
}
}
public class Stubject {
private String name;
private String code;
private String section;
private int credit;
private String type;
private int hours;
private String semestre;
public Stubject(String name, String code, String section, int credit, String type, int hours, String semestre) {
this.name = name;
this.code = code;
this.section = section;
this.credit = credit;
this.type = type;
this.hours = hours;
this.semestre = semestre;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
public String getSection() {
return section;
}
public int getCredit() {
return credit;
}
public String getType() {
return type;
}
public int getHours() {
return hours;
}
public String getSemestre() {
return semestre;
}
}
public class SubjectTabelItem extends Stubject {
private boolean selected = false;
public SubjectTabelItem(String name, String code, String section, int credit, String type, int hours, String semestre) {
super(name, code, section, credit, type, hours, semestre);
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean isSelected) {
this.selected = isSelected;
}
}
public class SubjectTableModel extends AbstractTableModel {
private List<SubjectTabelItem> items;
private String[] columnNames = new String[]{
"Name",
"Code",
"Section",
"Credit",
"Type",
"Hours",
"Semestre",
""
};
public SubjectTableModel(List<SubjectTabelItem> items) {
this.items = items;
}
public void setItems(List<SubjectTabelItem> items) {
this.items = items;
}
#Override
public int getRowCount() {
return items.size();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
case 1:
case 2:
case 4:
case 6:
return String.class;
case 3:
case 5:
return Integer.class;
case 7:
return Boolean.class;
}
return String.class;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
SubjectTabelItem item = items.get(rowIndex);
switch (columnIndex) {
case 0:
return item.getName();
case 1:
return item.getCode();
case 2:
return item.getSection();
case 3:
return item.getCredit();
case 4:
return item.getType();
case 5:
return item.getHours();
case 6:
return item.getSemestre();
case 7:
return item.isSelected();
}
return null;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
SubjectTabelItem item = items.get(rowIndex);
switch (columnIndex) {
case 7:
if (aValue instanceof Boolean) {
boolean selected = (boolean) aValue;
if (selected != item.isSelected()) {
item.setSelected(selected);
fireTableCellUpdated(rowIndex, columnIndex);
}
}
break;
}
}
public void highlight(String text) {
String lowerCased = text.toLowerCase();
for (int row = 0; row < getRowCount(); row++) {
SubjectTabelItem item = items.get(row);
if (item.getName().toLowerCase().contains(lowerCased)) {
setValueAt(true, row, 7);
} else {
setValueAt(false, row, 7);
}
}
}
}
}
See How to Write a Document Listener for more details
Related
I have created a JTable with some information about formula 1 racing car drivers. The below code is for the JTable
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.event.*;
public class Test1 extends JFrame implements ActionListener {
JButton button;
Test1() {
//setBounds(100, 100, 500, 400);
JFrame frame = new JFrame();
button = new JButton();
//button.setBounds(50, 50, 20, 10);s
button.setText("Random Race");
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new Model1Data());
frame.setBounds(100, 100, 500, 400);
JPanel panel = new JPanel();
frame.add(panel);
panel.add(new JScrollPane(table));
panel.add(button);
//add(new J
frame.setVisible(true);
//this.add(button);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
Formula1ChampionsManager d= new Formula1ChampionsManager();
d.button();
}
}
This is the code for the Model1Data. This is the code for the table to update its cells.
import javax.swing.table.AbstractTableModel;
public class Model1Data extends AbstractTableModel implements ChampionsManager {
String colNames[] = { "Name", "Team", "No of first Places", "Total Points" };
Class<?> colClasses[] = { String.class, String.class, Integer.class, Integer.class };
public int getRowCount() {
return myDrivers.size();
}
public int getColumnCount() {
return colNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return myDrivers.get(rowIndex).getName();
}
if (columnIndex == 1) {
return myDrivers.get(rowIndex).getTeam();
}
if (columnIndex == 2) {
return myDrivers.get(rowIndex).getfirstPlace();
}
if (columnIndex == 3) {
return myDrivers.get(rowIndex).totalPoints();
}
return null;
}
public String getColumnName(int columnIndex) {
return colNames[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return colClasses[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
myDrivers.get(rowIndex).setName((String) aValue);
}
if (columnIndex == 1) {
myDrivers.get(rowIndex).setTeam((String) aValue);
}
if (columnIndex == 2) {
myDrivers.get(rowIndex).setfirstPlace((Integer) aValue);
}
if (columnIndex == 3) {
myDrivers.get(rowIndex).settotalPoints((Integer) aValue);
}
//fireTableCellUpdated(rowIndex, columnIndex);
}
}
This what the table GUI looks like Table GUI. When I click the button the values for No of first places and Total Points are changed randomly. But the table doesn't get updated with the values. If I pull on the side of the frame it gets updated. How do I get it to update when I click the button?
The JTable listens for changes in the TableModel. Hence class AbstractTableModel has methods such as fireTableCellUpdated(rowIndex, columnIndex) (which you commented out in your code - why?). If you override method setValueAt then you need to call that method, otherwise the JTable will not update when its data is changed. I'm guessing that changing the size of the JFrame causes a repaint of the JTable which causes the JTable to reload the data from its model (but I could be wrong).
Your class Model1Data should extend DefaultTableModel, rather than AbstractTableModel, since that class already contains all the functionality you require. The only method that class Model1Data needs to override is getColumnClass (which it already does). You also need to override at least one constructor. Since you didn't post a minimal, reproducible example, I'm guessing that this constructor may be appropriate. Hence the code for class Model1Data should be:
public class Model1Data extends javax.swing.table.DefaultTableModel {
Class<?> colClasses[] = { String.class, String.class, Integer.class, Integer.class };
public Model1Data(Object[] columnNames, int rowCount) {
super(columnNames, rowCount);
}
public Class<?> getColumnClass(int columnIndex) {
return colClasses[columnIndex];
}
}
And in class Test1 (which does not need to extend JFrame, by the way) you can create the JTable like so:
JTable table = new JTable(new Model1Data(new String[]{ "Name", "Team", "No of first Places", "Total Points" }, 0));
Edit
Here's my take on what you are trying to do. The below code can be copied as is, compiled and run. Just click on the Random Race button and the data in the JTable will update.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test1 {
private JTable table;
private void buildAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTable(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JButton createButton(String text, int mnemonic, ActionListener listener) {
JButton button = new JButton(text);
button.setMnemonic(mnemonic);
button.addActionListener(listener);
return button;
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(createButton("Random Race", KeyEvent.VK_R, this::randomRace));
return buttonsPanel;
}
private JScrollPane createTable() {
table = new JTable(new Model1Data());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
/**
* Simulates a race and randomly determines placings of the drivers and assigns appropriate
* points scores.
*
* #param event - encapsulates button press event
*/
private void randomRace(ActionEvent event) {
Formula1ChampionsManager.simulate();
TableModel tm = table.getModel();
if (tm instanceof Model1Data) {
Model1Data model = (Model1Data) tm;
model.setRowCount(0);
model.init();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Test1().buildAndDisplayGui();
});
}
}
class Model1Data extends DefaultTableModel {
private static final String[] COLUMN_NAMES = {"Name", "Team", "No of first Places", "Total Points"};
public Model1Data() {
super(COLUMN_NAMES, 0);
init();
}
public void addDriver(F1Driver driver) {
addRow(new Object[]{driver.getName(),
driver.getTeam(),
driver.getFirstPlace(),
driver.getTotalPoints()});
}
public void init() {
Formula1ChampionsManager.getDrivers()
.stream()
.forEach(this::addDriver);
}
}
class Formula1ChampionsManager {
private static List<F1Driver> drivers;
static {
drivers = List.of(new F1Driver("Lewis Hamilton", F1Teams.MERCEDES),
new F1Driver("Max Verstappen",F1Teams.RED_BULL),
new F1Driver("Charles Leclerc",F1Teams.FERRARI),
new F1Driver("Lando Norris",F1Teams.MCLAREN),
new F1Driver("Fernando Alonso",F1Teams.ALPINE),
new F1Driver("Pierre Gasly",F1Teams.ALPHATAURI),
new F1Driver("Sebastian Vettel",F1Teams.ASTON_MARTIN),
new F1Driver("Nicholas Latifi",F1Teams.WILLIAMS),
new F1Driver("Valtteri Bottas",F1Teams.ALFA_ROMEO),
new F1Driver("Mick Schumacher",F1Teams.HAAS));
}
public static List<F1Driver> getDrivers() {
return drivers;
}
public static void simulate() {
List<F1Driver> result = new ArrayList<>(drivers);
Collections.shuffle(result);
F1Driver first = result.get(0);
first.update(F1Driver.FIRST);
F1Driver second = result.get(1);
second.update(F1Driver.SECOND);
F1Driver third = result.get(2);
third.update(F1Driver.THIRD);
F1Driver fourth = result.get(3);
fourth.update(F1Driver.FOURTH);
F1Driver fifth = result.get(4);
fifth.update(F1Driver.FIFTH);
F1Driver sixth = result.get(5);
sixth.update(F1Driver.SIXTH);
F1Driver seventh = result.get(6);
seventh.update(F1Driver.SEVENTH);
F1Driver eighth = result.get(7);
eighth.update(F1Driver.EIGHTH);
F1Driver ninth = result.get(8);
ninth.update(F1Driver.NINTH);
F1Driver tenth = result.get(9);
tenth.update(F1Driver.TENTH);
}
}
enum F1Teams {
MERCEDES("Mercedes"),
RED_BULL("Red Bull"),
FERRARI("Ferrari"),
MCLAREN("McLaren"),
ALPINE("Alpine"),
ALPHATAURI("AlphaTauri"),
ASTON_MARTIN("Aston Martin"),
WILLIAMS("Williams"),
ALFA_ROMEO("Alfa Romeo"),
HAAS("Haas");
private String name;
private F1Teams(String name) {
this.name = name;
}
#Override // java.lang.Enum
public String toString() {
return name;
}
}
class F1Driver {
public static final int FIRST = 25;
public static final int SECOND = 18;
public static final int THIRD = 15;
public static final int FOURTH = 12;
public static final int FIFTH = 10;
public static final int SIXTH = 8;
public static final int SEVENTH = 6;
public static final int EIGHTH = 4;
public static final int NINTH = 2;
public static final int TENTH = 1;
private String name;
private F1Teams team;
private int firstPlace;
private int totalPoints;
public F1Driver(String name, F1Teams team) {
this(name, team, 0, 0);
}
public F1Driver(String name, F1Teams team, int firstPlace, int totalPoints) {
Objects.requireNonNull(name, "Missing name.");
this.name = name;
this.team = team;
this.firstPlace = firstPlace;
this.totalPoints = totalPoints;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public F1Teams getTeam() {
return team;
}
public void setTeam(F1Teams team) {
this.team = team;
}
public int getFirstPlace() {
return firstPlace;
}
public void setFirstPlace(int firstPlace) {
this.firstPlace = firstPlace;
}
public int getTotalPoints() {
return totalPoints;
}
public void setTotalPoints(int totalPoints) {
this.totalPoints = totalPoints;
}
#Override // java.lang.Object
public boolean equals(Object obj) {
boolean equal = this == obj;
if (!equal) {
if (obj instanceof F1Driver) {
F1Driver other = (F1Driver) obj;
equal = name != null &&
name.equals(other.name) &&
team == other.team;
}
}
return equal;
}
#Override // java.lang.Object
public int hashCode() {
return (String.valueOf(name) + String.valueOf(team)).hashCode();
}
#Override // java.lang.Object
public String toString() {
return String.format("%s [%s] %d [%d]", name, team, totalPoints, firstPlace);
}
public void update(int points) {
totalPoints += points;
if (points == FIRST) {
firstPlace++;
}
}
}
Here's how it looks when I run the above code.
What should I do:java&Netbeans, MySQL
this array to Show Contacts Information in the Jtable of the Form When login with the account
ContactQuery cq=new ContactQuery();
ArrayList<Contact> ctList=cq.contactList();
//`id`, `fname`, `lname`, `groupc`, `phone`, `email`, `address`, `picture`
String[] colNames={"Id","First Name ","Last Name","Group","Phone","Email","Address","Picture"};
Object[][] rows=new Object [ctList.size()][8];
for(int i=0;i<ctList.size();i++)
{
rows[i][0]=ctList.get(i).getId();
rows[i][1]=ctList.get(i).getFname();
rows[i][2]=ctList.get(i).getLname();
rows[i][3]=ctList.get(i).getGroupc();
rows[i][4]=ctList.get(i).getPhone();
rows[i][5]=ctList.get(i).getEmail();
rows[i][6]=ctList.get(i).getAddress();
// this show only the (javax.swing.ImageIcon)not the image :What should i do here ?
ImageIcon pic= new ImageIcon(new ImageIcon(ctList.get(i).getPicture()).getImage().getScaledInstance(150, 100, Image.SCALE_SMOOTH));
rows[i][7]=pic;
}
MyModel mmd=new MyModel(rows, colNames);
jTable1.setModel(mmd);
jTable1.setRowHeight(100);
jTable1.getColumnModel().getColumn(7).setPreferredWidth(150);
I don't know what you're "particular" problem is, there's not enough context to ascertain exactly where you might be having problems, but, I suspect that you're not overriding the getColumnClass method of the AbstractTableModel, so it doesn't know how it should handle the ImageIcon.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
List<AnimalItem> items = new ArrayList<>(25);
items.add(new AnimalItem("Bear", new ImageIcon(getClass().getResource("/images/Bear.png"))));
items.add(new AnimalItem("Kitty", new ImageIcon(getClass().getResource("/images/Cat.png"))));
items.add(new AnimalItem("Doggy", new ImageIcon(getClass().getResource("/images/Doggy.png"))));
items.add(new AnimalItem("Foxy", new ImageIcon(getClass().getResource("/images/Fox.png"))));
items.add(new AnimalItem("Drop bear", new ImageIcon(getClass().getResource("/images/Koala.png"))));
items.add(new AnimalItem("Lion", new ImageIcon(getClass().getResource("/images/Lion.png"))));
items.add(new AnimalItem("Piggy", new ImageIcon(getClass().getResource("/images/Pig.png"))));
items.add(new AnimalItem("Bunny", new ImageIcon(getClass().getResource("/images/Rabbit.png"))));
items.add(new AnimalItem("Raccon", new ImageIcon(getClass().getResource("/images/Raccon.png"))));
AnimalTableModel tableModel = new AnimalTableModel(items);
JTable table = new JTable(tableModel);
table.setRowHeight(64);
add(new JScrollPane(table));
}
}
public class AnimalItem {
private String name;
private ImageIcon image;
public String getName() {
return name;
}
public ImageIcon getImage() {
return image;
}
public AnimalItem(String name, ImageIcon image) {
this.name = name;
this.image = image;
}
}
public class AnimalTableModel extends AbstractTableModel {
private List<AnimalItem> items;
private String[] columnNames = new String[]{
"Name",
"Image"
};
public AnimalTableModel(List<AnimalItem> items) {
this.items = items;
}
public void setItems(List<AnimalItem> items) {
this.items = items;
}
#Override
public int getRowCount() {
return items.size();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return String.class;
case 1: return ImageIcon.class;
}
return String.class;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
AnimalItem item = items.get(rowIndex);
switch (columnIndex) {
case 0:
return item.getName();
case 1:
return item.getImage();
}
return null;
}
}
}
nb: The images are been loaded directly from the application context (ie they are embedded), so you will need to supply your own images and they will need to be in the /images package
I am dynamically creating JTextField based on click event on '+' button. Below is screen shot.
The problem is when I click '+' button, fields created but not shown on JFrame. When I put the cursor on next row under 'item Name', text field becomes visible.
Where is the problem?
Below is my code.
CreateBill()
{
jf = new JFrame("Create Bill");
jf.getContentPane().setLayout(null);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setBounds(0, 0, d1.width, d1.height);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createRow(); // This will create first row by default.
jf.pack();
jf.setVisible(true);
}
private void createRow() {
textField = new JTextField();
textField.setToolTipText("item name");
textField.setBounds(143, 46+j, 288, 20);
textField.setColumns(10);
textField.getDocument().addDocumentListener(new DocumentListener()
{
#Override
public void insertUpdate(DocumentEvent e) {
updatePrice();
}
#Override
public void removeUpdate(DocumentEvent e) {
}
#Override
public void changedUpdate(DocumentEvent e) {
updatePrice();
}
});
AutoCompleteDecorator.decorate(textField, names, true);
jf.getContentPane().add(comboComplete);
jf.getContentPane().add(textField);
comboComplete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
String ItemSel = textField.getText().trim();
for(Item s:items)
{
if(s.getItemName().equals(ItemSel))
{
textField_1.setText(String.valueOf(s.getUnitPrice()));
}
}
}
});
textFields.add(textField);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setBounds(639, 46+j, 175, 20);
jf.getContentPane().add(textField_1);
textField_1.setColumns(10);
qty = new JTextField();
qty.setBounds(455, 46+j, 156, 20);
jf.getContentPane().add(qty);
qty.setColumns(10);
qty.getDocument().addDocumentListener(new DocumentListener()
{
#Override
public void insertUpdate(DocumentEvent e) {
getTotal();
}
#Override
public void removeUpdate(DocumentEvent e) {
}
#Override
public void changedUpdate(DocumentEvent e) {
getTotal();
}
});
textFields.add(qty);
textFields.add(textField_1);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setBounds(1038, 46+j, 156, 20);
jf.getContentPane().add(textField_3);
textField_3.setColumns(10);
textFields.add(textField_3);
JButton button = new JButton("+");
button.setBounds(1235, 45+j, 89, 23);
jf.getContentPane().add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("value of J"+j);
createRow(); // after pressing '+' button i am calling same method again. by changing value of j.
}
});
j=j+22;
jf.setVisible(true);
}
I want my all 4 text fields appear simultaneously.
You need to call repaint() on the container after adding a component to it. You also should call revalidate() too, before calling repaint, since this tells the layout managers to layout the new component, but you're using null layouts, something that you really want to avoid doing.
So my suggestion is to either 1) use nested JPanels with appropriate layout managers, and call revalidate and repaint on your containers after adding or removing components, or 2) yeah, use a Cardlayout to swap views as Andrew Thompson astutely recommends. You can have your second JPanel have a JTextField that uses the same Document as the previous JPanel, so it looks like both are using the same JTextField (as the top JTextField).
On looking further at your images, I have to wonder if a JTable might be an even better solution overall. And yeah, after you start using the layout managers, do also call pack() on your top level window after adding all components and before setting it visible.
For an example of a JTable implementation of this, something along the lines of...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
#SuppressWarnings("serial")
public class CreateRowGui extends JPanel {
private static final Item[] ITEMS = {
new Item("Light Bulb", 2.00),
new Item("Toilet Paper", 3.00),
new Item("Toothpaste", 1.50),
new Item("Aspirin", 3.75) };
private ItemTableModel tableModel = new ItemTableModel();
private JTable table = new JTable(tableModel);
private AddRowAction addRowAction = new AddRowAction("Add Row", KeyEvent.VK_A);
public CreateRowGui() {
TableCellRenderer moneyRenderer = new DefaultTableCellRenderer() {
private NumberFormat currFormat = NumberFormat.getCurrencyInstance();
#Override
protected void setValue(Object value) {
if (value != null) {
value = currFormat.format(value);
}
super.setValue(value);
}
};
table.getColumnModel().getColumn(2).setCellRenderer(moneyRenderer);
table.getColumnModel().getColumn(3).setCellRenderer(moneyRenderer);
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(addRowAction));
btnPanel.add(new JButton("Remove Row")); // TODO: need Action for this
setLayout(new BorderLayout());
add(new JScrollPane(table));
add(btnPanel, BorderLayout.PAGE_END);
}
class AddRowAction extends AbstractAction {
private NewRowPanel newRowPanel = new NewRowPanel(ITEMS);
public AddRowAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
newRowPanel.reset();
int reply = JOptionPane.showConfirmDialog(table,
newRowPanel.getMainPanel(),
"Select Item and Quantity",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (reply == JOptionPane.OK_OPTION) {
Item item = newRowPanel.getSelectedItem();
int quantity = newRowPanel.getQuantity();
tableModel.addRow(item, quantity);
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("CreateRowGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CreateRowGui());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class NewRowPanel {
private JPanel mainPanel = new JPanel();
private JComboBox<Item> itemsCombo;
private JSpinner quantitySpinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));
#SuppressWarnings("serial")
public NewRowPanel(Item[] items) {
itemsCombo = new JComboBox<>(items);
itemsCombo.setRenderer(new DefaultListCellRenderer(){
#Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (value != null) {
value = ((Item) value).getName();
} else {
value = "";
}
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});
mainPanel.add(new JLabel("Item:"));
mainPanel.add(itemsCombo);
mainPanel.add(Box.createHorizontalStrut(15));
mainPanel.add(new JLabel("Quantity"));
mainPanel.add(quantitySpinner);
}
public void reset() {
itemsCombo.setSelectedIndex(-1);
quantitySpinner.setValue(0);
}
public JPanel getMainPanel() {
return mainPanel;
}
public Item getSelectedItem() {
return (Item) itemsCombo.getSelectedItem();
}
public int getQuantity() {
return (int) quantitySpinner.getValue();
}
}
class ItemTableModel extends AbstractTableModel {
private static final String[] COL_NAMES = { "Item Name", "Quantity", "Unit Price", "Total" };
private static final long serialVersionUID = 1L;
private List<ItemWithCount> itemsWithCount = new ArrayList<>();
#Override
public int getColumnCount() {
return 4;
}
#Override
public int getRowCount() {
return itemsWithCount.size();
}
#Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return super.getColumnClass(columnIndex);
case 1:
return Integer.class;
case 2:
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}
#Override
public String getColumnName(int column) {
return COL_NAMES[column];
}
#Override
public Object getValueAt(int row, int column) {
ItemWithCount itemWithCount = itemsWithCount.get(row);
switch (column) {
case 0:
return itemWithCount.getItem().getName();
case 1:
return itemWithCount.getCount();
case 2:
return itemWithCount.getItem().getUnitPrice();
case 3:
return itemWithCount.getCount() * itemWithCount.getItem().getUnitPrice();
}
return null;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
ItemWithCount itemWithCount = itemsWithCount.get(rowIndex);
switch (columnIndex) {
case 1:
itemWithCount.setCount((int) aValue);
fireTableRowsUpdated(rowIndex, rowIndex);
break;
default:
break;
}
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0 || columnIndex == 1;
}
public void addRow(Item item, int quantity) {
ItemWithCount itemWithCount = new ItemWithCount(item, quantity);
itemsWithCount.add(itemWithCount);
int row = itemsWithCount.size() - 1;
fireTableRowsInserted(row, row);
}
private class ItemWithCount {
private Item item;
private int count;
public ItemWithCount(Item item, int count) {
this.item = item;
this.count = count;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Item getItem() {
return item;
}
}
}
class Item {
private String name;
private double unitPrice;
public Item(String name, double unitPrice) {
this.name = name;
this.unitPrice = unitPrice;
}
public String getName() {
return name;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
#Override
public String toString() {
return "Item [name=" + name + ", unitPrice=" + unitPrice + "]";
}
}
Okay, so what I have is a GUI with a table set up, (mostly, there are still a few problems) but what I need to know is this. Will the following code:
public void removeSelectedRows(JTable table)
{
DefaultTableModel model = (DefaultTableModel) table.getModel();
int[] rows = table.getSelectedRows();
for (int x=0;x<rows.length;++x){
model.removeRow(rows[x]-x);
}
for (int x=0;x<animals;++x)
{
if (Pets[x][0].equalsIgnoreCase(table.getValueAt(table.getSelectedRow(),0)+""))
{
Pets[x][0]=null;
Pets[x][1]=null;
Pets[x][2]=null;
Pets[x][3]=null;
Pets[x][4]=null;
}
}
animals=animals-1;
}
Delete the row that the user has selected? (By that I mean simply clicking on the row and then clicking the delete button). I don't want the row itself to be deleted but I want the values contained within the row the user has selected to be deleted.
Get ready to get you hands dirty.
At some point, DefaultTableModel will no longer meet your needs and you should be prepared to roll your own implementation.
First things first. You're working in an Object Orientated language, you should take advantage of this fact and represent your data as objects.
Secondly, when deleting multiple values from a table, it becomes really tricky quickly. Once you remove the first row, the indices no longer match, you need some way to map the values back to the index that they appear within the model.
Thirdly, the visible row indices may not be the same as those of the model, this is especially true when the table is sorted.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class TestJTable {
public static void main(String[] args) {
new TestJTable();
}
public TestJTable() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
List<Pet> pets = new ArrayList<>(25);
pets.add(new Pet("Tyrannosauridae", "TYRANNOSAURUS", 20, 35));
pets.add(new Pet("Dromaeosauridae", "VELOCIRAPTOR", 45, 90));
pets.add(new Pet("Ceratopsidae", "TRICERATOPS", 15, 30));
pets.add(new Pet("Stegosauridae", "STEGOSAURUS", 22, 25));
pets.add(new Pet("Titanosauridae", "MALAWISAURUS", 22, 25));
pets.add(new Pet("Compsognathidae", "COMPSOGNATHUS", 8, 25));
pets.add(new Pet("Brachiosauridae", "BRACHIOSAURUS", 8, 25));
pets.add(new Pet("Diplodocidae", "DIPLODOCUS", 8, 25));
final PetTableModel model = new PetTableModel(pets);
final JTable table = new JTable(model);
InputMap im = table.getInputMap(JTable.WHEN_FOCUSED);
ActionMap am = table.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
am.put("delete", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
int[] indices= table.getSelectedRows();
// Convert the view's row indices to the models...
int[] mapped = new int[indices.length];
for (int index = 0; index < indices.length; index++) {
mapped[index] = table.convertRowIndexToModel(indices[index]);
}
model.removePets(mapped);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PetTableModel extends AbstractTableModel {
private List<Pet> pets;
public PetTableModel() {
pets = new ArrayList<>(25);
}
public PetTableModel(List<Pet> pets) {
this.pets = pets;
}
#Override
public int getRowCount() {
return pets.size();
}
public void removePets(int... indicies) {
// Build a tempory list of Pet objects based
// on the supplied indices...
List<Pet> old = new ArrayList<>(indicies.length);
for (int index : indicies) {
old.add(pets.get(index));
}
// For each pet, get it's index in the model
// remove it from the model
// notify any listeners of the change to the model...
for (Pet pet : old) {
int index = pets.indexOf(pet);
pets.remove(pet);
fireTableRowsDeleted(index, index);
}
}
#Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
switch (columnIndex) {
case 2:
case 3:
clazz = Float.class;
}
return clazz;
}
#Override
public String getColumnName(int column) {
String name = "??";
switch (column) {
case 0:
name = "Breed";
break;
case 1:
name = "Category";
break;
case 2:
name = "Buy Price";
break;
case 3:
name = "Sell Price";
break;
}
return name;
}
#Override
public int getColumnCount() {
return 4;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Pet pet = pets.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = pet.getBreed();
break;
case 1:
value = pet.getCategory();
break;
case 2:
value = pet.getBuyPrice();
break;
case 3:
value = pet.getSellPrice();
break;
}
return value;
}
public void add(Pet pet) {
pets.add(pet);
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
}
public class Pet {
private String breed;
private String category;
private float buyPrice;
private float sellPrice;
public Pet(String breed, String category, float buyPrice, float sellPrice) {
this.breed = breed;
this.category = category;
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
}
public String getBreed() {
return breed;
}
public float getBuyPrice() {
return buyPrice;
}
public String getCategory() {
return category;
}
public float getSellPrice() {
return sellPrice;
}
}
}
I have a jtable that show a text file contain on own.
Each line in text file is a user record.
I add a edit button to edit each selected row on table.
I use this code, But i don't understand how to do this:
public class AllUser extends AbstractTableModel {
UserInformation uiS = new UserInformation();
String[] col = {"ID", "Fname", "Lname", "Gender", "Date"};
List<UserInformation> Udata = new ArrayList<UserInformation>();
public AllUser() {
BufferedReader br = null;
try {
FileReader fr = new FileReader("xxx.txt");
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
Udata.add(initializeUserInfos(line));
}
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ioe) {
}
}
}
}
private UserInformation initializeUserInfos(String str) {
UserInformation Uinit = new UserInformation();
String[] CellArray = str.split(" ");
Uinit.setID(CellArray[0]);
Uinit.setFname(CellArray[1]);
Uinit.setLname(CellArray[2]);
Uinit.setGender(CellArray[3]);
Uinit.setDate(CellArray[4]);
return Uinit;
}
#Override
public String getColumnName(int colu) {
return col[colu];
}
#Override
public int getRowCount() {
return Udata.size();
}
#Override
public int getColumnCount() {
return col.length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
UserInformation uinfoS = Udata.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = uinfoS.getID();
break;
case 1:
value = uinfoS.getFname();
break;
case 2:
value = uinfoS.getLname();
break;
case 3:
value = uinfoS.getGender();
break;
case 4:
value = uinfoS.getDate();
break;
default:
value = "...";
}
return value;
}
#Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
UserInformation userInfo = new UserInformation();
switch (columnIndex) {
case 0:
userInfo.setID((String) value);
break;
case 1:
userInfo.setFname((String) value);
break;
case 2:
userInfo.setLname((String) value);
break;
case 3:
userInfo.setGender((String) value);
break;
case 4:
userInfo.setDate((String) value);
break;
}
}
public Object getSelectedMember(int row){
UserInformation userInf=new UserInformation();
Object fname=userInf.getFname();
Object lname=userInf.getLname();
Object gender=userInf.getGender();
Object date=userInf.getDate();
return // ?
}
}
I add This getSelectedMember(int row) method to do this, But incorrect!
public class UserPage extends JFrame implements ActionListener {
private AllUser userModel;
private JTable uTable;
private JButton editButton;
public UserPage() {
userModel = new AllUser();
uTable = new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels(), BorderLayout.PAGE_START);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
}
public final JPanel buttonPanels() {
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
editButton=new JButton("Edit");
editButton.addActionListener(this);
buttonsPanel.add(editButton);
return buttonsPanel;
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== editButton){
int selectedRow=uTable.getSelectedRow();
if(selectedRow>0){
editUser(selectedRow);
}
else{
JOptionPane.showMessageDialog(null, "Select a user");
}
}
}
public void editUser(int row){
userModel.getSelectedMember(row);
new NewUserFrame_Edit().setVisible(true);
}
}
UserInformation Class:
public class UserInformation {
private String Fname;
private String Lname;
private String ID;
private String Gender;
private String Date;
public String getFname() {
return Fname;
}
public void setFname(String fname) {
this.Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
this.Lname = lname;
}
public String getID() {
return ID;
}
public void setID(String i_d) {
this.ID = i_d;
}
public String getGender() {
return Gender;
}
public void setGender(String gndr) {
this.Gender = gndr;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
this.Date = date;
}
#Override
public String toString() {
return ID + " " + Fname + " "
+ Lname + " " + Gender + " " + Date + "\n";
}
}
Sorry for long code.
It sould be like this: when user click a row in jtable and clicked a new page should open and then each old data should insert in own textfiled
Add a MouseListener to the table.
On a double click you create a JDialog that contains the text fields required to edit the data.
You then get the data from the table using the getValueAt(...) method.
the user then edits the data in the text fields.
When the user clicks the "Save" button on the dialog you get the data from each text field and update the table using the setValueAt(...) method.
if you want to save the data in your text file you then need to recreate the enitre file by writing out all the data from the table.
Create an extra column in table and fill the cell value with Edit. Then render the Edit cell with appropriate TableCellRenderer . Add MouseListener to table. Get the row and column number of the cell clicked on table. If the cell clicked contains value Edit , retrieve value at each column of that row and display it in a Panel containing JTextFields . Show that Panel using JOptionpane or in a JDialog. Here is the brief code demo for achieving this task:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.Cursor;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.DefaultTableCellRenderer;
class TableRowEdit extends JFrame
{
private JTable table;
private JScrollPane jsPane;
private TableModel myModel;
private JPanel dialogPanel;
private JTextField tf[];
private JLabel lbl[];
public void prepareAndShowGUI()
{
myModel = new MyModel();
table = new JTable(myModel);
jsPane = new JScrollPane(table);
table.getColumnModel().getColumn(2).setCellRenderer(new LabelCellRenderer());
table.addMouseListener(new MyMouseAdapter());
getContentPane().add(jsPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prepareDialogPanel();
pack();
setVisible(true);
}
private void prepareDialogPanel()
{
dialogPanel = new JPanel();
int col = table.getColumnCount() - 1;
dialogPanel.setLayout(new GridLayout(col,2));
tf = new JTextField[col];
lbl = new JLabel[col];
for (int i = 0; i < col; i++)
{
lbl[i] = new JLabel(table.getColumnName(i));
tf[i] = new JTextField(10);
dialogPanel.add(lbl[i]);
dialogPanel.add(tf[i]);
}
}
private void populateTextField(String[] s)
{
for (int i = 0 ; i < s.length ; i++ )
{
tf[i].setText(s[i]);
}
}
public class LabelCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,Object oValue, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, oValue,isSelected, hasFocus,row, column);
String value = (String)oValue;
JLabel label =(JLabel)c;
label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
label.setBackground(Color.lightGray);
label.setHorizontalTextPosition(SwingUtilities.CENTER);
label.setHorizontalAlignment(SwingUtilities.CENTER);
label.setText(value);
return label;
}
}
private class MyMouseAdapter extends MouseAdapter
{
#Override
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int row = table.rowAtPoint(new Point(x,y));
int col = table.columnAtPoint(new Point(x,y));
if (col == 2)
{
String arr[] = new String[table.getColumnCount() - 1];
for (int i = 0 ; i < arr.length ; i++)
{
arr[i] = (String)table.getValueAt(row,i);
}
populateTextField(arr);
JOptionPane.showMessageDialog(TableRowEdit.this,dialogPanel,"Information",JOptionPane.INFORMATION_MESSAGE);
//Write here the code to save changed values to file and update to JTable also.
}
}
}
private class MyModel extends AbstractTableModel
{
String[] columns = {
"Roll No.",
"Name",
"Action"
};
String[][] inData = {
{"1","Huge Jackman","Edit"},
{"2","Thomas Andrews","Edit"},
{"3","Shiney","Edit"}
};
#Override
public void setValueAt(Object value, int row, int col)
{
inData[row][col] = (String)value;
}
#Override
public Object getValueAt(int row, int col)
{
return inData[row][col];
}
#Override
public int getColumnCount()
{
return columns.length;
}
#Override
public int getRowCount()
{
return inData.length;
}
#Override
public String getColumnName(int col)
{
return columns[col];
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater( new Runnable()
{
#Override
public void run()
{
TableRowEdit td = new TableRowEdit();
td.prepareAndShowGUI();
}
});
}
}